This repository was archived by the owner on Jun 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathauth.ts
More file actions
116 lines (101 loc) · 3.03 KB
/
auth.ts
File metadata and controls
116 lines (101 loc) · 3.03 KB
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
import NextAuth from 'next-auth'
import type { NextAuthConfig, User } from 'next-auth'
import Google from 'next-auth/providers/google'
import GitHub from 'next-auth/providers/github'
import { initializeApp } from 'firebase/app'
import {
getAuth,
GithubAuthProvider,
GoogleAuthProvider,
signInWithCredential,
} from 'firebase/auth'
import { routes } from '@/constants/routes'
declare module 'next-auth' {
interface Session {
user: {
uid?: string
creationTime?: string
providerId?: string
accessToken?: string
refreshToken?: string
} & Omit<User, 'id'>
}
}
export const firebaseConfig = {
apiKey: process.env.FB_AUTH_API_KEY,
authDomain: process.env.FB_AUTH_AUTH_DOMAIN,
projectId: process.env.FB_AUTH_PROJECT_ID,
storageBucket: process.env.FB_AUTH_STORAGE_BUCKET,
messagingSenderId: process.env.FB_AUTH_MESSAGING_SENDER_ID,
appId: process.env.FB_AUTH_APP_ID,
measurementId: process.env.FB_AUTH_MEASUREMENT_ID,
}
const firebaseApp = initializeApp(firebaseConfig)
const firebaseAuth = getAuth(firebaseApp)
const authConfig = {
providers: [Google, GitHub],
callbacks: {
async signIn({ account }) {
try {
if (account) {
const {
provider,
id_token: idToken,
access_token: accessToken,
} = account
if (provider === 'google') {
const credential = GoogleAuthProvider.credential(idToken)
const userCredential = await signInWithCredential(
firebaseAuth,
credential,
)
return !!userCredential
}
if (provider === 'github' && accessToken) {
const credential = GithubAuthProvider.credential(accessToken)
const userCredential = await signInWithCredential(
firebaseAuth,
credential,
)
return !!userCredential
}
}
return false
} catch (e) {
return false
}
},
async jwt({ account, token }) {
if (account) {
const auth = getAuth()
const user = auth.currentUser
if (user) {
const { providerData, metadata, uid, refreshToken } = user
const { creationTime } = metadata
const { providerId } = providerData[0]
token.uid = uid
token.creationTime = creationTime
token.providerId = providerId
token.accessToken = account.access_token
token.refreshToken = refreshToken
}
}
return token
},
async session({ session, token }) {
session.user.uid = token.uid as string
session.user.creationTime = token.creationTime as string
session.user.providerId = token.providerId as string
session.user.accessToken = token.accessToken as string
session.user.refreshToken = token.refreshToken as string
return session
},
},
pages: {
error: routes.landingError,
},
} satisfies NextAuthConfig
export const {
handlers: { GET, POST },
auth,
} = NextAuth(authConfig)