From 6d0c8ec86eab99464d43c16f1bcaf64f3fc1b24a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20FAMIBELLE-PRONZOLA?= Date: Thu, 10 Jun 2021 19:22:46 +0200 Subject: [PATCH] Add Google provider to nextjauth --- package.json | 1 + pages/api/auth/[...nextauth].js | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 239f0fc..741d808 100644 --- a/package.json +++ b/package.json @@ -56,6 +56,7 @@ { "capIsNewExceptions": [ "Credentials", + "Google", "NextAuth" ] } diff --git a/pages/api/auth/[...nextauth].js b/pages/api/auth/[...nextauth].js index 5d73926..162b98f 100644 --- a/pages/api/auth/[...nextauth].js +++ b/pages/api/auth/[...nextauth].js @@ -26,6 +26,10 @@ const options = { 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, @@ -33,10 +37,15 @@ const options = { jwt: true }, callbacks: { - jwt: async (token, user) => { + jwt: async (token, user, account) => { if (user) { - token.jwt = user.jwt - token.user = user.user + const response = await fetch( + `${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)