-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.ts
76 lines (68 loc) · 2.07 KB
/
auth.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
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
import NextAuth from "next-auth";
import { authConfig } from './auth.config';
import {z} from "zod"
import CredentialsProvider from "@auth/core/providers/credentials";
import getUser from "./db/queries/users/getUser";
import { compare } from "bcrypt";
export const {handlers:{GET,POST},auth,signIn,signOut}=NextAuth({
secret: process.env.AUTH_SECRET,
providers: [
CredentialsProvider({
credentials: {
email: {
label: 'email',
type: 'email',
placeholder: 'grafbase',
},
password: { label: 'Password', type: 'password' },
},
async authorize(credentials, req) {
const { email, password } = credentials as {
email: string
password: string
}
// Add logic here to look up the user from the credentials supplied
const user=await getUser(credentials?.email)
if(credentials.email===user?.email){
const isValid = await compare(password, user?.password || "")
if (!isValid) {
throw new Error('Wrong credentials. Try again.')
}
return {id:user?.id,email:user?.email,name:user?.name,phone:user?.phone}
}
return null;
}
}),
],
callbacks:{
async jwt({token,user,session}){
//console.log("jwt callback",{token,user,session});
if(user){
return {
...token,
id:user.id,
phone:user.phone,
profile:user.profile,
email:user.email,
name:user.name
}
}
return token;
},
async session({session,token,user}){
//console.log("session callback",{session,token,user});
return {
...session,
user:{
...session.user,
id:token.id,
phone:token.phone,
}
}
return session
}
},
session:{
strategy:"jwt",
}
})