diff --git a/src/app/login/page.tsx b/src/app/login/page.tsx index c715348..257902f 100644 --- a/src/app/login/page.tsx +++ b/src/app/login/page.tsx @@ -1,14 +1,29 @@ 'use client'; -import { useState } from 'react'; +import { useState, useEffect } from 'react'; import { useRouter } from 'next/navigation'; export default function LoginPage() { const [username, setUsername] = useState(''); const [password, setPassword] = useState(''); const [error, setError] = useState(null); + const [checking, setChecking] = useState(true); const router = useRouter(); + // Check if already logged in + useEffect(() => { + fetch('/api/auth/me', { credentials: 'include' }) + .then(r => r.json()) + .then(data => { + if (data.authenticated) { + router.push(data.role === 'admin' ? '/admin' : '/user'); + } else { + setChecking(false); + } + }) + .catch(() => setChecking(false)); + }, [router]); + const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setError(null); @@ -30,6 +45,17 @@ export default function LoginPage() { router.push(data.role === 'admin' ? '/admin' : '/user'); }; + if (checking) { + return ( +
+
+
+
Checking authentication...
+
+
+ ); + } + const accent = '#E8A849'; const navy = '#010D1E'; const cream = '#f5f0e8'; diff --git a/src/middleware.ts b/src/middleware.ts index bdcc5ec..157a8ad 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -1,21 +1,10 @@ import { NextResponse } from 'next/server'; import type { NextRequest } from 'next/server'; -// Simple middleware for Next.js Edge Runtime -// This is a basic implementation that doesn't verify tokens -// In a production environment, you would want to verify the token +// Middleware runs on edge runtime - we can't verify JWT here easily +// Let the pages handle auth checks via /api/auth/me export function middleware(request: NextRequest) { - const { pathname } = request.nextUrl; - const token = request.cookies.get('session')?.value; - - // Protect admin routes - let page handle auth UI - // No redirect here; the admin page shows login prompt if not authenticated - - // If logged in and trying to access login, redirect to admin - if (pathname === '/login' && token) { - return NextResponse.redirect(new URL('/admin', request.url)); - } - + // No redirects - login and admin pages handle their own auth UI return NextResponse.next(); }