-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.ts
More file actions
101 lines (93 loc) · 3.48 KB
/
handler.ts
File metadata and controls
101 lines (93 loc) · 3.48 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
import CredentialsProvider from "next-auth/providers/credentials";
import { NextAuthOptions } from "next-auth";
import { JWT } from "next-auth/jwt";
import { User } from "next-auth"; // Extended User model with role
import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
export const NEXT_AUTH_HANDLER: NextAuthOptions = {
providers: [
CredentialsProvider({
name: "Credentials",
credentials: {
email: { label: "Email", type: "email", placeholder: "Email" },
password: { label: "Password", type: "password", placeholder: "Password" },
userType: { label: "User Type", type: "text", placeholder: "User or Admin" },
},
async authorize(credentials) {
if (!credentials) return null;
// Check if user exists in the database
const user = await prisma.user.findFirst({
where: { email: credentials.email },
include: {
Store: true, // Fetch the associated Store
PrintJob: true, // Fetch the associated PrintJobs
},
});
if (!user) return null; // User does not exist
// Check if password matches
if (user.password !== credentials.password) {
return null; // Incorrect password
}
// Return user data after successful authentication along with Store and PrintJobs
return {
id: user.id.toString(),
name: user.name,
email: user.email,
phone: user.phone,
role: user.role, // Store user role (e.g., 'admin' or 'user')
password: user.password,
confirmpassword: user.confirmpassword,
store: user.Store, // Add Store data
printJobs: user.PrintJob, // Add PrintJobs data
};
},
}),
],
pages: {
signIn: "/signin", // Custom sign-in page path
},
session: {
strategy: "jwt",
maxAge: 30 * 24 * 60 * 60, // 30 days
updateAge: 24 * 60 * 60, // Update after 1 day
},
jwt: {
maxAge: 30 * 24 * 60 * 60, // Token expiration time (30 days)
},
callbacks: {
async jwt({ token, user }: { token: JWT; user?: User }) {
if (user) {
// Log the user data in the JWT callback
console.log("User data in JWT callback:", user);
// Store all user data in the JWT
token.role = user.role;
token.email = user.email;
token.name = user.name;
token.phone = user.phone;
token.password = user.password;
token.confirmpassword = user.confirmpassword;
token.id = user.id;
token.store = user.store; // Store data added to JWT
token.printJobs = user.printJobs; // PrintJobs data added to JWT
}
return token;
},
async session({ session, token }: { session: any; token: JWT }) {
// Log the token data in the session callback
console.log("Token data in Session callback:", token);
if (session.user) {
// Add token data to session
session.user.email = token.email;
session.user.role = token.role;
session.user.name = token.name;
session.user.phone = token.phone;
session.user.password = token.password;
session.user.confirmpassword = token.confirmpassword;
session.user.id = token.id;
session.user.store = token.store; // Store data added to session
session.user.printJobs = token.printJobs; // PrintJobs data added to session
}
return session;
},
},
};