-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
75 lines (71 loc) · 2.91 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { NextRequest, NextResponse } from 'next/server';
import { isLoggedIn } from '@/app/lib/session';
// import { currentUser } from '@/app/lib/models/user';
const middleware = async (request: NextRequest) => {
/* // const requestHeaders = new Headers(request.headers);
// requestHeaders.set('ngrok-skip-browser-warning', 'true');
request.headers.set('ngrok-skip-browser-warning', 'true');
return NextResponse.next({
// request: { headers: requestHeaders },
request,
}); */
// response.cookies.set('ngrok-skip-browser-warning', 'true');
const loggedIn = await isLoggedIn();
if (loggedIn) {
if (request.nextUrl.pathname.startsWith('/creators')) {
return NextResponse.redirect(
new URL(`/user${request.nextUrl.pathname}`, process.env.NEXT_PUBLIC_BASE_URL),
);
}
} else {
if (!request.nextUrl.pathname.startsWith('/creators')) {
return NextResponse.redirect(new URL('/auth/login', process.env.NEXT_PUBLIC_BASE_URL));
}
}
/* if (loggedIn) {
const user = await currentUser();
if (!user) {
return NextResponse.redirect(`${process.env.NEXT_PUBLIC_BASE_URL}/auth/login`);
}
if (request.nextUrl.pathname === '/auth/login') {
return NextResponse.redirect(
user.creatorAccount
? `${process.env.NEXT_PUBLIC_BASE_URL}/creator`
: `${process.env.NEXT_PUBLIC_BASE_URL}/user`,
);
} else if (request.nextUrl.pathname === '/new-creator') {
if (user.creatorAccount) {
return NextResponse.redirect(`${process.env.NEXT_PUBLIC_BASE_URL}/creator`);
}
} else if (request.nextUrl.pathname === '/new-user') {
if (user.userAccount) {
return NextResponse.redirect(`${process.env.NEXT_PUBLIC_BASE_URL}/user`);
}
} else if (request.nextUrl.pathname.startsWith('/creator')) {
if (!user.creatorAccount && user.userAccount) {
return NextResponse.redirect(`${process.env.NEXT_PUBLIC_BASE_URL}/user`);
}
if (!user.userAccount && !user.creatorAccount) {
return NextResponse.redirect(`${process.env.NEXT_PUBLIC_BASE_URL}/new-creator`);
}
} else if (request.nextUrl.pathname.startsWith('/user')) {
if (!user.userAccount && user.creatorAccount) {
return NextResponse.redirect(`${process.env.NEXT_PUBLIC_BASE_URL}/creator`);
}
if (!user.creatorAccount && !user.userAccount) {
return NextResponse.redirect(`${process.env.NEXT_PUBLIC_BASE_URL}/new-user`);
}
}
} else {
if (request.nextUrl.pathname !== '/auth/login') {
return NextResponse.redirect(`${process.env.NEXT_PUBLIC_BASE_URL}/auth/login`);
}
} */
return NextResponse.next();
};
export const config = {
// matcher: ['/((?!api|_next/static|_next/image|favicon.ico).*)'],
matcher: ['/creators/:path*', '/creator/:path*', '/user/:path*'],
// matcher: ['/auth/login', '/new-creator', '/new-user', '/creator/:path*', '/user/:path*'],
};
export default middleware;