feat:添加登录功能
This commit is contained in:
@@ -1,29 +1,30 @@
|
||||
import { createRouter, createWebHistory, type RouteRecordRaw } from 'vue-router';
|
||||
import { useAuthStore } from '../stores/auth';
|
||||
|
||||
const routes: RouteRecordRaw[] = [
|
||||
{
|
||||
path: '/',
|
||||
name: 'dashboard',
|
||||
component: () => import('../features/dashboard/pages/DashboardPage.vue'),
|
||||
meta: { title: '仪表盘' }
|
||||
meta: { title: '仪表盘', requiresAuth: true }
|
||||
},
|
||||
{
|
||||
path: '/transactions',
|
||||
name: 'transactions',
|
||||
component: () => import('../features/transactions/pages/TransactionsPage.vue'),
|
||||
meta: { title: '交易记录' }
|
||||
meta: { title: '交易记录', requiresAuth: true }
|
||||
},
|
||||
{
|
||||
path: '/analysis',
|
||||
name: 'analysis',
|
||||
component: () => import('../features/analysis/pages/AnalysisPage.vue'),
|
||||
meta: { title: 'AI 智能分析' }
|
||||
meta: { title: 'AI 智能分析', requiresAuth: true }
|
||||
},
|
||||
{
|
||||
path: '/settings',
|
||||
name: 'settings',
|
||||
component: () => import('../features/settings/pages/SettingsPage.vue'),
|
||||
meta: { title: '设置' }
|
||||
meta: { title: '设置', requiresAuth: true }
|
||||
},
|
||||
{
|
||||
path: '/auth',
|
||||
@@ -56,6 +57,22 @@ const router = createRouter({
|
||||
routes
|
||||
});
|
||||
|
||||
router.beforeEach((to, _from, next) => {
|
||||
const authStore = useAuthStore();
|
||||
if (to.meta.requiresAuth && !authStore.isAuthenticated) {
|
||||
return next({
|
||||
name: 'login',
|
||||
query: to.fullPath === '/' ? undefined : { redirect: to.fullPath }
|
||||
});
|
||||
}
|
||||
|
||||
if (authStore.isAuthenticated && to.name && ['login', 'register', 'forgot-password'].includes(to.name.toString())) {
|
||||
return next({ name: 'dashboard' });
|
||||
}
|
||||
|
||||
return next();
|
||||
});
|
||||
|
||||
router.afterEach((to) => {
|
||||
if (to.meta.title) {
|
||||
document.title = `AI 记账 · ${to.meta.title}`;
|
||||
|
||||
Reference in New Issue
Block a user