-
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
14 changed files
with
357 additions
and
46 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => queryInterface.addColumn('Users', 'social', { | ||
type: Sequelize.BOOLEAN, | ||
allowNull: false, | ||
defaultValue: false | ||
}), | ||
down: queryInterface => queryInterface | ||
.removeColumn('Users', 'social') | ||
.then(() => queryInterface.removeColumn('Users', 'social')) | ||
}; |
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,16 @@ | ||
PORT= | ||
DATABASE_URL= | ||
TEST_DATABASE_URL= | ||
NODE_ENV= | ||
SECRET= | ||
FACEBOOK_APP_ID= | ||
FACEBOOK_SECRET= | ||
FACEBOOK_CALLBACk= | ||
TWITTER_KEY= | ||
TWITTER_SECRET= | ||
TWITTER_CALLBACk= | ||
SESSION_SECRET= | ||
GOOGLE_CLIENT_ID= | ||
GOOGLE_CLIENT_SECRET= | ||
BASE_URL= | ||
GOOGLE_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,110 @@ | ||
import passport from 'passport'; | ||
import googleStrategy from 'passport-google-oauth'; | ||
import dotenv from 'dotenv'; | ||
import { Strategy as FacebookStrategy } from 'passport-facebook'; | ||
import { Strategy as TwitterStrategy } from 'passport-twitter'; | ||
import { createUserFromSocialLoginDetails } from '../utils'; | ||
|
||
dotenv.config(); | ||
const GoogleStrategy = googleStrategy.OAuth2Strategy; | ||
|
||
passport.use( | ||
new GoogleStrategy( | ||
{ | ||
clientID: process.env.GOOGLE_CLIENT_ID, | ||
clientSecret: process.env.GOOGLE_CLIENT_SECRET, | ||
callbackURL: `${process.env.BASE_URL}/${process.env.GOOGLE_CALLBACK}` | ||
}, | ||
async (accessToken, refreshToken, profile, done) => { | ||
const firstName = profile.name.givenName; | ||
const lastName = profile.name.familyName; | ||
const username = profile.displayName.replace(/ /g, '_').toLowerCase(); | ||
const email = profile.emails[0].value; | ||
const image = profile.photos[0].value; | ||
|
||
const data = { | ||
email, | ||
firstName, | ||
lastName, | ||
username, | ||
image | ||
}; | ||
|
||
const result = await createUserFromSocialLoginDetails(data); | ||
|
||
return done(null, result); | ||
} | ||
) | ||
); | ||
|
||
passport.use( | ||
new FacebookStrategy( | ||
{ | ||
clientID: process.env.FACEBOOK_APP_ID, | ||
clientSecret: process.env.FACEBOOK_SECRET, | ||
callbackURL: process.env.FACEBOOK_CALLBACk, | ||
profileFields: ['id', 'displayName', 'photos', 'emails', 'name'] | ||
}, | ||
async (accessToken, refreshToken, profile, done) => { | ||
const email = profile.emails[0].value; | ||
const lastName = profile.name.familyName.toLocaleLowerCase(); | ||
const firstName = profile.name.givenName.toLocaleLowerCase(); | ||
const username = profile.displayName.replace(/ /g, '_').toLocaleLowerCase(); | ||
const image = profile.photos[0].value; | ||
|
||
const data = { | ||
email, | ||
firstName, | ||
lastName, | ||
username, | ||
image | ||
}; | ||
|
||
const result = await createUserFromSocialLoginDetails(data); | ||
|
||
return done(null, result); | ||
} | ||
) | ||
); | ||
|
||
passport.use( | ||
new TwitterStrategy( | ||
{ | ||
consumerKey: process.env.TWITTER_KEY, | ||
consumerSecret: process.env.TWITTER_SECRET, | ||
callbackURL: process.env.TWITTER_CALLBACk, | ||
includeEmail: true | ||
}, | ||
async (token, tokenSecret, profile, done) => { | ||
const { username } = profile; | ||
let { displayName } = profile; | ||
const image = profile.photos[0].value; | ||
const email = profile.emails[0].value.toLocaleLowerCase(); | ||
displayName = displayName.split(' '); | ||
const firstName = displayName[0].toLocaleLowerCase(); | ||
const lastName = displayName[1].toLocaleLowerCase(); | ||
|
||
const data = { | ||
email, | ||
firstName, | ||
lastName, | ||
username, | ||
image | ||
}; | ||
|
||
const result = await createUserFromSocialLoginDetails(data); | ||
|
||
return done(null, result); | ||
} | ||
) | ||
); | ||
|
||
passport.serializeUser((user, done) => { | ||
done(null, user); | ||
}); | ||
|
||
passport.deserializeUser((user, done) => { | ||
done(null, user); | ||
}); | ||
|
||
export default passport; |
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,7 @@ | ||
import v1 from './v1'; | ||
import oauth from './oauth'; | ||
|
||
export default (app) => { | ||
app.use('/api/v1', v1); | ||
app.use('/auth', oauth); | ||
}; |
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,42 @@ | ||
import express from 'express'; | ||
import passport from '../../middlewares/passport'; | ||
|
||
const router = express.Router(); | ||
|
||
// google login | ||
router.get('/google', passport.authenticate('google', { scope: ['profile', 'email'] })); | ||
router.get( | ||
'/google/callback', | ||
passport.authenticate('google', { | ||
failureRedirect: '/api/v1/error' | ||
}), | ||
(req, res) => { | ||
res.status(200).json(req.user); | ||
} | ||
); | ||
|
||
// facebook login | ||
router.get('/facebook', passport.authenticate('facebook', { scope: 'email' })); | ||
router.get( | ||
'/facebook/callback', | ||
passport.authenticate('facebook', { | ||
failureRedirect: '/api/v1/error' | ||
}), | ||
(req, res) => res.status(200).send({ | ||
u: req.user | ||
}) | ||
); | ||
|
||
// twiiter login | ||
router.get('/twitter', passport.authenticate('twitter', { scope: ['include_email=true'] })); | ||
router.get( | ||
'/twitter/callback', | ||
passport.authenticate('twitter', { | ||
failureRedirect: '/api/v1/error' | ||
}), | ||
(req, res) => res.status(200).json({ | ||
u: req.user | ||
}) | ||
); | ||
|
||
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
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.