-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.ts
57 lines (51 loc) · 1.61 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
/* eslint-disable @typescript-eslint/no-unused-vars */
import NextAuth from "next-auth"
import GitHub from "next-auth/providers/github"
import { client } from "./sanity/lib/client"
import { USER_BY_GITHUB_ID } from "./sanity/lib/queries"
import { sanityWrite } from "./sanity/lib/client-write"
export const { handlers, auth, signIn, signOut } = NextAuth({
providers: [GitHub],
callbacks: {
async signIn({ user, profile }) {
const { name, email, image } = user;
const { id, login, bio } = profile;
try {
// Check if the user exists in your Sanity database
const exists = await client.withConfig({useCdn:false}).fetch(USER_BY_GITHUB_ID, { id });
if (!exists) {
await sanityWrite.create({
_type: "author",
id,
name,
email,
image,
username: login,
bio: bio || ''
})
console.log('user created');
} else {
console.log("User already exists:", exists);
}
return true;
} catch (error) {
console.error("Error during sign-in:", error);
return false; // Reject sign-in
}
},
async jwt({ token , profile, account }) {
if (account && profile) {
const user = await client.withConfig({useCdn: false}).fetch(USER_BY_GITHUB_ID, { id: profile.id })
console.log(user);
token.id = user?._id
}
return token; // Always return the token
},
async session({token, session}){
Object.assign(session, {
id : token?.id
})
return session
}
}
})