mirror of
https://github.com/Nezumi-2711/9router.git
synced 2026-09-22 20:00:47 +00:00
feat(auth): Enhance authentication flow and settings management
This commit is contained in:
+15
-5
@@ -12,16 +12,26 @@ export async function proxy(request) {
|
||||
if (pathname.startsWith("/dashboard")) {
|
||||
const token = request.cookies.get("auth_token")?.value;
|
||||
|
||||
if (!token) {
|
||||
return NextResponse.redirect(new URL("/login", request.url));
|
||||
if (token) {
|
||||
try {
|
||||
await jwtVerify(token, SECRET);
|
||||
return NextResponse.next();
|
||||
} catch (err) {
|
||||
return NextResponse.redirect(new URL("/login", request.url));
|
||||
}
|
||||
}
|
||||
|
||||
const origin = request.nextUrl.origin;
|
||||
try {
|
||||
await jwtVerify(token, SECRET);
|
||||
return NextResponse.next();
|
||||
const res = await fetch(`${origin}/api/settings/require-login`);
|
||||
const data = await res.json();
|
||||
if (data.requireLogin === false) {
|
||||
return NextResponse.next();
|
||||
}
|
||||
} catch (err) {
|
||||
return NextResponse.redirect(new URL("/login", request.url));
|
||||
// On error, require login
|
||||
}
|
||||
return NextResponse.redirect(new URL("/login", request.url));
|
||||
}
|
||||
|
||||
// Redirect / to /dashboard if logged in, or /dashboard if it's the root
|
||||
|
||||
Reference in New Issue
Block a user