-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.ts
135 lines (127 loc) · 3.72 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
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
132
133
134
135
import { createUser, getUserByEmail, updateUserProfile } from "@/actions/users"
import NextAuth, {
getServerSession,
type Account,
type NextAuthOptions,
type Profile,
} from "next-auth"
import GoogleProvider from "next-auth/providers/google"
export const authOptions: NextAuthOptions = {
providers: [
GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID || "",
clientSecret: process.env.GOOGLE_CLIENT_SECRET || "",
async profile(tokens, profile) {
// console.log("tokens in google provider", tokens)
// console.log("profile in google provider", profile)
// console.log("profile in google provider", profile)
// get the user details
const email = tokens.email
let existingUser = await getUserByEmail(email)
// console.log("found user", existingUser)
if (!existingUser) {
console.log(
"User does not exist, creating new user in google provider profile"
)
// User does not exist, create new user
existingUser = await createUser({
email: email,
fullName: "",
picture: "",
})
}
return {
email: tokens.email,
name: tokens.name,
picture: tokens.picture,
id: tokens.sub,
customUser: existingUser,
...tokens,
...profile,
}
},
}),
],
session: {
strategy: "jwt" as const,
},
pages: {
signIn: "/auth/signin",
// signOut: "/auth/signout",
},
callbacks: {
async signIn({
account,
profile,
}: {
account: Account | null
profile?: Profile | any
}) {
// console.log("in signIn callback")
// console.log("account in signIn callback", account)
// console.log("profile in signIn callback", profile)
if (
account &&
account.provider === "google" &&
profile &&
profile.email
) {
const email = profile?.email
let existingUser = await getUserByEmail(email)
if (existingUser && profile) {
await updateUserProfile(email, {
picture: profile.picture ?? "",
name: profile.name || "",
})
}
if (!existingUser) {
// User does not exist, create new user
console.log(
"User does not exist, creating new user in signIn callback"
)
existingUser = await createUser({
email: email,
fullName: profile?.name || "",
picture: profile?.picture || "",
})
}
account.user = existingUser // Attach the user object to the account
}
return true
},
async session({ session, token }: { session: any; token: any }) {
// console.log("in session callback")
// console.log("session in session: ", session)
// console.log("token in session : ", token)
// Manually assign parameters
// session.accessToken = token.access_token
// session.user.id = token.id
session.expires = token.expires_at
session.user.customUser = token.customUser
// console.log("Final session : ", session)
return session
},
async jwt({
token,
user,
}: {
token: any
profile?: any
user: any
account?: Account | null
trigger?: "signIn" | "signUp" | "update"
isNewUser?: boolean
session?: any
}) {
// console.log("in jwt callback")
// console.log("token in jwt", token)
// console.log("user in jwt", user)
return { ...token, ...user }
},
},
}
export default NextAuth(authOptions)
export const getSession = async () => {
const session = await getServerSession(authOptions)
return session
}