client: fix redirect

This commit is contained in:
fantasticit 2022-07-14 21:49:17 +08:00
parent 4bed78aa64
commit 9560066cfa
1 changed files with 10 additions and 2 deletions

View File

@ -12,9 +12,17 @@ import { HttpClient } from 'services/http-client';
*/ */
export const toLogin = () => { export const toLogin = () => {
const currentPath = Router.asPath; const currentPath = Router.asPath;
const maybeRedirect = Router.query?.redirect;
const isInLogin = currentPath.startsWith('login'); const isInLogin = currentPath.startsWith('login');
if (!isInLogin) { if (!isInLogin) {
Router.push(`/login?redirect=${currentPath}`); let next = maybeRedirect || currentPath;
if (next.includes('login')) {
next = '/app';
}
Router.replace(`/login?redirect=${next}`);
} }
}; };
@ -108,7 +116,7 @@ export const useUser = () => {
const user = res as unknown as ILoginUser; const user = res as unknown as ILoginUser;
refetch(); refetch();
setStorage('user', JSON.stringify(user)); setStorage('user', JSON.stringify(user));
user.token && setStorage('token,', user.token); user.token && setStorage('token', user.token);
const next = router.query?.redirect || '/app'; const next = router.query?.redirect || '/app';
Router.replace(next as string); Router.replace(next as string);
}); });