登录功能
All checks were successful
Docker Build & Deploy / Build Docker Image (push) Successful in 30s
Docker Build & Deploy / Deploy to Production (push) Successful in 5s

This commit is contained in:
孙诚
2025-12-25 13:27:23 +08:00
parent ebb49577dd
commit 728c39f43d
16 changed files with 395 additions and 23 deletions

View File

@@ -1,29 +1,57 @@
import { createRouter, createWebHistory } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
routes: [
{
path: '/login',
name: 'login',
component: () => import('../views/LoginView.vue'),
meta: { requiresAuth: false },
},
{
path: '/',
name: 'transactions',
component: () => import('../views/TransactionsRecord.vue'),
meta: { requiresAuth: true },
},
{
path: '/email',
name: 'email',
component: () => import('../views/EmailRecord.vue'),
meta: { requiresAuth: true },
},
{
path: '/setting',
name: 'setting',
component: () => import('../views/SettingView.vue'),
meta: { requiresAuth: true },
},
{
path: '/calendar',
name: 'calendar',
component: () => import('../views/CalendarView.vue'),
meta: { requiresAuth: true },
},
],
})
// 路由守卫
router.beforeEach((to, from, next) => {
const authStore = useAuthStore()
const requiresAuth = to.meta.requiresAuth !== false // 默认需要认证
if (requiresAuth && !authStore.isAuthenticated) {
// 需要认证但未登录,跳转到登录页
next({ name: 'login', query: { redirect: to.fullPath } })
} else if (to.name === 'login' && authStore.isAuthenticated) {
// 已登录用户访问登录页,跳转到首页
next({ name: 'transactions' })
} else {
next()
}
})
export default router