-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
29 lines (24 loc) · 824 Bytes
/
Copy pathmiddleware.ts
File metadata and controls
29 lines (24 loc) · 824 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
import { getToken } from 'next-auth/jwt'
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
export async function middleware(request: NextRequest) {
const path = request.nextUrl.pathname
// Check if the path starts with /my
if (path.startsWith('/my')) {
const token = await getToken({
req: request,
secret: process.env.NEXTAUTH_SECRET,
})
// Redirect unauthenticated users to the login page
if (!token) {
const loginUrl = new URL('/auth', request.nextUrl.origin)
loginUrl.searchParams.set('callbackUrl', request.nextUrl.pathname)
return NextResponse.redirect(loginUrl)
}
}
return NextResponse.next()
}
// Configure the paths that should be handled by this middleware
export const config = {
matcher: '/my/:path*'
}