-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from andela/ft-social-login-164198492
#164198492: Social Login Endpoints
- Loading branch information
Adedamola Salisu
authored and
Adedamola Salisu
committed
Mar 4, 2019
1 parent
d0478e1
commit 6241c87
Showing
18 changed files
with
567 additions
and
271 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
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,26 +1,91 @@ | ||
const passport = require("passport"); | ||
const LocalStrategy = require("passport-local").Strategy; | ||
const mongoose = require("mongoose"); | ||
const User = mongoose.model("User"); | ||
import passport from 'passport'; | ||
import passportGoogle from 'passport-google-oauth20'; | ||
import passportTwitter from 'passport-twitter'; | ||
import passportFacebook from 'passport-facebook'; | ||
import dotenv from 'dotenv'; | ||
import db from '../database/models'; | ||
|
||
const { User } = db; | ||
|
||
const GoogleStrategy = passportGoogle.Strategy; | ||
const TwitterStrategy = passportTwitter.Strategy; | ||
const FacebookStrategy = passportFacebook.Strategy; | ||
|
||
dotenv.config(); | ||
|
||
let trustProxy = false; | ||
|
||
if (process.env.DYNO) { | ||
trustProxy = true; | ||
} | ||
|
||
const googleClientId = process.env.GOOGLE_CLIENT_ID; | ||
const googleClientSecret = process.env.GOOGLE_CLIENT_SECRET; | ||
const googleReturnUrl = process.env.GOOGLE_RETURN_URL; | ||
const twitterConsumerKey = process.env.TWITTER_CONSUMER_KEY; | ||
const twitterConsumerSecret = process.env.TWITTER_CONSUMER_SECRET; | ||
const twitterReturnUrl = process.env.TWITTER_RETURN_URL; | ||
const facebookAppId = process.env.FACEBOOK_APP_ID; | ||
const facebookAppSecret = process.env.FACEBOOK_APP_SECRET; | ||
const facebookReturnUrl = process.env.FACEBOOK_RETURN_URL; | ||
|
||
const handleSocialLogin = async (email, firstname, lastname, username, photo, cb) => { | ||
try{ | ||
const existingUser = await User.findOne({ where: { email } }) | ||
return cb(null, { | ||
data: existingUser.dataValues | ||
}); | ||
} | ||
catch{ | ||
const user = await User.create({ | ||
email, | ||
firstname, | ||
lastname, | ||
username: username ? `${username}-${new Date().getTime()}` : `${firstname ? firstname.toLowerCase() : email}${lastname ? lastname.toLowerCase() : ''}-${new Date().getTime()}`, | ||
image: photo, | ||
}) | ||
return cb(null, { data: user.dataValues }) | ||
} | ||
}; | ||
|
||
passport.use( | ||
new LocalStrategy( | ||
{ | ||
usernameField: "user[email]", | ||
passwordField: "user[password]" | ||
}, | ||
function(email, password, done) { | ||
User.findOne({ email: email }) | ||
.then(function(user) { | ||
if (!user || !user.validPassword(password)) { | ||
return done(null, false, { | ||
errors: { "email or password": "is invalid" } | ||
}); | ||
} | ||
|
||
return done(null, user); | ||
}) | ||
.catch(done); | ||
} | ||
) | ||
new GoogleStrategy({ | ||
clientID: googleClientId, | ||
clientSecret: googleClientSecret, | ||
callbackURL: googleReturnUrl | ||
}, (accessToken, refreshToken, profile, cb) => { | ||
const { name, emails, photos } = profile; | ||
handleSocialLogin(emails[0].value, name.givenName, name.familyName, null, photos[0].value, cb); | ||
}) | ||
); | ||
|
||
passport.use(new TwitterStrategy({ | ||
consumerKey: twitterConsumerKey, | ||
consumerSecret: twitterConsumerSecret, | ||
callbackURL: twitterReturnUrl, | ||
userProfileURL: 'https://api.twitter.com/1.1/account/verify_credentials.json?include_email=true', | ||
includeEmail: true, | ||
proxy: trustProxy | ||
}, | ||
(token, tokenSecret, profile, cb) => { | ||
const { username, emails, photos } = profile; | ||
handleSocialLogin(emails[0].value, null, null, username, photos[0].value, cb); | ||
}) | ||
); | ||
|
||
passport.use(new FacebookStrategy({ | ||
clientID: facebookAppId, | ||
clientSecret: facebookAppSecret, | ||
callbackURL: facebookReturnUrl, | ||
profileFields: ['id', 'displayName', 'photos', 'email'] | ||
}, | ||
(accessToken, refreshToken, profile, cb) => { | ||
const { displayName, photos, emails } = profile; | ||
const splitnames = displayName.split(' '); | ||
const firstname = splitnames[0]; | ||
const lastname = splitnames.length > 1 ? splitnames[1] : ''; | ||
handleSocialLogin(emails[0].value, firstname, lastname, null, photos[0].value, cb); | ||
}) | ||
); | ||
|
||
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
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,28 @@ | ||
export default { | ||
up(queryInterface, Sequelize) { | ||
return queryInterface.createTable('Tags', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
name: { | ||
type: Sequelize.STRING | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE, | ||
defaultValue: new Date().getTime(), | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE, | ||
defaultValue: new Date().getTime(), | ||
} | ||
}); | ||
}, | ||
down(queryInterface, Sequelize) { | ||
return queryInterface.dropTable('Tags'); | ||
} | ||
}; |
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,13 @@ | ||
export default (sequelize, DataTypes) => { | ||
const Tag = sequelize.define('Tag', { | ||
name: DataTypes.STRING | ||
}, {}); | ||
Tag.associate = (models) => { | ||
// associations can be defined here | ||
Tag.hasMany(models.Article, { | ||
foreingKey: 'tagId', | ||
as: 'articles' | ||
}); | ||
}; | ||
return Tag; | ||
}; |
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,19 @@ | ||
export default { | ||
up(queryInterface, Sequelize) { | ||
return queryInterface.bulkInsert('Tags', [{ | ||
name: 'Food' | ||
}, { | ||
name: 'Technology' | ||
}, { | ||
name: 'Art' | ||
}, { | ||
name: 'Finance' | ||
}, { | ||
name: 'Health' | ||
}], {}); | ||
}, | ||
|
||
down(queryInterface, Sequelize) { | ||
return queryInterface.bulkDelete('Tags', null, {}); | ||
} | ||
}; |
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.