-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(social-login): enable social login via existing accounts
- add facebook login with passport - add google login with passport - add twitter login with passport [Finishes #166789873]
- Loading branch information
Karangwa Hirwa Julien
authored and
Karangwa Hirwa Julien
committed
Jul 16, 2019
1 parent
d0c2a88
commit 1984b64
Showing
16 changed files
with
1,089 additions
and
604 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,20 +1,33 @@ | ||
DATABASE_USERNAME_DEV="your-database-username" | ||
DATABASE_PASSWORD="your-database-password" | ||
DATABASE_USERNAME_DEV="your_dev_db_username" | ||
DATABASE_URL="" | ||
DATABASE_PASSWORD="your_dev_db_password" | ||
|
||
DATABASE_USERNAME_TEST="your-database-username-for-test" | ||
DATABASE_PASSWORD_TEST="your-postgres-password-test" | ||
HEROKU_DATABASE_USERNAME="jgxfkdjw" | ||
HEROKU_SECRET_KEY="LmbmTy8mqqZEAsmDfKrugdecYNqzeBJ1" | ||
HEROKU_HOST="raja.db.elephantsql.com" | ||
HEROKU_DATABASE="jgxfkdjw" | ||
|
||
SENDGRID_API_KEY="insert your send grid token" | ||
SECRET_JWT_KEY="your-own-secret-key" | ||
SECRET_KEY="your-database-password" | ||
SENDGRID_API_KEY="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" | ||
HOST="https://ah-lobos-backend-swagger.herokuapp.com" | ||
DATABASE_PASSWORD_TEST="your_test_db_password" | ||
DATABASE_USERNAME_TEST="your_test_db_username" | ||
SECRET_JWT_KEY="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6IkthcmFuZ3dhIEhpcndhIEp1bGllbiIsImVtYWlsIjoianVsaXVzaGlyd2FAZ21haWwuY29tIiwiaWF0IjoxNTYyMzA3NDEzLCJleHAiOjE1NjIzOTM4MTN9.vooQWDbNtRRMx4LVZM7n6O2rrUjmfGhWfWnOuppxj9I" | ||
EMAIL="your_email" | ||
PASSWORD="your_password" | ||
SECRET="your_secret" | ||
|
||
HEROKU_DATABASE_USERNAME="heroku-database-username" | ||
HEROKU_SECRET_KEY="heroku-database-password" | ||
HEROKU_HOST="heroku-host" | ||
HEROKU_DATABASE="heroku-database-name" | ||
|
||
HOST="127.0.0.1:7000" | ||
GMAIL_APP_ID="207391721395-nr9q4f02giavmn6bj91lgosf6ordht9h.apps.googleusercontent.com" | ||
GMAIL_APP_SECRET="nfjp3BycPRO-kmExVAMSZjqu" | ||
FACEBOOK_APP_ID="1245296778980169" | ||
FACEBOOK_APP_SECRET="e735b5c122dbaa24dd5d71c20b55062f" | ||
SECRET="23G4HJ5K6L9876K5JH4G" | ||
TWITTER_APP_ID="fuwF7UbQaKoJqI0KR68Yaqbyj" | ||
TWITTER_APP_SECRET="4e6IHRuSdKfyMeEgQNAfsla3oQNXumsBKCwEMcJcsjPtRfBJhA" | ||
TWITTER_ACCESS_TOKEN="1063823892-tZLv5zh2kwpWZoEPRZrrXRVD3EvMUsO0VqB7DVS" | ||
TWITTER_ACCESS_SECRET="cCJmqIE7Gf2xuttD37A4uzdn1GRGuDTVjrb2goPyJre4j" | ||
CALLBACK="http://127.0.0.1:7000/auth/twitter/callback" | ||
|
||
CLOUDINARY_CLOUD_NAME="username" | ||
CLOUDINARY_API_KEY="api-key" | ||
CLOUDINARY_API_SECRET="api-secret" | ||
|
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,117 @@ | ||
import dotenv from 'dotenv'; | ||
import model from '../db/models'; | ||
import processToken from '../helpers/processToken'; | ||
|
||
const { Users } = model; | ||
|
||
dotenv.config(); | ||
|
||
/** | ||
* User Information are saved here | ||
*/ | ||
class UserInfo { | ||
/** | ||
* | ||
* @param {Object} req | ||
* @param {Object} res | ||
* @returns {Object} response after user signin | ||
*/ | ||
static async googleLogin(req, res) { | ||
const { displayName } = req.user; | ||
const newUser = await Users.create({ | ||
username: displayName, | ||
email: req.user.emails[0].value, | ||
image: req.user.photos[0].value, | ||
isVerified: req.user.emails[0].verified, | ||
socialId: req.user.id, | ||
provider: 'google', | ||
hash: process.env.DEFAULT_PASSWORD | ||
}); | ||
if (newUser) { | ||
const { | ||
dataValues: { | ||
id, username, email | ||
} | ||
} = newUser; | ||
const token = await processToken.signToken(newUser.dataValues); | ||
return res.status(200).json({ | ||
message: `Welcome to Authors Haven ${displayName} `, | ||
data: { | ||
token, id, username, email | ||
}, | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* @param {Object} req | ||
* @param {Object} res | ||
* @returns {Object} user successfully logged in | ||
*/ | ||
static async facebookLogin(req, res) { | ||
const { displayName } = req.user; | ||
const newUser = await Users.create({ | ||
username: displayName, | ||
email: req.user.emails[0].value, | ||
image: req.user.photos[0].value, | ||
isVerified: true, | ||
socialId: req.user.id, | ||
provider: 'facebook', | ||
hash: process.env.DEFAULT_PASSWORD | ||
}); | ||
if (newUser) { | ||
const { | ||
dataValues: { | ||
id, username, email | ||
} | ||
} = newUser; | ||
const token = await processToken.signToken(newUser.dataValues); | ||
return res.status(200).json({ | ||
message: `Welcome to Authors Haven ${displayName} `, | ||
data: { | ||
token, id, username, email | ||
}, | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* @param {Object} req | ||
* @param {Object} res | ||
* @return {Object} user logged in | ||
*/ | ||
static async twitterLogin(req, res) { | ||
const { | ||
displayName | ||
} = req.user; | ||
|
||
// eslint-disable-next-line no-unused-expressions | ||
const newUser = await Users.create({ | ||
username: req.user.username, | ||
email: `${req.user.username}@gmail.com`, | ||
image: req.user.photos[0].value, | ||
isVerified: true, | ||
socialId: req.user.id, | ||
provider: 'twitter', | ||
hash: process.env.DEFAULT_PASSWORD | ||
}); | ||
if (newUser) { | ||
const { | ||
dataValues: { | ||
id, username | ||
} | ||
} = newUser; | ||
const token = await processToken.signToken(newUser.dataValues); | ||
return res.status(200).json({ | ||
message: `Welcome to Authors Haven ${displayName} `, | ||
data: { | ||
token, id, username | ||
}, | ||
}); | ||
} | ||
} | ||
} | ||
|
||
export default UserInfo; |
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,23 @@ | ||
import passport from 'passport'; | ||
import FacebookStrategy from 'passport-facebook'; | ||
import config from '../envirnoment'; | ||
|
||
passport.use(new FacebookStrategy({ | ||
clientID: config.facebook_app_id, | ||
clientSecret: config.facebook_app_secret, | ||
callbackURL:`https://ah-lobos-backend-swagger.herokuapp.com/auth/facebook/callback`, | ||
profileFields: ['id', 'displayName', 'photos', 'email'] | ||
}, | ||
(accessToken, refreshToken, profile, done) => { | ||
done(null, profile); | ||
})); | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import passport from 'passport'; | ||
import GoogleStrategy from 'passport-google-oauth20'; | ||
import config from '../envirnoment'; | ||
|
||
passport.use(new GoogleStrategy({ | ||
clientID: config.google_app_id, | ||
clientSecret: config.google_app_secret, | ||
includeEmail: true, | ||
callbackURL: `${config.host}/auth/google/callback` | ||
}, | ||
(accessToken, refreshToken, profile, done) => { | ||
done(null, profile); | ||
} | ||
)); | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import passport from 'passport'; | ||
import TwitterTokenStrategy from 'passport-twitter'; | ||
import config from '../envirnoment'; | ||
|
||
passport.use(new TwitterTokenStrategy({ | ||
consumerKey: config.twitter_app_id, | ||
consumerSecret: config.twitter_app_secret, | ||
callbackURL: config.twitter_callback | ||
}, | ||
(token, tokenSecret, profile, done) => { | ||
done(null, profile); | ||
} | ||
)); | ||
|
||
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
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 @@ | ||
export default (req, res, next) => { | ||
req.user = { | ||
id: req.body.id, | ||
displayName: 'Karangwa Hirwa Julien', | ||
name: { familyName: 'Karangwa Hirwa', givenName: 'Julien' }, | ||
emails: | ||
[{ value: req.body.email, verified: true }], | ||
photos: | ||
[{ | ||
value: | ||
'https://lh4.googleusercontent.com/-FZSypt5JyRU/AAAAAAAAAAI/AAAAAAAAAAA/ACHi3repeJYC3C7JQWReWg7zqfLcOxh0Qg/mo/photo.jpg' | ||
}], | ||
provider: 'google' | ||
}; | ||
next(); | ||
}; |
Oops, something went wrong.