-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.js
More file actions
31 lines (28 loc) · 818 Bytes
/
Copy pathmiddleware.js
File metadata and controls
31 lines (28 loc) · 818 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
import { NextResponse } from 'next/server'
import { getTokenPayload } from '@/utils'
import { AUTH_TOKEN } from '@/constants'
export async function middleware (req) {
const token = req.cookies && req.cookies.get(AUTH_TOKEN) ? req.cookies.get(AUTH_TOKEN)?.value : null
if (!token) {
return NextResponse.redirect(new URL('/auth/login', req.url))
}
// Check if token is valid
try {
const {payload} = await getTokenPayload(token)
req.user = payload
const response = NextResponse.next()
return response
} catch (error) {
return NextResponse.redirect(new URL('/auth/login', req.url))
}
}
export const config = {
matcher: [
// '/((?!api|_next/static|_next/image|favicon.ico).*)',
'/want-to-read',
'/read',
'/reading',
'/book/new',
'/book/edit/:bookId*',
],
}