Skip to content

Commit

Permalink
fix: redirect user by type
Browse files Browse the repository at this point in the history
  • Loading branch information
yougyung committed Sep 12, 2024
1 parent 1a71c60 commit 7b75357
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,13 @@ async function getAuth(request: NextRequest): Promise<{
}

const allowedOnlyGuestPath = ['/sign-in', '/sign-up', '/find-password', '/find-id'];
const allowedGuestPath = ['/tutorial', ...allowedOnlyGuestPath];
const allowInitUserPath = ['/tutorial', '/grade-upload'];
const allowedGuestPath = ['/', '/tutorial', ...allowedOnlyGuestPath];
const allowInitUserPath = ['/', '/tutorial', '/grade-upload'];

function isAllowedGuestPath(path: string, strict: boolean = false) {
if (path === '/') {
return true;
}

const allowedPath = strict ? allowedOnlyGuestPath : allowedGuestPath;
return allowedPath.some((allowedPath) => path.startsWith(allowedPath));

return allowedPath.some((allowedPath) => path === allowedPath);
}

export async function middleware(request: NextRequest) {
Expand All @@ -46,17 +43,22 @@ export async function middleware(request: NextRequest) {
return await retryAuth(request);
}

if (auth.role === 'init' && !allowInitUserPath.some((path) => request.nextUrl.pathname.startsWith(path))) {
return Response.redirect(new URL('/grade-upload', request.url));
}

if (auth.role === 'guest' && !isAllowedGuestPath(request.nextUrl.pathname)) {
console.log(isAllowedGuestPath(request.nextUrl.pathname));
return Response.redirect(new URL('/sign-in', request.url));
}

if (auth.role !== 'guest' && isAllowedGuestPath(request.nextUrl.pathname, true)) {
return Response.redirect(new URL('/my', request.url));
}

if (auth.role === 'init' && !allowInitUserPath.some((path) => request.nextUrl.pathname === path)) {
return Response.redirect(new URL('/grade-upload', request.url));
}

if (auth.role === 'user' && request.nextUrl.pathname === '/') {
return Response.redirect(new URL('/my', request.url));
}
}

async function retryAuth(request: NextRequest) {
Expand Down

0 comments on commit 7b75357

Please sign in to comment.