-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.ts
More file actions
131 lines (116 loc) · 3.76 KB
/
Copy pathauth.ts
File metadata and controls
131 lines (116 loc) · 3.76 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
import NextAuth from "next-auth"
import Credentials from "next-auth/providers/credentials"
import { PrismaClient } from "./lib/generated/prisma";
import { JWT } from "next-auth/jwt"
declare module "next-auth" {
interface Session {
user: {
id: string
email: string
name: string
role: "student" | "placement-cell" | "employer" | "faculty"
}
}
interface User {
id: string
role: "student" | "placement-cell" | "employer" | "faculty"
}
}
declare module "next-auth/jwt" {
interface JWT {
id: string
role: "student" | "placement-cell" | "employer" | "faculty"
}
}
const prisma = new PrismaClient()
export const { handlers, signIn, signOut, auth } = NextAuth({
providers: [
Credentials({
credentials: {
email: {
type: "email",
label: "Email",
placeholder: "Enter your email",
},
password: {
type: "password",
label: "Password",
placeholder: "Enter password",
},
role: {
type: "text",
label: "Role",
},
},
authorize: async (credentials) => {
console.log("Authorizing user with credentials:", credentials);
if (credentials?.role === "student") {
const user = await prisma.student.findUnique({
where: { email: credentials.email as string },
});
if (user && user.password === credentials?.password) {
return { id: user.id, email: user.email, name: user.name, role: "student" };
}
}
if (credentials?.role === "faculty") {
const user = await prisma.faculty.findUnique({
where: { email: credentials.email as string },
});
if (user && user.password === credentials?.password) {
return { id: user.id, email: user.email, name: user.name, role: "faculty" };
}
}
if (credentials?.role === "placement-cell") {
const user = await prisma.placmentCell.findUnique({
where: { email: credentials.email as string },
});
if (user && user.password === credentials?.password) {
return { id: user.id, email: user.email, name: user.name, role: "placement-cell" };
}
}
if (credentials?.role === "employer") {
const user = await prisma.employer.findUnique({
where: { email: credentials.email as string },
});
if (user && user.password === credentials?.password) {
return { id: user.id, email: user.email, name: user.name, role: "employer" };
}
}
return null;
}
})
],
callbacks: {
session({ session, token, user }) {
// console.log("Session callback:", { session, token, user });
return {
...session,
user: {
id: token.id as string,
email: session.user?.email || "",
name: session.user?.name || "",
role: token.role as "student" | "placement-cell" | "employer" | "faculty",
}
}
},
jwt({ token, user }) {
if (user) {
token.id = user.id;
token.role = user.role;
}
return token;
},
},
session: {
strategy: "jwt",
maxAge: 20 * 60, // 20 minutes
},
pages: {
signIn: '/sign-in',
error: '/error'
},
secret: process.env.AUTH_SECRET,
jwt: {
maxAge: 20 * 60
}
})