-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ft(social auth): implement social auth
- implement facebook authentication - implement google authentication - write unit tests - document api using swagger [Delivers #169817866]
- Loading branch information
Showing
22 changed files
with
1,070 additions
and
210 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,3 +1,11 @@ | ||
{ | ||
"plugins": [ | ||
[ | ||
"@babel/plugin-transform-runtime", | ||
{ | ||
"regenerator": true | ||
} | ||
] | ||
], | ||
"presets": ["@babel/preset-env"] | ||
} |
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,2 @@ | ||
src/app.js | ||
src/database/models/index.js |
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 |
---|---|---|
|
@@ -9,3 +9,4 @@ dist/ | |
coverage/ | ||
|
||
.DS_Store | ||
coverage/ |
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
Large diffs are not rendered by default.
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 |
---|---|---|
@@ -1,13 +1,15 @@ | ||
import express from 'express'; | ||
import bodyParser from 'body-parser'; | ||
import swagger from './routes/swagger'; | ||
import socialRoute from './routes/social.route'; | ||
import './config/passport.config'; | ||
|
||
const app = express(); | ||
app.use(express.json()); | ||
app.use(bodyParser.json()); | ||
app.use(bodyParser.urlencoded({ extended: false })); | ||
|
||
app.use('/API/v1', swagger); | ||
app.use('/', swagger); | ||
app.use('/api/v1/auth', socialRoute); | ||
app.get('/', (req, res) => res.status(200).send({ status: 200, message: 'Welcome to Barefoot Nomad!' })); | ||
|
||
export default app; |
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,30 @@ | ||
import { Strategy as GoogleStrategy } from 'passport-google-oauth20'; | ||
import dotenv from 'dotenv'; | ||
import passport from 'passport'; | ||
import FacebookStrategy from 'passport-facebook'; | ||
import userController from '../controllers/user.controller'; | ||
|
||
dotenv.config(); | ||
const { | ||
GOOGLE_CLIENT_ID, | ||
GOOGLE_CLIENT_SECRET, | ||
FACEBOOK_CLIENT_ID, | ||
FACEBOOK_CLIENT_SECRET, | ||
BASE_URL | ||
} = process.env; | ||
passport.use( | ||
new GoogleStrategy({ | ||
callbackURL: `${BASE_URL}/api/v1/auth/google/redirect`, | ||
clientID: GOOGLE_CLIENT_ID, | ||
clientSecret: GOOGLE_CLIENT_SECRET, | ||
googleFields: ['emails', 'firstName', 'lastName'], | ||
}, userController.googleAndFacebookPlusAuth) | ||
); | ||
passport.use( | ||
new FacebookStrategy({ | ||
callbackURL: `${BASE_URL}/api/v1/auth/facebook/redirect`, | ||
clientID: FACEBOOK_CLIENT_ID, | ||
clientSecret: FACEBOOK_CLIENT_SECRET, | ||
profileFields: ['id', 'displayName', 'first_name', 'last_name', 'email'], | ||
}, userController.googleAndFacebookPlusAuth) | ||
); |
Empty file.
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,62 @@ | ||
import encryptToken from '../helpers/encryption'; | ||
import db from '../database/models'; | ||
|
||
/** | ||
* class that contains functions to implement login with social accounts | ||
*/ | ||
class UserController { | ||
/** | ||
*login function to get profile from google and facebook and manipulate it | ||
* | ||
* | ||
*@param {object} accessToken response | ||
*@param {object} refreshToken response | ||
*@param {object} profile object | ||
*@param {object} done callback | ||
*@returns {object} object | ||
*/ | ||
static async googleAndFacebookPlusAuth(accessToken, refreshToken, profile, done) { | ||
try { | ||
const userData = { | ||
id: profile.id, | ||
firstname: profile.name.givenName, | ||
lastname: profile.name.familyName, | ||
email: profile.emails[0].value, | ||
authtype: profile.provider | ||
}; | ||
const { | ||
email, firstname, lastname, authtype | ||
} = userData; | ||
await db.users.findOrCreate({ | ||
where: { email }, | ||
defaults: { | ||
firstname, | ||
lastname, | ||
email, | ||
password: null, | ||
authtype | ||
} | ||
}); | ||
done(null, userData); | ||
} catch (error) { | ||
done(error, false, error.message); | ||
} | ||
} | ||
|
||
/** | ||
*login function to return data from social accounts to the user | ||
* | ||
* | ||
*@param {object} req request | ||
*@param {object} res response | ||
*@returns {object} object | ||
*/ | ||
static authGoogleAndFacebook(req, res) { | ||
const token = encryptToken(req.user); | ||
return res.status(200).send({ | ||
status: 200, message: `User logged in with ${req.user.authtype}`, data: req.user, token | ||
}); | ||
} | ||
} | ||
|
||
export default UserController; |
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,56 @@ | ||
|
||
module.exports = { | ||
up: (queryInterface, Sequelize) => queryInterface.createTable('users', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
firstname: { | ||
allowNull: false, | ||
type: Sequelize.STRING | ||
}, | ||
lastname: { | ||
allowNull: false, | ||
type: Sequelize.STRING | ||
}, | ||
country: { | ||
allowNull: false, | ||
type: Sequelize.STRING | ||
}, | ||
gender: { | ||
allowNull: false, | ||
type: Sequelize.STRING | ||
}, | ||
birthday: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
email: { | ||
allowNull: false, | ||
type: Sequelize.STRING | ||
}, | ||
password: { | ||
allowNull: true, | ||
type: Sequelize.STRING | ||
}, | ||
phonenumber: { | ||
allowNull: false, | ||
type: Sequelize.STRING | ||
}, | ||
authtype: { | ||
allowNull: false, | ||
type: Sequelize.STRING | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
} | ||
}), | ||
down: (queryInterface) => queryInterface.dropTable('users') | ||
}; |
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,18 @@ | ||
|
||
module.exports = (sequelize, DataTypes) => { | ||
const users = sequelize.define('users', { | ||
firstname: DataTypes.STRING, | ||
lastname: DataTypes.STRING, | ||
country: DataTypes.STRING, | ||
gender: DataTypes.STRING, | ||
birthday: DataTypes.DATE, | ||
email: DataTypes.STRING, | ||
password: DataTypes.STRING, | ||
phonenumber: DataTypes.STRING, | ||
authtype: DataTypes.STRING | ||
}, {}); | ||
users.associate = () => { | ||
// associations can be defined here | ||
}; | ||
return users; | ||
}; |
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,11 @@ | ||
import { config } from 'dotenv'; | ||
import jwt from 'jsonwebtoken'; | ||
|
||
config(); | ||
|
||
const encryptToken = (payload) => { | ||
const tokenGenerated = jwt.sign(payload, process.env.KEY); | ||
return tokenGenerated; | ||
}; | ||
|
||
export default encryptToken; |
Empty file.
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,53 @@ | ||
import { Router } from 'express'; | ||
import passport from 'passport'; | ||
import userController from '../controllers/user.controller'; | ||
|
||
const router = Router(); | ||
router.use(passport.initialize()); | ||
router.use(passport.session()); | ||
passport.serializeUser((user, done) => { | ||
done(null, user); | ||
}); | ||
|
||
/** | ||
* @swagger | ||
* | ||
* /auth/facebook: | ||
* get: | ||
* summary: User login with facebook account | ||
* responses: | ||
* 200: | ||
* description: Logged in with facebook successfully | ||
*/ | ||
/** | ||
* @swagger | ||
* | ||
* /auth/google: | ||
* get: | ||
* summary: User login with google account | ||
* responses: | ||
* 200: | ||
* description: Logged in successfully | ||
*/ | ||
/** | ||
* @swagger | ||
* components: | ||
* schemas: | ||
* User: | ||
* type: object | ||
* required: | ||
* - access_token | ||
* properties: | ||
* access_token: | ||
* type: string | ||
* example: | ||
* access_token: xx-xx-xx | ||
* | ||
*/ | ||
|
||
router.get('/google', passport.authenticate('google', { scope: ['profile', 'email'] })); | ||
router.get('/google/redirect', passport.authenticate('google', { failureRedirect: '/google' }), userController.authGoogleAndFacebook); | ||
router.get('/facebook', passport.authenticate('facebook')); | ||
router.get('/facebook/redirect', passport.authenticate('facebook', { failureRedirect: '/facebook' }), userController.authGoogleAndFacebook); | ||
|
||
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
Oops, something went wrong.