Fix router guard broken cookie check to use isLuciaLoggedIn cookie
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
39
tests/router/routerGuard.test.js
Normal file
39
tests/router/routerGuard.test.js
Normal file
@@ -0,0 +1,39 @@
|
||||
import { describe, it, expect, beforeEach } from 'vitest';
|
||||
|
||||
describe('router beforeEach guard logic', () => {
|
||||
beforeEach(() => {
|
||||
// Clear cookies
|
||||
document.cookie.split(';').forEach((c) => {
|
||||
const name = c.split('=')[0].trim();
|
||||
if (name) {
|
||||
document.cookie = name + '=; Max-Age=-99999999; path=/';
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Simulate the guard logic from router/index.ts
|
||||
function runGuard(to) {
|
||||
const isLoggedIn = document.cookie
|
||||
.split(';')
|
||||
.some((c) => c.trim().startsWith('isLuciaLoggedIn='));
|
||||
|
||||
if (to.name === 'Login') {
|
||||
if (isLoggedIn) return { name: 'Files' };
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
it('redirects logged-in user from Login to Files', () => {
|
||||
document.cookie = 'isLuciaLoggedIn=true';
|
||||
expect(runGuard({ name: 'Login' })).toEqual({ name: 'Files' });
|
||||
});
|
||||
|
||||
it('allows unauthenticated user to visit Login', () => {
|
||||
expect(runGuard({ name: 'Login' })).toBeUndefined();
|
||||
});
|
||||
|
||||
it('does not interfere with non-Login routes', () => {
|
||||
document.cookie = 'isLuciaLoggedIn=true';
|
||||
expect(runGuard({ name: 'Files' })).toBeUndefined();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user