-
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.
[ft-#166240824] Enable Social Login via Google, Facebook and twitter
- Loading branch information
Ramadhan
authored and
Ramadhan
committed
Jun 12, 2019
1 parent
493d3a3
commit 9e77be6
Showing
13 changed files
with
335 additions
and
20 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
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,102 @@ | ||
import jwt from 'jsonwebtoken'; | ||
import model from '../models'; | ||
|
||
const { User } = model; | ||
|
||
/** | ||
* | ||
* | ||
* @class socialLogin | ||
*/ | ||
class socialLogin { | ||
/** | ||
* signup via facebook | ||
* login via facebook | ||
* | ||
* @static | ||
* @param {object} req - request object | ||
* @param {object} res - response object | ||
* @memberof socialLogin | ||
* @returns {object} - the response body | ||
*/ | ||
static async socialFacebookAuth(req, res) { | ||
let findIfExist = await User.findOne({ | ||
where: { | ||
uniqueId: req.user.id | ||
} | ||
}); | ||
if (!findIfExist) { | ||
findIfExist = User.create({ | ||
firstName: req.user.name.givenName, | ||
lastName: req.user.name.familyName, | ||
userName: req.user.username, | ||
profileImage: req.user.photos[0].value, | ||
provider: req.user.provider, | ||
uniqueId: req.user.id | ||
}); | ||
} | ||
const token = jwt.sign({ id: findIfExist.id }, 'secret', { expiresIn: '1d' }); | ||
res.json({ user: { ...findIfExist.get(), token } }); | ||
} | ||
|
||
/** | ||
* signup user via Google | ||
* login user via Google | ||
* | ||
* @static | ||
* @param {object} req - request object | ||
* @param {object} res - request response | ||
* @memberof socialLogin | ||
* @returns {object} - the response body | ||
*/ | ||
static async socialGoogleAuth(req, res) { | ||
let findIfExist = await User.findOne({ | ||
where: { | ||
uniqueId: req.user.id | ||
} | ||
}); | ||
if (!findIfExist) { | ||
findIfExist = User.create({ | ||
firstName: req.user.name.familyName, | ||
lastName: req.user.name.givenName, | ||
userName: req.user.displayName, | ||
email: req.user.emails[0].value, | ||
profileImage: req.user.photos[0].value, | ||
provider: req.user.provider, | ||
uniqueId: req.user.id | ||
}); | ||
} | ||
const token = jwt.sign({ id: findIfExist.id }, 'secret', { expiresIn: '1d' }); | ||
res.json({ user: { ...findIfExist.get(), token } }); | ||
} | ||
|
||
/** | ||
* signup user via Twitter | ||
* login user via Twitter | ||
* | ||
* @static | ||
* @param {object} req - request object | ||
* @param {object} res - request response | ||
* @memberof socialLogin | ||
* @returns {object} - the response body | ||
*/ | ||
static async socialtwitterAuth(req, res) { | ||
let findIfExist = await User.findOne({ | ||
where: { | ||
uniqueId: req.user.id | ||
} | ||
}); | ||
if (!findIfExist) { | ||
findIfExist = User.create({ | ||
userName: req.user.username, | ||
profileImage: req.user.photos[0].value, | ||
provider: req.user.provider, | ||
uniqueId: req.user.id | ||
}); | ||
} | ||
const token = jwt.sign({ id: findIfExist.id }, 'secret', { expiresIn: '1d' }); | ||
res.json({ user: { ...findIfExist.get(), token } }); | ||
} | ||
} | ||
|
||
export default socialLogin; |
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,36 @@ | ||
const user = (sequelize, DataTypes) => { | ||
const User = sequelize.define('user', { | ||
firstName: { | ||
type: DataTypes.STRING, | ||
allowNull: true | ||
}, | ||
lastName: { | ||
type: DataTypes.STRING, | ||
allowNull: true | ||
}, | ||
userName: { | ||
type: DataTypes.STRING, | ||
allowNull: true | ||
}, | ||
email: { | ||
type: DataTypes.STRING, | ||
allowNull: true | ||
}, | ||
profileImage: { | ||
type: DataTypes.STRING, | ||
allowNull: true | ||
}, | ||
provider: { | ||
type: DataTypes.STRING, | ||
allowNull: false | ||
}, | ||
uniqueId: { | ||
type: DataTypes.STRING, | ||
allowNull: false | ||
} | ||
}); | ||
|
||
return User; | ||
}; | ||
|
||
export default user; |
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,15 @@ | ||
import express from 'express'; | ||
import { passport } from '../../../configs/environnements'; | ||
import socialLogin from '../../controllers/socialAuth'; | ||
|
||
// Declaring the app | ||
const facebookRouter = express(); | ||
|
||
facebookRouter.get('/facebook', passport.authenticate('facebook')); | ||
facebookRouter.get( | ||
'/facebook/callback', | ||
passport.authenticate('facebook'), | ||
socialLogin.socialFacebookAuth | ||
); | ||
|
||
export default facebookRouter; |
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,10 @@ | ||
import express from 'express'; | ||
import { passport } from '../../../configs/environnements'; | ||
import socialLogin from '../../controllers/socialAuth'; | ||
// Declaring the app | ||
const googleRouter = express(); | ||
|
||
googleRouter.get('/google', passport.authenticate('google', { scope: ['email', 'profile'] })); | ||
googleRouter.get('/google/callback', passport.authenticate('google'), socialLogin.socialGoogleAuth); | ||
|
||
export default googleRouter; |
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,14 @@ | ||
import express from 'express'; | ||
import { passport } from '../../../configs/environnements'; | ||
import socialLogin from '../../controllers/socialAuth'; | ||
// Declaring the app | ||
const twitterRouter = express(); | ||
|
||
twitterRouter.get('/twitter', passport.authenticate('twitter', { scope: ['email', 'profile'] })); | ||
twitterRouter.get( | ||
'/twitter/callback', | ||
passport.authenticate('twitter'), | ||
socialLogin.socialtwitterAuth | ||
); | ||
|
||
export default twitterRouter; |
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.