From b6a4d1e94ac212fad1329ed1fbd1785cae8619f0 Mon Sep 17 00:00:00 2001 From: RichardJECooke Date: Thu, 17 Oct 2024 16:20:17 +0200 Subject: [PATCH 1/2] Explain how to get email so we can finally fix this bug of 7 years - https://github.com/jaredhanson/passport-oauth2/issues/73 --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 54b19da..01fc863 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,9 @@ passport.use(new OAuth2Strategy({ clientSecret: EXAMPLE_CLIENT_SECRET, callbackURL: "http://localhost:3000/auth/example/callback" }, - function(accessToken, refreshToken, profile, cb) { + function(accessToken, refreshToken, params, profile, cb) { + const token = jwt_decode(accessToken); // user id lives in here + const profileInfo = jwt_decode(params.id_token)); // user email lives in here User.findOrCreate({ exampleId: profile.id }, function (err, user) { return cb(err, user); }); From 1396323244811afe71e655603bed85395a32a2c6 Mon Sep 17 00:00:00 2001 From: RichardJECooke Date: Thu, 17 Oct 2024 16:51:16 +0200 Subject: [PATCH 2/2] Add email to scopes so it's return from provider --- README.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 01fc863..f537f79 100644 --- a/README.md +++ b/README.md @@ -61,11 +61,13 @@ passport.use(new OAuth2Strategy({ tokenURL: 'https://www.example.com/oauth2/token', clientID: EXAMPLE_CLIENT_ID, clientSecret: EXAMPLE_CLIENT_SECRET, - callbackURL: "http://localhost:3000/auth/example/callback" + callbackURL: "http://localhost:3000/auth/example/callback", + scope: "openid email profile offline_access", }, function(accessToken, refreshToken, params, profile, cb) { - const token = jwt_decode(accessToken); // user id lives in here - const profileInfo = jwt_decode(params.id_token)); // user email lives in here + const token = jwt_decode(accessToken); + const email = jwt_decode(params.id_token).email; + const user = { ...token, email }; User.findOrCreate({ exampleId: profile.id }, function (err, user) { return cb(err, user); });