Add Google provider to nextjauth

This commit is contained in:
Cédric FAMIBELLE-PRONZOLA
2021-06-10 19:22:46 +02:00
parent 647da8fb30
commit 6d0c8ec86e
2 changed files with 13 additions and 3 deletions
+1
View File
@@ -56,6 +56,7 @@
{ {
"capIsNewExceptions": [ "capIsNewExceptions": [
"Credentials", "Credentials",
"Google",
"NextAuth" "NextAuth"
] ]
} }
+12 -3
View File
@@ -26,6 +26,10 @@ const options = {
throw new Error(errorMessage) throw new Error(errorMessage)
} }
} }
}),
Providers.Google({
clientId: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET
}) })
], ],
database: process.env.NEXT_PUBLIC_DATABASE_URL, database: process.env.NEXT_PUBLIC_DATABASE_URL,
@@ -33,10 +37,15 @@ const options = {
jwt: true jwt: true
}, },
callbacks: { callbacks: {
jwt: async (token, user) => { jwt: async (token, user, account) => {
if (user) { if (user) {
token.jwt = user.jwt const response = await fetch(
token.user = user.user `${process.env.NEXT_PUBLIC_API_URL}/auth/${account.provider}/callback?access_token=${account?.accessToken}`
)
const data = await response.json()
token.id = data.id || user.id
token.jwt = data.jwt || user.jwt
token.user = data.user || user.user
} }
return Promise.resolve(token) return Promise.resolve(token)