fix: auto bootstrap org context outside index route
Ensure authenticated users without current_org_id are redirected or auto-switched before entering business routes, preventing global 500 errors after multi-organization rollout. Made-with: Cursor
This commit is contained in:
+45
-1
@@ -10,8 +10,9 @@ import '~/fontawesome/all.css';
|
||||
import './index.less'
|
||||
// @ts-ignore
|
||||
import useUser from '~/store/user'
|
||||
import useOrg from "~/store/org";
|
||||
import cookie from "~/utils/cookie";
|
||||
import {CurrentUserRole, HtyAuthToken, HtySudoToken, HtyUnionIDToken, pad} from "~/utils";
|
||||
import {CurrentOrgId, CurrentUserRole, HtyAuthToken, HtySudoToken, HtyUnionIDToken, pad} from "~/utils";
|
||||
import {HtyRoles, HtyStates, UserStates} from "~/types";
|
||||
import useTimer from "~/store/timer";
|
||||
|
||||
@@ -39,9 +40,29 @@ Date.prototype.toJSON = function () {
|
||||
return [year, pad(month), pad(date)].join('-') + 'T' + [pad(hour), pad(minute), pad(second)].join(':')
|
||||
}
|
||||
|
||||
const parseCurrentOrgIdFromToken = (token: string | null): string | undefined => {
|
||||
if (!token) return undefined;
|
||||
try {
|
||||
const tokenParts = token.split('.');
|
||||
if (tokenParts.length < 2) return undefined;
|
||||
const payloadRaw = tokenParts[1].replace(/-/g, '+').replace(/_/g, '/');
|
||||
const payloadJson = decodeURIComponent(
|
||||
atob(payloadRaw)
|
||||
.split('')
|
||||
.map((char) => `%${(`00${char.charCodeAt(0).toString(16)}`).slice(-2)}`)
|
||||
.join('')
|
||||
);
|
||||
const payload = JSON.parse(payloadJson);
|
||||
return payload.current_org_id || undefined;
|
||||
} catch (_error) {
|
||||
return undefined;
|
||||
}
|
||||
};
|
||||
|
||||
router.beforeEach(async (to, from , next) => {
|
||||
document.title = to.meta.title as string;
|
||||
let { store, read, login, logout, chooseRole, getUnreadTongzhis, set_editing } = useUser();
|
||||
let { store: orgStore, loadMyOrgs, switchOrg } = useOrg();
|
||||
set_editing(false)
|
||||
store.unionid = (store.unionid || cookie.get(HtyUnionIDToken) || '').toString();
|
||||
if (!store.current.hty_id) {
|
||||
@@ -126,6 +147,29 @@ router.beforeEach(async (to, from , next) => {
|
||||
getUnreadTongzhis();
|
||||
}
|
||||
}
|
||||
|
||||
if (enabled && is_registered && to.path !== '/' && to.path !== '/org/select') {
|
||||
const authToken = window.localStorage.getItem(HtyAuthToken);
|
||||
const tokenOrgId = parseCurrentOrgIdFromToken(authToken);
|
||||
if (!tokenOrgId) {
|
||||
const organizations = await loadMyOrgs();
|
||||
if (organizations.length === 1) {
|
||||
const switched = await switchOrg(organizations[0].id);
|
||||
if (!switched) {
|
||||
next(false);
|
||||
return;
|
||||
}
|
||||
} else if (organizations.length > 1) {
|
||||
await router.replace('/org/select');
|
||||
next();
|
||||
return;
|
||||
}
|
||||
} else if (!orgStore.currentOrgId) {
|
||||
window.localStorage.setItem(CurrentOrgId, tokenOrgId);
|
||||
orgStore.currentOrgId = tokenOrgId;
|
||||
}
|
||||
}
|
||||
|
||||
let {stopTimer} = useTimer()
|
||||
stopTimer();
|
||||
if (to.path.endsWith("/profile") && store.currentRole && !to.path.includes(store.currentRole.toLowerCase())) {
|
||||
|
||||
Reference in New Issue
Block a user