Skip to content

Commit

Permalink
refactor : Conditionally return login route for other users and admin
Browse files Browse the repository at this point in the history
  • Loading branch information
ad956 committed Jul 27, 2024
1 parent ee5aa0e commit 1d20dc1
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions middlewares/handlePrivateRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ export default async function handlePrivateRoute(
request: NextRequest,
token: string | undefined
) {
const currentRole = request.nextUrl.pathname.split("/")[1];

if (!token) {
return NextResponse.redirect(new URL("/login", request.url));
let loginUrl = currentRole === "admin" ? "/admin-login" : "/login";

return NextResponse.redirect(new URL(loginUrl, request.url));
}

const decryptedToken = await decrypt(token);
const userRole = decryptedToken.user.role;
const currentRole = request.nextUrl.pathname.split("/")[1];

if (currentRole !== userRole) {
return NextResponse.redirect(new URL(`/${userRole}`, request.url));
Expand Down

0 comments on commit 1d20dc1

Please sign in to comment.