forked from cartsnitch/cartsnitch
fix(e2e): resolve lint error, Dashboard auth gap, and mock auth redirect
- Remove unused `response` variable in j8-unauth-access.spec.ts:40 - Move Dashboard route inside ProtectedRoute wrapper in App.tsx - Add VITE_MOCK_AUTH mode to ProtectedRoute: check Zustand isAuthenticated flag instead of calling authClient.useSession() Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
@@ -37,7 +37,7 @@ test.describe('J8: Unauthenticated Access', () => {
|
|||||||
test('shows loading spinner while auth session is pending', async ({ page }) => {
|
test('shows loading spinner while auth session is pending', async ({ page }) => {
|
||||||
// Intercept but don't respond — session stays pending
|
// Intercept but don't respond — session stays pending
|
||||||
await page.context().clearCookies();
|
await page.context().clearCookies();
|
||||||
const response = await page.request.fetch('/api/auth/session', {
|
await page.request.fetch('/api/auth/session', {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -31,8 +31,8 @@ export default function App() {
|
|||||||
<BrowserRouter>
|
<BrowserRouter>
|
||||||
<Routes>
|
<Routes>
|
||||||
<Route element={<Layout />}>
|
<Route element={<Layout />}>
|
||||||
<Route index element={<Dashboard />} />
|
|
||||||
<Route element={<ProtectedRoute />}>
|
<Route element={<ProtectedRoute />}>
|
||||||
|
<Route index element={<Dashboard />} />
|
||||||
<Route path="purchases" element={<Purchases />} />
|
<Route path="purchases" element={<Purchases />} />
|
||||||
<Route path="purchases/:id" element={<PurchaseDetail />} />
|
<Route path="purchases/:id" element={<PurchaseDetail />} />
|
||||||
<Route path="products" element={<Products />} />
|
<Route path="products" element={<Products />} />
|
||||||
|
|||||||
@@ -4,12 +4,22 @@ import { authClient } from '../lib/auth-client.ts'
|
|||||||
import { useAuthStore } from '../stores/auth.ts'
|
import { useAuthStore } from '../stores/auth.ts'
|
||||||
|
|
||||||
export function ProtectedRoute() {
|
export function ProtectedRoute() {
|
||||||
|
const isMockAuth = import.meta.env.VITE_MOCK_AUTH === 'true'
|
||||||
const { data: session, isPending } = authClient.useSession()
|
const { data: session, isPending } = authClient.useSession()
|
||||||
|
const isAuthenticated = useAuthStore((s) => s.isAuthenticated)
|
||||||
const setAuthenticated = useAuthStore((s) => s.setAuthenticated)
|
const setAuthenticated = useAuthStore((s) => s.setAuthenticated)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setAuthenticated(!!session)
|
if (!isMockAuth) {
|
||||||
}, [session, setAuthenticated])
|
setAuthenticated(!!session)
|
||||||
|
}
|
||||||
|
}, [session, setAuthenticated, isMockAuth])
|
||||||
|
|
||||||
|
// In mock auth mode, rely on Zustand store (set by Login/Register pages)
|
||||||
|
if (isMockAuth) {
|
||||||
|
if (!isAuthenticated) return <Navigate to="/login" replace />
|
||||||
|
return <Outlet />
|
||||||
|
}
|
||||||
|
|
||||||
if (isPending) {
|
if (isPending) {
|
||||||
return (
|
return (
|
||||||
|
|||||||
Reference in New Issue
Block a user