-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
39 lines (32 loc) · 1.07 KB
/
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
30
31
32
33
34
35
36
37
38
39
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
import { getToken } from 'next-auth/jwt';
export { default } from 'next-auth/middleware';
export async function middleware(request: NextRequest) {
const token = await getToken({ req: request, secret: process.env.NEXTAUTH_SECRET });
const url = request.nextUrl;
const isAuthPage = url.pathname.startsWith('/sign-in') || url.pathname.startsWith('/sign-up') || url.pathname.startsWith('/verify') || url.pathname.startsWith('/landing');
const isProtectedPage = !isAuthPage;
if (token && isAuthPage) {
return NextResponse.redirect(new URL('/', request.url));
}
if (!token && isProtectedPage) {
return NextResponse.redirect(new URL('/landing', request.url));
}
return NextResponse.next();
}
export const config = {
matcher: [
'/sign-in',
'/sign-up',
'/verify',
'/',
'/throwbackTunes',
'/profile',
'/globalhits',
'/partystarters',
'/loveballads',
'/favourite',
'/landing'
],
};