-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
45 lines (35 loc) · 1.36 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
44
45
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})
const url = request.nextUrl;
if(token && (
url.pathname.startsWith('/signin') ||
url.pathname.startsWith('/signup') ||
url.pathname.startsWith('/')
)){
return NextResponse.redirect(new URL('/dashboard', request.url))
}
}
// See "Matching Paths" below to learn more
export const config = {
matcher: ['/dashbord/:path*']
}
// import { NextResponse } from 'next/server';
// import type { NextRequest } from 'next/server';
// // Middleware function to protect the dashboard route
// export function middleware(req: NextRequest) {
// // Get the token from cookies (assuming JWT is stored in cookies)
// const token = req.cookies.get('token')?.value;
// // If there's no token, redirect to login page
// if (!token) {
// return NextResponse.redirect(new URL('/signin', req.url));
// }
// // If token exists, allow the user to access the dashboard
// return NextResponse.next();
// }
// // Define which routes the middleware applies to
// export const config = { matcher: ["/dashboard"] }