-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
29 lines (24 loc) · 913 Bytes
/
middleware.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { NextRequest, NextResponse } from 'next/server';
import isAuthenticated from './lib/isAuthenticated';
export const config = {
matcher: ['/checkout', '/account', '/account/:path*'],
};
export function middleware(request: NextRequest) {
const isLoginPage = request.nextUrl.pathname === '/account/login';
const isRecoverPasswordPage =
request.nextUrl.pathname.startsWith('/account/recover');
const isResetPasswordPage =
request.nextUrl.pathname.startsWith('/account/reset');
const isRegisterPage = request.nextUrl.pathname === '/account/register';
const authPages =
isLoginPage ||
isRecoverPasswordPage ||
isRegisterPage ||
isResetPasswordPage;
if (authPages && isAuthenticated(request)) {
return NextResponse.redirect(new URL('/account', request.url));
}
if (!authPages && !isAuthenticated(request)) {
return NextResponse.redirect(new URL('/account/login', request.url));
}
}