-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from andela/feature/167164984/enable-social-login
#167164984 Enable social login for Facebook and Google
- Loading branch information
Showing
13 changed files
with
378 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
repo_token: bmbJJhTYflmvp4musILhQ4vscjSi794RE |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,47 @@ | ||
import dotenv from 'dotenv'; | ||
import models from '../database/models'; | ||
import authHelper from '../helpers/authHelper'; | ||
|
||
dotenv.config(); | ||
|
||
const { User } = models; | ||
|
||
const controller = async (req, res) => { | ||
const { user } = req; | ||
const userData = { | ||
firstName: user.firstName, | ||
lastName: user.lastName, | ||
email: `${user.oauthId}@gmail.com`, | ||
password: user.oauthId, | ||
avatarUrl: user.profileImage, | ||
isVerified: true, | ||
}; | ||
|
||
try { | ||
let dbUser = await User.findOne({ | ||
where: { password: user.oauthId } | ||
}); | ||
|
||
if (!dbUser) { | ||
dbUser = await User.create(userData); | ||
} | ||
|
||
const payload = { | ||
id: dbUser.id, | ||
email: '', | ||
token: authHelper.generateToken({ id: dbUser.id }), | ||
bio: dbUser.bio, | ||
image: dbUser.avatar, | ||
isVerified: dbUser.isVerified, | ||
firstName: dbUser.firstName, | ||
lastName: dbUser.lastName, | ||
avatarUrl: dbUser.avatarUrl, | ||
}; | ||
const token = authHelper.generateToken(payload); | ||
|
||
return res.redirect(`${process.env.FRONTEND_OAUTH_CALLBACK}?token=${token}`); | ||
} catch (e) { | ||
return res.redirect(`${process.env.FRONTEND_OAUTH_CALLBACK}`); | ||
} | ||
}; | ||
export default controller; |
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,29 @@ | ||
const callback = (accessToken, refreshToken, profile, done) => { | ||
const { id, provider } = profile; | ||
const { picture } = profile._json; | ||
const user = { oauthId: id, type: provider }; | ||
|
||
if (provider === 'google') { | ||
const { family_name: lastName, given_name: firstName, email } = profile._json; | ||
user.lastName = lastName; | ||
user.firstName = firstName; | ||
user.email = email; | ||
|
||
if (picture && picture.trim() !== '') { | ||
user.profileImage = picture.trim(); | ||
} | ||
} else if (provider === 'facebook') { | ||
const { last_name: lastName, first_name: firstName } = profile._json; | ||
user.lastName = lastName; | ||
user.firstName = firstName; | ||
|
||
const photo = picture && picture.data && picture.data.url; | ||
|
||
if (photo && photo.trim() !== '') { | ||
user.profileImage = photo.trim(); | ||
} | ||
} | ||
return done(null, user); | ||
}; | ||
|
||
export default callback; |
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,39 @@ | ||
import passport from 'passport'; | ||
import { Strategy as FacebookStrategy } from 'passport-facebook'; | ||
import { ExtractJwt, Strategy as JWTStrategy } from 'passport-jwt'; | ||
import { OAuth2Strategy as passportGoogle } from 'passport-google-oauth'; | ||
import dotenv from 'dotenv'; | ||
import callback from './callback'; | ||
|
||
dotenv.config(); | ||
|
||
const passportFacebookConfig = { | ||
clientID: process.env.FACEBOOK_CLIENT_ID, | ||
clientSecret: process.env.FACEBOOK_CLIENT_SECRET, | ||
callbackURL: process.env.FACEBOOK_CALLBACK_URL, | ||
profileFields: ['id', 'displayName', 'name', 'picture.type(large)'], | ||
}; | ||
|
||
const passportGoogleConfig = { | ||
clientID: process.env.GOOGLE_CLIENT_ID, | ||
clientSecret: process.env.GOOGLE_CLIENT_SECRET, | ||
callbackURL: process.env.GOOGLE_CALLBACK_URL, | ||
}; | ||
|
||
const passportJWTConfig = { | ||
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), | ||
secretOrKey: process.env.SECRET_KEY | ||
}; | ||
|
||
const jwtHandler = (payload, done) => { | ||
done(null, payload); | ||
}; | ||
|
||
const setPassportMiddleware = (server) => { | ||
server.use(passport.initialize()); | ||
passport.use(new FacebookStrategy(passportFacebookConfig, callback)); | ||
passport.use(new passportGoogle(passportGoogleConfig, callback)); | ||
passport.use(new JWTStrategy(passportJWTConfig, jwtHandler)); | ||
}; | ||
|
||
export default setPassportMiddleware; |
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,13 @@ | ||
import express from 'express'; | ||
import passport from 'passport'; | ||
import socialController from '../controllers/social'; | ||
|
||
const router = express.Router(); | ||
|
||
router.get('/oauth/facebook', passport.authenticate('facebook', { session: false })); | ||
router.get('/oauth/facebook/callback', passport.authenticate('facebook', { session: false }), socialController); | ||
|
||
router.get('/oauth/google', passport.authenticate('google', { session: false, scope: ['openid', 'profile', 'email'] })); | ||
router.get('/oauth/google/callback', passport.authenticate('google', { session: false }), socialController); | ||
|
||
export default router; |
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,61 @@ | ||
export const facebookProfileData = { | ||
id: '10217955248420473', | ||
username: undefined, | ||
displayName: 'Ree Emili', | ||
name: { | ||
familyName: 'Emili', | ||
givenName: 'Ree', | ||
middleName: undefined | ||
}, | ||
gender: undefined, | ||
profileUrl: undefined, | ||
photos: [{ | ||
value: 'https://platform-lookaside.fbsbx.com/platform/profilepic/?asid=10217955248420473&height=200&width=200&ext=1567064544&hash=AeREG6S5RGgebQxg' | ||
}], | ||
provider: 'facebook', | ||
_raw: '{"id":"10217955248420473","name":"Ree Emili","last_name":"Emili","first_name":"Ree","picture":{"data":{"height":200,"is_silhouette":false,"url":"https:\\/\\/platform-lookaside.fbsbx.com\\/platform\\/profilepic\\/?asid=10217955248420473&height=200&width=200&ext=1567064544&hash=AeREG6S5RGgebQxg","width":200}}}', | ||
_json: { | ||
id: '10217955248420473', | ||
name: 'Ree Emili', | ||
last_name: 'Emili', | ||
first_name: 'Ree', | ||
picture: { | ||
data: { | ||
height: 200, | ||
is_silhouette: false, | ||
url: 'https://platform-lookaside.fbsbx.com/platform/profilepic/?asid=10217955248420473&height=200&width=200&ext=1567064921&hash=AeRplVHtzPiBW6hq', | ||
width: 200 | ||
} | ||
} | ||
} | ||
}; | ||
|
||
|
||
export const googleProfileData = { | ||
id: '101484569342137250397', | ||
displayName: 'Rita Emili', | ||
name: { | ||
familyName: 'Emili', | ||
givenName: 'Rita' | ||
}, | ||
emails: [{ | ||
value: 'rita.emili@andela.com', verified: true | ||
}], | ||
photos: [{ | ||
value: 'https://lh5.googleusercontent.com/-Wnh9r4t4PKI/AAAAAAAAAAI/AAAAAAAAAAA/ACHi3reFo9mVElLGMqSXJsZp6NTeVLfA0w/photo.jpg' | ||
}], | ||
provider: 'google', | ||
_raw: | ||
'{\n "sub": "101484569342137250397",\n "name": "Rita Emili",\n "given_name": "Rita",\n "family_name": "Emili",\n "picture": "https://lh5.googleusercontent.com/-Wnh9r4t4PKI/AAAAAAAAAAI/AAAAAAAAAAA/ACHi3reFo9mVElLGMqSXJsZp6NTeVLfA0w/photo.jpg",\n "email": "rita.emili@andela.com",\n "email_verified": true,\n "locale": "en",\n "hd": "andela.com"\n}', | ||
_json: { | ||
sub: '101484569342137250397', | ||
name: 'Rita Emili', | ||
given_name: 'Rita', | ||
family_name: 'Emili', | ||
picture: 'https://lh5.googleusercontent.com/-Wnh9r4t4PKI/AAAAAAAAAAI/AAAAAAAAAAA/ACHi3reFo9mVElLGMqSXJsZp6NTeVLfA0w/photo.jpg', | ||
email: 'rita.emili@andela.com', | ||
email_verified: true, | ||
locale: 'en', | ||
hd: 'andela.com' | ||
} | ||
}; |
Oops, something went wrong.