-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmiddleware.ts
More file actions
41 lines (29 loc) · 938 Bytes
/
Copy pathmiddleware.ts
File metadata and controls
41 lines (29 loc) · 938 Bytes
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
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
{/* export { default } from "next-auth/middleware" */ }
import { getToken } from 'next-auth/jwt'
// This function can be marked `async` if using `await` inside
export async function middleware(request: NextRequest) {
const token = await getToken({ req: request, secret: process.env.NEXTAUTH_SECRET });
const url = request.nextUrl
if (token && (
url.pathname.startsWith('/login') ||
url.pathname.startsWith('/sign-up')
)) {
return NextResponse.redirect(new URL('/dashboard', request.url))
}
if (!token && url.pathname.startsWith("/dashboard")) {
return NextResponse.redirect(new URL("/login", request.url));
}
return NextResponse.next();
}
// See "Matching Paths" below to learn more
export const config = {
matcher: [
"/",
"/sign-in",
"/sign-up",
"/dashboard/:path*",
"/otp"
],
}