-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
50 lines (46 loc) · 1.13 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
import { withAuth } from "next-auth/middleware";
import { NextResponse } from "next/server";
export default withAuth(
function middleware(req) {
const token = req.nextauth.token;
const path = req.nextUrl.pathname;
// Unauthenticated
if (!token) return NextResponse.redirect(new URL("/login", req.url));
// Authenticated
const userEmail = token.email;
if (userEmail?.split("@")[1] === "student.chula.ac.th") {
// As Student
if (path.startsWith("/jobs")) {
return NextResponse.redirect(new URL("/landing", req.url));
}
} else {
// As Employer
if (
path.startsWith("/search") ||
path.startsWith("/offer") ||
path.startsWith("/studentjobs")
) {
return NextResponse.redirect(new URL("/landing", req.url));
}
}
},
{
callbacks: {
authorized: ({ token }) => !!token,
},
pages: {
signIn: "/login",
},
}
);
export const config = {
matcher: [
"/search/:path*",
"/jobs/:path*",
"/offer/:path*",
"/studentjobs/:path*",
"/payment-history",
"/chat/:path*",
"/profile/:path*",
],
};