-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable Social Login using facebook [Delivers #167574878]
- Loading branch information
Adekoreday
committed
Aug 1, 2019
1 parent
452b993
commit 24133b0
Showing
16 changed files
with
387 additions
and
8 deletions.
There are no files selected for viewing
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,34 @@ | ||
import models from '../database/models'; | ||
|
||
const { User } = models; | ||
|
||
/** | ||
* @export | ||
* @class Users | ||
*/ | ||
class Auth { | ||
/** | ||
* @name FacebookSocialLogin | ||
* @async | ||
* @static | ||
* @memberof Auth | ||
* @param {Object} request express request object | ||
* @param {Object} response express response object | ||
* @returns {JSON} JSON object with details of new user | ||
*/ | ||
static async FacebookSocialLogin(request, response) { | ||
const mail = request.user.emails[0].value; | ||
const { givenName, familyName } = request.user.name; | ||
const users = await User.findOrCreate({ | ||
where: { email: mail }, | ||
defaults: { | ||
firstName: givenName, | ||
lastName: familyName, | ||
email: mail, | ||
userName: givenName | ||
} | ||
}); | ||
response.status(200).json({ success: 'true', data: users[0].dataValues }); | ||
} | ||
} | ||
export default Auth; |
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,17 @@ | ||
import passport from 'passport'; | ||
import { Strategy as FacebookStrategy } from 'passport-facebook'; | ||
import callback from './passportCallback'; | ||
|
||
export default () => { | ||
passport.use( | ||
new FacebookStrategy( | ||
{ | ||
clientID: process.env.FACEBOOK_APP_ID, | ||
clientSecret: process.env.FACEBOOK_APP_SECRET, | ||
callbackURL: process.env.REDIRECT_URL, | ||
profileFields: ['id', 'first_name', 'last_name', 'photos', 'email'] | ||
}, | ||
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,16 @@ | ||
import passport from 'passport'; | ||
import facebookPassport from './facebook'; | ||
|
||
export default (app) => { | ||
app.use(passport.initialize()); | ||
app.use(passport.session()); | ||
passport.serializeUser((user, cb) => { | ||
cb(null, user); | ||
}); | ||
|
||
passport.deserializeUser((obj, cb) => { | ||
cb(null, obj); | ||
}); | ||
|
||
facebookPassport(); | ||
}; |
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 @@ | ||
/** | ||
* @name passportError | ||
* @static | ||
* @memberof Users | ||
* @param {string} accessToken express err object | ||
* @param {string} refreshToken express request object | ||
* @param {Object} profile express response object | ||
* @param {function} done express next function | ||
* @returns {JSON} JSON object with details of new user | ||
*/ | ||
const callback = (accessToken, refreshToken, profile, done) => done(null, profile); | ||
|
||
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,23 @@ | ||
/** | ||
* @export | ||
* @class Users | ||
*/ | ||
class ErrorCatcher { | ||
/** | ||
* @name passportError | ||
* @static | ||
* @memberof Users | ||
* @param {Object} error express err object | ||
* @param {Object} req express request object | ||
* @param {Object} res express response object | ||
* @param {function} next express next function | ||
* @returns {JSON} JSON object with details of new user | ||
*/ | ||
static passportErrors(error, req, res, next) { | ||
if (!error) { | ||
next(); | ||
} | ||
res.status(400).json({ success: 'false', message: 'Auth failed' }); | ||
} | ||
} | ||
export default ErrorCatcher; |
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,17 @@ | ||
import express from 'express'; | ||
import passport from 'passport'; | ||
import Passport from '../helpers/passport'; | ||
import PassportError from '../helpers/passportError'; | ||
import Auth from '../controllers/Auth'; | ||
|
||
const auth = express.Router(); | ||
|
||
Passport(auth); | ||
auth.get('/facebook', passport.authenticate('facebook', { scope: ['email'] })); | ||
auth.get( | ||
'/facebook/callback', | ||
passport.authenticate('facebook', { failureRedirect: '/login' }), | ||
PassportError.passportErrors, | ||
Auth.FacebookSocialLogin | ||
); | ||
export default auth; |
Oops, something went wrong.