diff --git a/packages/core/src/context.ts b/packages/core/src/context.ts index 554083ad3d..feedceef1c 100644 --- a/packages/core/src/context.ts +++ b/packages/core/src/context.ts @@ -468,6 +468,7 @@ export class Context { private createTimerDispose(timer: NodeJS.Timeout) { const dispose = () => { clearTimeout(timer) + if (!this.state) return return remove(this.state.disposables, dispose) } this.state.disposables.push(dispose) diff --git a/plugins/frontend/auth/client/index.ts b/plugins/frontend/auth/client/index.ts index 2b5e9357a2..06b9b24ec5 100644 --- a/plugins/frontend/auth/client/index.ts +++ b/plugins/frontend/auth/client/index.ts @@ -1,6 +1,5 @@ -import { Context } from '~/client' +import { Context, store } from '~/client' import { Icons } from '~/components' -import { user } from './utils' import Login from './login.vue' import Profile from './profile.vue' import SignIn from './icons/sign-in.vue' @@ -12,11 +11,19 @@ Icons.register('sign-out', SignOut) Icons.register('user-full', UserFull) export default (ctx: Context) => { + const dispose = Context.router.beforeEach((route, from) => { + if (route.meta.authority && !store.user) { + return history.state.forward === '/login' ? '/' : '/login' + } + }) + + ctx.disposables.push(dispose) + ctx.addPage({ path: '/login', name: '登录', icon: 'sign-in', - position: () => user.value ? 'hidden' : 'bottom', + position: () => store.user ? 'hidden' : 'bottom', component: Login, }) @@ -24,7 +31,7 @@ export default (ctx: Context) => { path: '/profile', name: '用户资料', icon: 'user-full', - position: () => user.value ? 'bottom' : 'hidden', + position: () => store.user ? 'bottom' : 'hidden', component: Profile, }) } diff --git a/plugins/frontend/auth/client/login.vue b/plugins/frontend/auth/client/login.vue index 65ad920c32..d94e89beb8 100644 --- a/plugins/frontend/auth/client/login.vue +++ b/plugins/frontend/auth/client/login.vue @@ -1,12 +1,12 @@