Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions src/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,36 @@
import { NextRequest, NextResponse } from 'next/server';
import { cookies } from 'next/headers';

const AFTER_LOGIN_DOMAIN = ['/mydashboard', '/dashboard/:path*', '/dashboard', '/mypage'] satisfies readonly string[];
const AFTER_LOGIN_DOMAIN = ['/mydashboard', '/dashboard', '/mypage'] satisfies readonly string[];
const BEFORE_LOGIN_DOMAIN = ['/faq', '/privacy', '/login', '/signup', '/'] satisfies readonly string[];

export const middleware = async (request: NextRequest) => {
const cookieStore = await cookies();
const accessToken = cookieStore.get('accessToken');

const pathname = request.nextUrl.pathname;

if (!accessToken?.value) {
if (AFTER_LOGIN_DOMAIN.includes(request.nextUrl.pathname)) return NextResponse.redirect(new URL('/login', request.url));
if (AFTER_LOGIN_DOMAIN.some((path) => pathname.startsWith(path))) {
return NextResponse.redirect(new URL('/login', request.url));
}
} else {
if (BEFORE_LOGIN_DOMAIN.includes(request.nextUrl.pathname)) return NextResponse.redirect(new URL('/mydashboard', request.url));
if (BEFORE_LOGIN_DOMAIN.includes(pathname)) {
return NextResponse.redirect(new URL('/mydashboard', request.url));
}
}

return NextResponse.next();
};

export const config = {
matcher: '/:path*',
matcher: [
{
source: '/:path*',
missing: [
{ type: 'header', key: 'next-router-prefetch' },
{ type: 'header', key: 'purpose', value: 'prefetch' },
],
},
],
};