-
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.
- Loading branch information
Showing
17 changed files
with
470 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
fail_on_violations: true | ||
eslint: | ||
enabled: true | ||
config_file: .eslintrc.json | ||
ignore_file: .eslintignore | ||
enabled: true | ||
config_file: .eslintrc.json | ||
ignore_file: .eslintignore |
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,118 @@ | ||
import module from '../models'; | ||
import generateToken from '../../helpers/tokens/generate.token'; | ||
import status from '../../helpers/constants/status.codes'; | ||
|
||
const { User } = module; | ||
|
||
/** | ||
* | ||
* | ||
* @class socialLogin | ||
*/ | ||
class socialLogin { | ||
/** | ||
* signup || login via facebook | ||
* | ||
* @static | ||
* @param {object} req - request object | ||
* @param {object} res - response object | ||
* @memberof socialLogin | ||
* @returns {object} - the response body | ||
* | ||
*/ | ||
static async facebookAuth(req, res) { | ||
let existingUser = await User.findOne({ | ||
where: { | ||
uniqueId: req.user.id | ||
} | ||
}); | ||
if (!existingUser) { | ||
existingUser = await User.create({ | ||
username: req.user.name.familyName, | ||
profileImage: req.user.photos[0].value, | ||
provider: req.user.provider, | ||
uniqueId: req.user.id | ||
}); | ||
} | ||
const token = generateToken({ | ||
id: existingUser.dataValues.id, | ||
username: existingUser.dataValues.username | ||
}, | ||
process.env.TOKEN_KEY); | ||
return res.status(status.CREATED).json({ user: { ...existingUser.get(), token } }); | ||
} | ||
|
||
/** | ||
* signup || login via Google | ||
* | ||
* @static | ||
* @param {object} req - request object | ||
* @param {object} res - request response | ||
* @memberof socialLogin | ||
* @returns {object} - the response body | ||
* | ||
*/ | ||
static async googleAuth(req, res) { | ||
let existingUser = await User.findOne({ | ||
where: { | ||
uniqueId: req.user.id | ||
} | ||
}); | ||
if (!existingUser) { | ||
existingUser = User.create({ | ||
username: req.user.displayName, | ||
email: req.user.emails[0].value, | ||
profileImage: req.user.photos[0].value, | ||
provider: req.user.provider, | ||
uniqueId: req.user.id | ||
}); | ||
console.log(existingUser.dataValues); | ||
const token = generateToken({ username: req.user.displayName, id: req.user.id }, | ||
process.env.TOKEN_KEY); | ||
return res.status(status.CREATED).json({ user: { ...existingUser.get(), token } }); | ||
} | ||
const token = generateToken({ | ||
id: existingUser.dataValues.id, | ||
username: existingUser.dataValues.username | ||
}, | ||
process.env.TOKEN_KEY); | ||
return res.status(status.CREATED).json({ user: { ...existingUser.get(), token } }); | ||
} | ||
|
||
/** | ||
* signup || login user via Twitter | ||
* | ||
* @static | ||
* @param {object} req - request object | ||
* @param {object} res - request response | ||
* @memberof socialLogin | ||
* @returns {object} - the response body | ||
* | ||
*/ | ||
static async twitterAuth(req, res) { | ||
let existingUser = await User.findOne({ | ||
where: { | ||
uniqueId: req.user.id | ||
} | ||
}); | ||
if (!existingUser) { | ||
existingUser = User.create({ | ||
username: req.user.username, | ||
profileImage: req.user.photos[0].value, | ||
provider: req.user.provider, | ||
uniqueId: req.user.id | ||
}); | ||
const token = generateToken({ username: req.user.displayName, id: req.user.id }, | ||
process.env.TOKEN_KEY); | ||
return res.status(status.CREATED).json({ user: { ...existingUser.get(), token } }); | ||
} | ||
const token = generateToken({ | ||
id: existingUser.dataValues.id, | ||
username: existingUser.dataValues.username | ||
}, | ||
process.env.TOKEN_KEY); | ||
return res.status(status.CREATED).json({ user: { ...existingUser.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,51 @@ | ||
export default { | ||
up: (queryInterface, Sequelize) => queryInterface.createTable('Articles', { | ||
id: { | ||
allowNull: false, | ||
unique: true, | ||
primaryKey: true, | ||
type: Sequelize.UUID, | ||
defaultValue: Sequelize.UUIDV4 | ||
}, | ||
title: { | ||
type: Sequelize.STRING, | ||
allowNull: false | ||
}, | ||
slug: { | ||
type: Sequelize.STRING, | ||
allowNull: false, | ||
unique: true | ||
}, | ||
description: { | ||
type: Sequelize.STRING, | ||
allowNull: false | ||
}, | ||
body: { | ||
type: Sequelize.TEXT, | ||
allowNull: false | ||
}, | ||
tagList: { | ||
type: Sequelize.ARRAY(Sequelize.TEXT), | ||
allowNull: false | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
userId: { | ||
type: Sequelize.INTEGER, | ||
references: { | ||
model: 'Users', | ||
key: 'id' | ||
}, | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE', | ||
allowNull: false | ||
} | ||
}), | ||
down: (queryInterface, Sequelize) => queryInterface.dropTable('Articles') | ||
}; |
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 @@ | ||
'use strict'; | ||
module.exports = (sequelize, DataTypes) => { | ||
const test - articl = sequelize.define('test-articl', { | ||
name: DataTypes.STRING | ||
}, {}); | ||
test - articl.associate = function(models) { | ||
// associations can be defined here | ||
}; | ||
return test - articl; | ||
}; |
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 |
---|---|---|
|
@@ -62,6 +62,5 @@ export default (sequelize, DataTypes) => { | |
|
||
return queryResult; | ||
}; | ||
|
||
return 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 |
---|---|---|
@@ -1,10 +1,32 @@ | ||
import { Router } from 'express'; | ||
import ValidateUser from '../../middlewares/ValidateUser'; | ||
import authController from '../controllers/authController'; | ||
import socialAuthController from '../controllers/socialAuth'; | ||
import passport from '../../configs/passport'; | ||
import social from '../../middlewares/social/social'; | ||
|
||
const authRouter = new Router(); | ||
|
||
authRouter.post('/signup', ValidateUser.validateSignup, authController.signup); | ||
authRouter.post('/login', authController.login); | ||
|
||
authRouter.post('/mockFacebook', social, socialAuthController.facebookAuth); | ||
authRouter.post('/mockGoogle', social, socialAuthController.googleAuth); | ||
authRouter.post('/mockTwitter', social, socialAuthController.twitterAuth); | ||
|
||
authRouter.get('/facebook', passport.authenticate('facebook')); | ||
authRouter.get('/facebook/callback', | ||
passport.authenticate('facebook'), | ||
socialAuthController.facebookAuth); | ||
|
||
authRouter.get('/google', passport.authenticate('google', { scope: ['email', 'profile'] })); | ||
authRouter.get('/google/callback', | ||
passport.authenticate('google'), | ||
socialAuthController.googleAuth); | ||
|
||
authRouter.get('/twitter', passport.authenticate('twitter', { scope: ['email', 'profile'] })); | ||
authRouter.get('/twitter/callback', | ||
passport.authenticate('twitter'), | ||
socialAuthController.twitterAuth); | ||
|
||
export default authRouter; |
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 passport from 'passport'; | ||
import FacebookStrategy from 'passport-facebook'; | ||
import { OAuth2Strategy } from 'passport-google-oauth'; | ||
import TwitterStrategy from 'passport-twitter'; | ||
|
||
const { | ||
GOOGLE_CLIENT_ID, | ||
GOOGLE_CLIENT_SECRET, | ||
GOOGLE_CALLBACK, | ||
FACEBOOK_CLIENT_ID, | ||
FACEBOOK_CLIENT_SECRET, | ||
FACEBOOK_CALLBACK, | ||
TWITTER_CLIENT_ID, | ||
TWITTER_CLIENT_SECRET, | ||
TWITTER_CALLBACK | ||
} = process.env; | ||
|
||
passport.use(new OAuth2Strategy({ | ||
clientID: GOOGLE_CLIENT_ID, | ||
clientSecret: GOOGLE_CLIENT_SECRET, | ||
callbackURL: GOOGLE_CALLBACK, | ||
profileFields: ['name', 'photos', 'email'] | ||
}, | ||
(accessToken, refreshToken, profile, done) => done(null, profile))); | ||
|
||
passport.use(new FacebookStrategy({ | ||
clientID: FACEBOOK_CLIENT_ID, | ||
clientSecret: FACEBOOK_CLIENT_SECRET, | ||
callbackURL: FACEBOOK_CALLBACK, | ||
profileFields: ['name', 'photos', 'email'] | ||
}, | ||
(accessToken, refreshTocken, profile, done) => done(null, profile))); | ||
|
||
passport.use(new TwitterStrategy({ | ||
consumerKey: TWITTER_CLIENT_ID, | ||
consumerSecret: TWITTER_CLIENT_SECRET, | ||
callbackURL: TWITTER_CALLBACK, | ||
profile: ['name', 'photo', 'email'] | ||
}, | ||
(accessToken, refreshTocken, profile, done) => { | ||
done(null, profile); | ||
})); | ||
|
||
passport.serializeUser((user, done) => done(null, user)); | ||
passport.deserializeUser((user, done) => done(null, user)); | ||
|
||
export default passport; |
Oops, something went wrong.