-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
48 lines (40 loc) · 1.44 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
46
47
48
import { NextResponse } from 'next/server';
import { NextRequest } from 'next/server';
export function middleware(req: NextRequest) {
const path = req.nextUrl.pathname;
const isPublicPath = path === '/sign-up' || path === '/sign-in';
const token = req.cookies.get('token')?.value || '';
// Check if the user is trying to access a public path while authenticated
if (isPublicPath && token) {
return NextResponse.redirect(new URL('/', req.nextUrl));
}
// Check if the user is trying to access a protected path while not authenticated
if (!isPublicPath && !token) {
return NextResponse.redirect(new URL('/sign-in', req.nextUrl));
}
// // Check if the user is trying to access '/onboarding' while already onboarded
// const isOnboardingPath = path === '/onboarding';
// const isUserOnboarded = /* Add your logic to check if the user is onboarded */;
// if (isOnboardingPath && isUserOnboarded) {
// return NextResponse.redirect(new URL('/', req.nextUrl));
// }
}
export const config = {
matcher: [
'/',
'/sign-up',
'/sign-in',
'/onboarding',
'/profile/:userId*',
'/search',
'/activity/:userId*',
'/edit-profile/:userId*',
'/followers/:userId*',
'/followings/:userId*',
'/activity',
'/likes',
'/thread/:threadId*',
'/thread/create',
'/settings/:userId*',
],
};