claude rework
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
import { describe, it, expect, beforeEach } from 'vitest'
|
||||
import router from '../index'
|
||||
|
||||
describe('router auth guard', () => {
|
||||
beforeEach(() => {
|
||||
localStorage.clear()
|
||||
})
|
||||
|
||||
it('redirects unauthenticated users away from protected routes', async () => {
|
||||
await router.push('/feeds')
|
||||
|
||||
expect(router.currentRoute.value.name).toBe('login')
|
||||
})
|
||||
|
||||
it('lets authenticated users reach protected routes', async () => {
|
||||
localStorage.setItem('user-token', 'abc123')
|
||||
|
||||
await router.push('/feeds')
|
||||
|
||||
expect(router.currentRoute.value.name).toBe('feeds')
|
||||
})
|
||||
|
||||
it('redirects the root path to the feeds route', async () => {
|
||||
localStorage.setItem('user-token', 'abc123')
|
||||
|
||||
await router.push('/')
|
||||
|
||||
expect(router.currentRoute.value.name).toBe('feeds')
|
||||
})
|
||||
})
|
||||
+3
-17
@@ -1,32 +1,20 @@
|
||||
import { createRouter, createWebHistory } from 'vue-router'
|
||||
import HomeView from '../views/HomeView.vue'
|
||||
|
||||
const router = createRouter({
|
||||
history: createWebHistory(import.meta.env.BASE_URL),
|
||||
routes: [
|
||||
{
|
||||
path: '/',
|
||||
name: 'home',
|
||||
component: HomeView,
|
||||
meta: { requiresAuth: true }, // Add a meta field to indicate that this route requires authentication
|
||||
redirect: '/feeds',
|
||||
},
|
||||
{
|
||||
path: '/feeds',
|
||||
name: 'feeds',
|
||||
// route level code-splitting
|
||||
// this generates a separate chunk (About.[hash].js) for this route
|
||||
// this generates a separate chunk (Feed.[hash].js) for this route
|
||||
// which is lazy-loaded when the route is visited.
|
||||
component: () => import('../views/FeedView.vue'),
|
||||
meta: { requiresAuth: true }, // Add a meta field to indicate that this route requires authentication
|
||||
},
|
||||
{
|
||||
path: '/about',
|
||||
name: 'about',
|
||||
// route level code-splitting
|
||||
// this generates a separate chunk (About.[hash].js) for this route
|
||||
// which is lazy-loaded when the route is visited.
|
||||
component: () => import('../views/AboutView.vue'),
|
||||
meta: { requiresAuth: true }, // Add a meta field to indicate that this route requires authentication
|
||||
meta: { requiresAuth: true },
|
||||
},
|
||||
{
|
||||
path: '/login',
|
||||
@@ -38,13 +26,11 @@ const router = createRouter({
|
||||
})
|
||||
router.beforeEach((to, from, next) => {
|
||||
if (to.meta.requiresAuth) {
|
||||
// TODO Check if the user is authenticated (e.g., check for a valid token)
|
||||
let isAuthenticated = false;
|
||||
if (localStorage.getItem("user-token") != null){
|
||||
isAuthenticated = true;
|
||||
}
|
||||
|
||||
|
||||
if (!isAuthenticated) {
|
||||
// Redirect to the login page
|
||||
next('/login');
|
||||
|
||||
Reference in New Issue
Block a user