-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauth.js
89 lines (79 loc) · 2.62 KB
/
auth.js
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
import React from 'react';
import Credentials from "next-auth/providers/credentials";
import NextAuth from 'next-auth';
import mysql from 'mysql2/promise';
import { GetDBSettings } from '@/sharedCode/common';
import { toast } from '@/hooks/use-toast';
import bcrypt from 'bcryptjs';
let connectionparams = GetDBSettings();
export const { handlers, signIn, signOut, auth } = NextAuth({
providers: [
Credentials({
credentials: {
email: { label: "Email", type: "email", placeholder: "Email" },
password: { label: "Password", type: "password", placeholder: "Password" },
},
async authorize(credentials) {
let currentuser = null;
const connection = await mysql.createConnection(connectionparams);
const [rows] = await connection.execute(
'CALL GetUserByEmail(?)',
[credentials.email])
connection.end();
currentuser = rows[0][0];
console.log("current user",rows)
if (!currentuser) {
console.log("This Email is Already Exists");
toast({
title: "You submitted the following values:",
description: (
<pre className="mt-2 w-[340px] rounded-md bg-slate-950 p-4 text-white">
<p> User Credentials Invalid</p>
</pre>
),
});
return null;
}
const isPasswordValid = await bcrypt.compare(credentials.password, currentuser.password);
if (!isPasswordValid) {
console.log("Invalid credentials - Incorrect password");
toast({
title: "You submitted the following values:",
description: (
<pre className="mt-2 w-[340px] rounded-md bg-slate-950 p-4 text-white">
<p>Invalid password</p>
</pre>
),
});
return null;
}
// Return the user object, mapping `userid` to `id`
console.log("current: ",currentuser)
return {
id: currentuser.userId,
email: currentuser.email,
name: currentuser.name,
};
}
})
],
callbacks: {
async jwt({ token, user }) {
if (user) {
token.id = user.id; // Assign the user id (userid)
token.email = user.email;
token.name = user.name;
}
return token;
},
async session({ session, token }) {
session.user.id = token.id; // Pass the user id (userid) in the session
session.user.email = token.email;
session.user.name = token.name;
return session;
}
},
pages: {
signIn: '/auth/signin',
},
});