-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(social-login): enable social login through facebook and google
- add test for response from social login - enable social login to facebook - enable social login to google [Delivers #159206051]
- Loading branch information
1 parent
864281c
commit 3fc805a
Showing
11 changed files
with
10,715 additions
and
28 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 |
---|---|---|
@@ -1,31 +1,60 @@ | ||
import passport from 'passport'; | ||
import bcrypt from 'bcrypt'; | ||
import local from 'passport-local'; | ||
import db from '../models'; | ||
import GoogleStrategy from 'passport-google-oauth20'; | ||
import FacebookStrategy from 'passport-facebook'; | ||
import AuthController from '../controllers/authController'; | ||
import { User } from '../models'; | ||
|
||
const LocalStrategy = local.Strategy; | ||
|
||
passport.use(new LocalStrategy( | ||
{ | ||
usernameField: 'user[email]', | ||
passwordField: 'user[password]' | ||
}, | ||
((email, password, done) => { | ||
db.User.findOne({ where: { email } }) | ||
.then((user) => { | ||
bcrypt.compare( | ||
password, | ||
user.hashedPassword, | ||
(err, res) => { | ||
if (!user || !res || err) { | ||
return done(null, false, { | ||
errors: { 'email or password': 'is invalid' } | ||
}); | ||
} | ||
return done(null, user); | ||
const passportConfig = (app) => { | ||
app.use(passport.initialize()); | ||
app.use(passport.session()); | ||
passport.use(new LocalStrategy( | ||
{ | ||
usernameField: 'user[email]', | ||
passwordField: 'user[password]' | ||
}, | ||
((email, password, done) => { | ||
User.findOne({ email }) | ||
.then((user) => { | ||
if (!user || !user.validPassword(password)) { | ||
return done(null, false, { | ||
errors: { 'email or password': 'is invalid' } | ||
}); | ||
} | ||
); | ||
}) | ||
.catch(done); | ||
}) | ||
)); | ||
|
||
return done(null, user); | ||
}) | ||
.catch(done); | ||
}) | ||
)); | ||
|
||
/** | ||
* configurations for passport strategies | ||
*/ | ||
|
||
passport.use(new GoogleStrategy({ | ||
clientID: process.env.GOOGLE_CLIENT_ID, | ||
clientSecret: process.env.GOOGLE_CLIENT_SECRET, | ||
callbackURL: '/api/users/login/google/redirect', | ||
}, AuthController.strategyCallback)); | ||
|
||
passport.use(new FacebookStrategy({ | ||
clientID: process.env.FACEBOOK_APP_ID, | ||
clientSecret: process.env.FACEBOOK_APP_SECRET, | ||
callbackURL: '/api/users/login/facebook/redirect', | ||
profileFields: ['id', 'displayName', 'photos', 'email'], | ||
}, AuthController.strategyCallback)); | ||
|
||
passport.serializeUser(((user, done) => { | ||
done(null, user.id); | ||
})); | ||
passport.deserializeUser(((id, done) => { | ||
User.findById(id, (err, user) => { | ||
done(err, user); | ||
}); | ||
})); | ||
}; | ||
|
||
export default passportConfig; |
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,99 @@ | ||
import { User } from '../models'; | ||
import utilities from '../helpers/utilities'; | ||
/** | ||
* @class AuthController | ||
* | ||
* @export | ||
* | ||
*/ | ||
export default class AuthController { | ||
/** | ||
* @description - json response function | ||
* @static | ||
* | ||
* | ||
* @param {object} req | ||
* @param {object} res | ||
* | ||
* @returns {json} json | ||
* | ||
* @memberof AuthController | ||
* | ||
*/ | ||
static jsonResponse(req, res) { | ||
const user = { | ||
email: req.user.email, | ||
token: utilities.signToken({ id: req.user.id }), | ||
firstname: req.user.firstname, | ||
lastname: req.user.lastname, | ||
username: req.user.username, | ||
image: req.user.image, | ||
}; | ||
if (req.user.created) { | ||
return res.status(201).json({ success: true, user }); | ||
} | ||
return res.status(200).json({ success: true, user }); | ||
} | ||
|
||
/** | ||
* @description - model query function | ||
* @static | ||
* | ||
* | ||
* @param {object} user | ||
* @param {function} done | ||
* | ||
* @returns {object} newOrFoundUser | ||
* | ||
* @memberof AuthController | ||
* | ||
*/ | ||
static queryModel(user, done) { | ||
User.findOrCreate({ | ||
where: { | ||
email: user.email, | ||
}, | ||
defaults: user, | ||
}).spread((newOrFoundUser, created) => { | ||
if (!newOrFoundUser) { return done(null, false); } | ||
const { | ||
id, email, username, lastname, firstname, image, | ||
} = newOrFoundUser.dataValues; | ||
return done(null, { | ||
id, | ||
email, | ||
username, | ||
lastname, | ||
firstname, | ||
image, | ||
created, | ||
}); | ||
}); | ||
} | ||
|
||
|
||
/** | ||
* @description - strategy callback function | ||
* @static | ||
* | ||
* @param {object} accessToken | ||
* @param {object} refreshToken | ||
* @param {object} profile | ||
* @param {function} done | ||
* | ||
* @returns {json} json | ||
* | ||
* @memberof AuthController | ||
* | ||
*/ | ||
static strategyCallback(accessToken, refreshToken, profile, done) { | ||
const userProfile = { | ||
firstname: profile.name.familyName, | ||
lastname: profile.name.givenName, | ||
username: profile.id, | ||
email: profile.emails[0].value, | ||
image: profile.photos[0].value, | ||
}; | ||
AuthController.queryModel(userProfile, done); | ||
} | ||
} |
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.