-
-
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.
- Loading branch information
1 parent
1f564e0
commit 71891f9
Showing
6 changed files
with
126 additions
and
15 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
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,80 @@ | ||
/** | ||
* Module dependencies | ||
*/ | ||
const path = require('path'); | ||
const passport = require('passport'); | ||
const AppleStrategy = require('passport-apple'); | ||
|
||
const config = require(path.resolve('./config')); | ||
const users = require('../../controllers/users.controller'); | ||
|
||
const callbackURL = `${config.api.protocol}://${config.api.host}${ | ||
config.api.port ? ':' : '' | ||
}${config.api.port ? config.api.port : ''}/${ | ||
config.api.base | ||
}/auth/apple/callback`; | ||
|
||
module.exports = () => { | ||
// Use google strategy | ||
if ( | ||
config.oAuth && | ||
config.oAuth.apple && | ||
config.oAuth.apple.clientID && | ||
config.oAuth.apple.teamID && | ||
config.oAuth.apple.keyID | ||
) { | ||
passport.use( | ||
new AppleStrategy( | ||
{ | ||
clientID: config.oAuth.apple.clientID, | ||
teamID: config.oAuth.apple.teamID, | ||
callbackURL: config.oAuth.apple.callbackURL | ||
? config.oAuth.apple.callbackURL | ||
: callbackURL, | ||
keyID: config.oAuth.apple.keyID, | ||
privateKeyLocation: config.oAuth.apple.privateKeyLocation | ||
? config.oAuth.apple.privateKeyLocation | ||
: null, | ||
scope: ['email', 'name'], | ||
passReqToCallback: true, | ||
}, | ||
async (req, accessToken, refreshToken, decodedIdToken, profile, cb) => { | ||
// Set the provider data and include tokens | ||
const providerData = decodedIdToken; | ||
providerData.appleProfile = req.appleProfile; | ||
providerData.accessToken = accessToken || null; | ||
providerData.refreshToken = refreshToken || null; | ||
providerData.profile = profile || null; | ||
providerData.sub = decodedIdToken.sub; | ||
// Create the user OAuth profile | ||
const providerUserProfile = { | ||
firstName: | ||
req.appleProfile && req.appleProfile.name | ||
? req.appleProfile.name.firstName | ||
: null, | ||
lastName: | ||
req.appleProfile && req.appleProfile.name | ||
? req.appleProfile.name.lastName | ||
: null, | ||
email: req.appleProfile ? req.appleProfile.email : null, | ||
avatar: null, | ||
provider: 'apple', | ||
sub: providerData.sub, | ||
providerData, | ||
}; | ||
// Save the user OAuth profile | ||
try { | ||
const user = await users.saveOAuthUserProfile( | ||
providerUserProfile, | ||
'sub', | ||
'apple', | ||
); | ||
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