fix: remove middleware redirect, let login page handle auth check
This commit is contained in:
+27
-1
@@ -1,14 +1,29 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { useState } from 'react';
|
import { useState, useEffect } from 'react';
|
||||||
import { useRouter } from 'next/navigation';
|
import { useRouter } from 'next/navigation';
|
||||||
|
|
||||||
export default function LoginPage() {
|
export default function LoginPage() {
|
||||||
const [username, setUsername] = useState('');
|
const [username, setUsername] = useState('');
|
||||||
const [password, setPassword] = useState('');
|
const [password, setPassword] = useState('');
|
||||||
const [error, setError] = useState<string | null>(null);
|
const [error, setError] = useState<string | null>(null);
|
||||||
|
const [checking, setChecking] = useState(true);
|
||||||
const router = useRouter();
|
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) => {
|
const handleSubmit = async (e: React.FormEvent) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
setError(null);
|
setError(null);
|
||||||
@@ -30,6 +45,17 @@ export default function LoginPage() {
|
|||||||
router.push(data.role === 'admin' ? '/admin' : '/user');
|
router.push(data.role === 'admin' ? '/admin' : '/user');
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (checking) {
|
||||||
|
return (
|
||||||
|
<main style={{ minHeight: '100vh', display: 'flex', alignItems: 'center', justifyContent: 'center', background: '#FAF7F0' }}>
|
||||||
|
<div style={{ textAlign: 'center' }}>
|
||||||
|
<div style={{ fontSize: '32px', marginBottom: '16px' }}>⏳</div>
|
||||||
|
<div style={{ fontSize: '16px', color: '#555' }}>Checking authentication...</div>
|
||||||
|
</div>
|
||||||
|
</main>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const accent = '#E8A849';
|
const accent = '#E8A849';
|
||||||
const navy = '#010D1E';
|
const navy = '#010D1E';
|
||||||
const cream = '#f5f0e8';
|
const cream = '#f5f0e8';
|
||||||
|
|||||||
+3
-14
@@ -1,21 +1,10 @@
|
|||||||
import { NextResponse } from 'next/server';
|
import { NextResponse } from 'next/server';
|
||||||
import type { NextRequest } from 'next/server';
|
import type { NextRequest } from 'next/server';
|
||||||
|
|
||||||
// Simple middleware for Next.js Edge Runtime
|
// Middleware runs on edge runtime - we can't verify JWT here easily
|
||||||
// This is a basic implementation that doesn't verify tokens
|
// Let the pages handle auth checks via /api/auth/me
|
||||||
// In a production environment, you would want to verify the token
|
|
||||||
export function middleware(request: NextRequest) {
|
export function middleware(request: NextRequest) {
|
||||||
const { pathname } = request.nextUrl;
|
// No redirects - login and admin pages handle their own auth UI
|
||||||
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));
|
|
||||||
}
|
|
||||||
|
|
||||||
return NextResponse.next();
|
return NextResponse.next();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user