generated from vercel/ai-chatbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.ts
50 lines (46 loc) · 1.14 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
import NextAuth, { type DefaultSession } from 'next-auth'
import GitHub from 'next-auth/providers/github'
import GoogleProvider from 'next-auth/providers/google'
import sha256 from 'crypto-js/sha256'
import Hex from 'crypto-js/enc-hex'
function reduceUserId(userId: string) {
const hash = BigInt('0x' + Hex.stringify(sha256(userId)))
const stringHash = (hash % BigInt('9007199254740992')).toString()
return stringHash
}
declare module 'next-auth' {
interface Session {
user: {
/** The user's id. */
id: string
} & DefaultSession['user']
}
}
export const {
handlers: { GET, POST },
auth,
CSRF_experimental // will be removed in future
} = NextAuth({
providers: [
GitHub,
GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET
})
],
callbacks: {
async jwt({ token, user, profile }) {
if (profile) {
token.id = reduceUserId(user.id)
token.image = profile.avatar_url || profile.picture
}
return token
},
authorized({ auth }) {
return !!auth?.user
}
},
pages: {
signIn: '/sign-in'
}
})