-
Notifications
You must be signed in to change notification settings - Fork 1
/
middleware.ts
47 lines (37 loc) · 1.02 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
40
41
42
43
import { getToken } from 'next-auth/jwt';
import { withAuth } from 'next-auth/middleware';
import { NextFetchEvent, NextRequest, NextResponse } from 'next/server';
export default async function middleware(req: NextRequest, event: NextFetchEvent) {
const token = await getToken({ req });
const isAuthenticated = !!token;
if (["/login", "/register", "/"].includes(req.nextUrl.pathname)) {
if (isAuthenticated) {
return NextResponse.redirect(new URL('/home', req.url));
}
return NextResponse.next();
}
const authMiddleware = await withAuth({
pages: {
signIn: `/login`,
},
});
// @ts-expect-error
return authMiddleware(req, event);
}
export const config = {
matcher: [
"/",
"/login",
"/register",
"/home",
"/index",
"/library",
"/providers",
"/queue",
"/search",
"/artist/:path*",
"/genre/:path*",
"/playlist/:path*",
"/account",
]
}