-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
132 lines (110 loc) · 4.63 KB
/
Copy pathmiddleware.ts
File metadata and controls
132 lines (110 loc) · 4.63 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//TODO: (1) remove TeamCode page bcoz. it is same as joinTeam page (functionality should be retained)
//TODO: (2) add User interface to the types folder for reusability
import { getToken } from "next-auth/jwt";
import { NextRequest, NextResponse } from "next/server";
export { default } from "next-auth/middleware"
interface User {
id: string;
email: string;
name: string;
hasFilledDetails?: boolean;
events?: number[];
event1TeamRole?: number;
event2TeamRole?: number;
};
export async function middleware(request: NextRequest) {
const token = await getToken({ req: request });
const path = request.nextUrl.pathname;
const user : User|null = token?.user as User;
console.log(user);
if (!token && !(path === "/")) {
return NextResponse.redirect(new URL('/', request.url))
}
//? Check if user is trying to go to an event page without signin
if (!user && path.startsWith("/events")) {
return NextResponse.redirect(new URL('/', request.url))
}
//? Check if user is trying to fill user details again
//? If yes, redirect to root route
if (user && user.hasFilledDetails && (
path.startsWith('/userDetails') ||
path.startsWith('/events/pioneira/detailsForm')
)) {
return NextResponse.redirect(new URL('/', request.url))
}
if (user && !user.hasFilledDetails && !user.email.endsWith("@vitstudent.ac.in") && path.startsWith('/userDetails')) {
return NextResponse.redirect(new URL('/events/pioneira/detailsForm', request.url))
}
if (user) {
const match = path.match(/\/events\/event(\d+)\b/);
if (match) {
const eventNumber = parseInt(match[1], 10);
//? Check for a valid event number
//? If not redirect to root route
if (eventNumber < 1 || eventNumber > 5) {
return NextResponse.redirect(new URL('/', request.url))
}
//? Check if the user is a participant for the event
//? If not redirect to root route
if (!user.events || !user.events.includes(eventNumber)) {
return NextResponse.redirect(new URL('/', request.url))
}
console.log(user.event1TeamRole);
//? Check if it is event1
if (eventNumber === 1) {
//? Check if the user has an event1TeamRole. i.e, if the user is part of a team or not
//? If not, and the page is not joinTeam or createTeam page
//? Redirect to createTeam page
if (
path.startsWith('/events/event1/createTeam') ||
path.startsWith('/events/event1/joinTeam') ||
path.startsWith('/events/event1/userConsent')
) {
if (user.event1TeamRole === 0) {
return NextResponse.redirect(new URL('/events/event1/leaderDashboard', request.url))
} else if (user.event1TeamRole === 1) {
return NextResponse.redirect(new URL('/events/event1/memberDashboard', request.url))
} else {
return NextResponse.redirect(new URL('/', request.url))
}
}
//? Check if the user has an event1TeamRole. i.e, if the user is part of a team or not
//? If yes and the page is joinTeam or createTeam
//? Redirect to the Dashboard
if (user.event1TeamRole !== undefined && user.event1TeamRole !== null && (
path.startsWith('/events/event1/joinTeam') ||
path.startsWith('/events/event1/createTeam')
)) {
//? If teamRole == 0 (leader)
if (user.event1TeamRole === 0) {
return NextResponse.redirect(new URL('/events/event1/leaderDashboard', request.url))
}
//? If teamRole == 1 (member)
if (user.event1TeamRole === 1) {
return NextResponse.redirect(new URL('/events/event1/memberDashboard', request.url))
}
}
//? Check if leader is trying to access the member dashboard
if (user.event1TeamRole === 0 && path.startsWith('/events/event1/memberDashboard')) {
return NextResponse.redirect(new URL('/events/event1/leaderDashboard', request.url))
}
//? Check if member is trying to access the leader dashboard
if (user.event1TeamRole === 1 && path.startsWith('/events/event1/leaderDashboard')) {
return NextResponse.redirect(new URL('/events/event1/memberDashboard', request.url))
}
//? Check if a non-leader is trying to access rounds
if (user.event1TeamRole !== 0 && path.startsWith('/events/event1/round')) {
return NextResponse.redirect(new URL('/events/event1/memberDashboard', request.url))
}
}
} else {
console.log("No event number found in the pathname.");
}
}
}
export const config = {
matcher: [
'/userDetails',
'/events/:path*'
]
}