47 lines
1.1 KiB
TypeScript
47 lines
1.1 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import type { NextRequest } from 'next/server';
|
|
|
|
/** Routes that must work without an existing session (first-time invitees). */
|
|
const publicRoutes = [
|
|
'/',
|
|
'/login',
|
|
'/register',
|
|
'/terms',
|
|
'/privacy',
|
|
'/forgot-password',
|
|
'/accept-invite',
|
|
'/accept-organization-invite',
|
|
];
|
|
|
|
export function proxy(request: NextRequest) {
|
|
const { pathname } = request.nextUrl;
|
|
const token = request.cookies.get('accessToken')?.value;
|
|
const isAuthenticated = !!token;
|
|
|
|
if (isAuthenticated && pathname === '/') {
|
|
return NextResponse.redirect(new URL('/today', request.url));
|
|
}
|
|
|
|
if (publicRoutes.includes(pathname)) {
|
|
return NextResponse.next();
|
|
}
|
|
|
|
if (!isAuthenticated) {
|
|
if (pathname === '/login') {
|
|
return NextResponse.next();
|
|
}
|
|
|
|
const loginUrl = new URL('/login', request.url);
|
|
loginUrl.searchParams.set('from', pathname);
|
|
return NextResponse.redirect(loginUrl);
|
|
}
|
|
|
|
return NextResponse.next();
|
|
}
|
|
|
|
export const config = {
|
|
matcher: [
|
|
'/((?!_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)',
|
|
],
|
|
};
|