If user is authenticated, redirect to app from /login and /register pages
This commit is contained in:
@@ -3,35 +3,53 @@ import { NextResponse } from "next/server";
|
||||
import { cookies } from "next/headers";
|
||||
|
||||
export async function middleware(req, res) {
|
||||
const session = req.cookies.get("session");
|
||||
// Login if not logged in
|
||||
if (!session) {
|
||||
return NextResponse.redirect(new URL("/login", req.url));
|
||||
}
|
||||
//Call the authentication endpoint
|
||||
const responseAPI = await fetch(new URL("/api/login", req.url), {
|
||||
headers: {
|
||||
Cookie: `session=${session?.value}`,
|
||||
},
|
||||
});
|
||||
const session = await req.cookies.get("session");
|
||||
if (req.nextUrl.pathname !== "/login" && req.nextUrl.pathname != "/register") {
|
||||
// Login if not logged in
|
||||
if (!session) {
|
||||
return NextResponse.redirect(new URL("/login", req.url));
|
||||
}
|
||||
//Call the authentication endpoint
|
||||
const responseAPI = await fetch(new URL("/api/login", req.url), {
|
||||
headers: {
|
||||
Cookie: `session=${session?.value}`,
|
||||
},
|
||||
});
|
||||
|
||||
// Login if unauthorized
|
||||
if (responseAPI.status !== 200) {
|
||||
return NextResponse.redirect(new URL("/login", req.url));
|
||||
}
|
||||
// Login if unauthorized
|
||||
if (responseAPI.status !== 200) {
|
||||
return NextResponse.redirect(new URL("/login", req.url));
|
||||
}
|
||||
|
||||
// If new user, redirect to onboarding
|
||||
var user = JSON.parse(req.cookies.get("user").value)
|
||||
if (user.defined) {
|
||||
return NextResponse.next();
|
||||
// If new user, redirect to onboarding
|
||||
var user = JSON.parse(req.cookies.get("user").value)
|
||||
if (user.defined) {
|
||||
return NextResponse.next();
|
||||
} else {
|
||||
return NextResponse.redirect(new URL("/onboarding", req.url));
|
||||
}
|
||||
} else {
|
||||
return NextResponse.redirect(new URL("/onboarding", req.url));
|
||||
// Currently in the /login or /register, if user is authenticated, go ahead and direct them to the app
|
||||
if (session) {
|
||||
const responseAPI = await fetch(new URL("/api/login", req.url), {
|
||||
headers: {
|
||||
Cookie: `session=${session?.value}`,
|
||||
},
|
||||
});
|
||||
if (responseAPI.status == 200) {
|
||||
return NextResponse.redirect(new URL("/app", req.url))
|
||||
} else {
|
||||
return NextResponse.next() // Unauthenticated, continue
|
||||
}
|
||||
} else {
|
||||
return NextResponse.next() // Not logged in, direct to login
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Protected routes
|
||||
export const config = {
|
||||
matcher: ['/((?!login|register|onboarding|api|_next/static|_next/image|auth|favicon.ico|robots.txt|images|logo|$).*)',],
|
||||
matcher: ['/((?!onboarding|api|_next/static|_next/image|auth|favicon.ico|robots.txt|images|logo|$).*)',],
|
||||
missing: [
|
||||
{ type: 'header', key: 'next-router-prefetch' },
|
||||
{ type: 'header', key: 'purpose', value: 'prefetch' },
|
||||
|
||||
Reference in New Issue
Block a user