-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(auth, config): review oAuth for google sign in ✨
- Loading branch information
1 parent
dd12686
commit 4446da4
Showing
12 changed files
with
140 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,12 @@ module.exports = { | |
googleAnalyticsTrackingID: 'WAOS_NODE_app_googleAnalyticsTrackingID', | ||
contact: '[email protected]', | ||
}, | ||
port: 3000, | ||
host: 'localhost', | ||
api: { | ||
protocol: 'http', | ||
port: 3000, | ||
host: 'localhost', | ||
base: 'api', | ||
}, | ||
db: { | ||
uri: 'mongodb://localhost/WaosNodeDev', | ||
debug: true, | ||
|
@@ -140,15 +144,11 @@ module.exports = { | |
}, | ||
}, | ||
}, | ||
google: { | ||
clientId: 'WAOS_NODE_google_clientId', | ||
}, | ||
microsoft: { | ||
clientId: 'WAOS_NODE_microsoft_clientId', | ||
issuer: 'https://login.microsoftonline.com/9188040d-6c67-4c5b-b112-36a304b66dad/v2.0', | ||
discovery: 'https://login.microsoftonline.com/common/v2.0/.well-known/openid-configuration', | ||
// issuer: 'WAOS_NODE_microsoft_issuer', | ||
// discovery: 'WAOS_NODE_microsoft_discovery' | ||
oAuth: { | ||
google: { // google console / api & service / identifier | ||
clientID: null, | ||
clientSecret: null, | ||
}, | ||
}, | ||
// joi is used to manage schema restrictions, on the top of mongo / orm | ||
joi: { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* Module dependencies | ||
*/ | ||
const path = require('path'); | ||
const passport = require('passport'); | ||
const GoogleStrategy = require('passport-google-oauth20'); | ||
|
||
const config = require(path.resolve('./config')); | ||
const users = require('../../controllers/users.controller'); | ||
|
||
module.exports = () => { | ||
// Use google strategy | ||
if (config.oAuth && config.oAuth.google && config.oAuth.google.clientID && config.oAuth.google.clientSecret) { | ||
passport.use( | ||
new GoogleStrategy( | ||
{ | ||
clientID: config.oAuth.google.clientID, | ||
clientSecret: config.oAuth.google.clientSecret, | ||
callbackURL: `${config.api.protocol}://${config.api.host}${config.api.port ? ':' : ''}${config.api.port ? config.api.port : ''}/${config.api.base}/auth/google/callback`, | ||
scope: ['profile', 'email'], | ||
}, | ||
async (accessToken, refreshToken, profile, cb) => { | ||
// Set the provider data and include tokens | ||
const providerData = profile._json; | ||
providerData.accessToken = accessToken; | ||
providerData.refreshToken = refreshToken; | ||
// Create the user OAuth profile | ||
const providerUserProfile = { | ||
firstName: profile.name.givenName, | ||
lastName: profile.name.familyName, | ||
email: profile.emails[0].value, | ||
avatar: providerData.picture ? providerData.picture : undefined, | ||
provider: 'google', | ||
providerIdentifierField: 'email', | ||
providerData, | ||
}; | ||
// Save the user OAuth profile | ||
try { | ||
const user = await users.saveOAuthUserProfile(providerUserProfile, 'google'); | ||
return cb(null, user); | ||
} catch (err) { | ||
return cb(err); | ||
} | ||
}, | ||
), | ||
); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.