-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.js
More file actions
35 lines (24 loc) · 866 Bytes
/
Copy pathmiddleware.js
File metadata and controls
35 lines (24 loc) · 866 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
import { NextRequest , NextResponse } from 'next/server';
import { getToken } from 'next-auth/jwt';
export async function middleware(req) {
const session = await getToken({ req });
const isAuth = !!session;
const isAuthPage = req.nextUrl.pathname.startsWith('/login') || req.nextUrl.pathname.startsWith('/register');
if (isAuthPage && isAuth) {
return NextResponse.redirect('/profile');
}
// If the user is authenticated and they're not trying to access an auth page, let them proceed
if (isAuth && !isAuthPage) {
return NextResponse.next();
}
// If the user is not authenticated and they're trying to access an auth page, let them proceed
if (!isAuth && isAuthPage) {
return NextResponse.next();
}
// else{
// return NextResponse.redirect('/');
// }
}
export const config = {
matcher: ['/profile/:path*'],
};