-
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
Mireille Niwemuhuza
authored and
Mireille Niwemuhuza
committed
Jun 16, 2019
1 parent
9b39eea
commit e2e39ba
Showing
16 changed files
with
434 additions
and
56 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import models from '../../sequelize/models'; | ||
import tokenGeneration from '../../helpers/Token.helper'; | ||
|
||
const userInfo = { | ||
async googleLogin(req, res) { | ||
const { displayName } = req.user; | ||
const newUser = await models.User.create({ | ||
firstName: req.user.name.givenName, | ||
lastName: req.user.name.familyName, | ||
email: req.user.emails[0].value, | ||
image: req.user.photos[0].value, | ||
provider: req.user.provider, | ||
verified: req.user.emails[0].verified, | ||
socialId: req.user.id, | ||
}); | ||
if (newUser) { | ||
const { | ||
id, firstName, lastName, email, provider | ||
} = newUser.dataValues; | ||
const token = await tokenGeneration.generateToken(newUser.dataValues); | ||
return res.status(201).json({ | ||
status: 201, | ||
message: `Welcome to Authors Haven ${displayName} `, | ||
data: { | ||
token, id, firstName, lastName, email, provider | ||
}, | ||
}); | ||
} | ||
}, | ||
|
||
async facebookLogin(req, res) { | ||
const { displayName } = req.user; | ||
const names = displayName.split(' '); | ||
const newUser = await models.User.create({ | ||
firstName: names[0], | ||
lastName: names[1], | ||
email: req.user.emails[0].value, | ||
image: req.user.photos[0].value, | ||
provider: req.user.provider, | ||
verified: true, | ||
socialId: req.user.id, | ||
}); | ||
if (newUser) { | ||
const { | ||
id, firstName, lastName, email, provider | ||
} = newUser.dataValues; | ||
const token = await tokenGeneration.generateToken(newUser.dataValues); | ||
return res.status(201).json({ | ||
message: `Welcome to Authors Haven ${displayName} `, | ||
data: { | ||
token, id, firstName, lastName, email, provider | ||
}, | ||
}); | ||
} | ||
}, | ||
async twitterLogin(req, res) { | ||
const { | ||
displayName | ||
} = req.user; | ||
const names = displayName.split(' '); | ||
const newUser = await models.User.create({ | ||
firstName: names[0], | ||
lastName: names[1], | ||
username: req.user.username, | ||
image: req.user.photos[0].value, | ||
provider: req.user.provider, | ||
verified: true, | ||
socialId: req.user.id, | ||
}); | ||
if (newUser) { | ||
const { | ||
id, firstName, lastName, email, provider | ||
} = newUser.dataValues; | ||
const token = await tokenGeneration.generateToken(newUser.dataValues); | ||
return res.status(201).json({ | ||
message: `Welcome to Authors Haven ${displayName} `, | ||
data: { | ||
token, id, firstName, lastName, email, provider | ||
}, | ||
}); | ||
} | ||
}, | ||
}; | ||
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
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,40 @@ | ||
import passport from 'passport'; | ||
import GoogleStrategy from 'passport-google-oauth20'; | ||
import FacebookStrategy from 'passport-facebook'; | ||
import TwitterTokenStrategy from 'passport-twitter'; | ||
import dotenv from 'dotenv'; | ||
|
||
dotenv.config(); | ||
|
||
passport.use(new GoogleStrategy( | ||
{ | ||
clientID: process.env.GOOGLE_ID, | ||
clientSecret: process.env.GOOGLE_SECRET, | ||
includeEmail: true, | ||
callbackURL: '/api/auth/login/google/redirect' | ||
}, | ||
(accessToken, refreshToken, profile, done) => { | ||
done(null, profile); | ||
} | ||
),); | ||
passport.use(new FacebookStrategy( | ||
{ | ||
clientID: process.env.FCBK_ID, | ||
clientSecret: process.env.FCBK_APP_SECRET, | ||
callbackURL: '/api/auth/login/facebook/redirect', | ||
profileFields: ['id', 'displayName', 'photos', 'email'] | ||
}, | ||
(accessToken, refreshToken, profile, done) => { | ||
done(null, profile); | ||
} | ||
),); | ||
passport.use(new TwitterTokenStrategy( | ||
{ | ||
consumerKey: process.env.TWITTER_ID, | ||
consumerSecret: process.env.TWITTER_APP_SECRET, | ||
callbackURL: 'http://localhost:3001/api/auth/login/twitter/redirect' | ||
}, | ||
(token, tokenSecret, profile, done) => { | ||
done(null, profile); | ||
} | ||
)); |
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,6 @@ | ||
import models from '../sequelize/models'; | ||
|
||
models.User.destroy({ | ||
where: {}, | ||
truncate: false | ||
}); |
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,59 @@ | ||
import models from '../sequelize/models'; | ||
import tokenGeneration from '../helpers/Token.helper'; | ||
|
||
const userExists = { | ||
async google(req, res, next) { | ||
const { emails, displayName } = req.user; | ||
const currentUser = await models.User.findAll({ | ||
where: { | ||
email: emails[0].value, | ||
}, | ||
}); | ||
if (currentUser.length > 0) { | ||
const token = await tokenGeneration.generateToken(currentUser[0].dataValues); | ||
const { | ||
id, firstName, lastName, email, socialId, provider | ||
} = currentUser[0].dataValues; | ||
return res.status(200).json({ | ||
message: `Welcome to Authors Haven ${displayName} `, | ||
data: { | ||
token, | ||
id, | ||
firstName, | ||
lastName, | ||
email, | ||
socialId, | ||
provider | ||
}, | ||
}); | ||
} | ||
next(); | ||
}, | ||
async twitter(req, res, next) { | ||
const { displayName } = req.user; | ||
const currentUser = await models.User.findAll({ | ||
where: { | ||
socialId: req.user.id, | ||
}, | ||
}); | ||
if (currentUser.length > 0) { | ||
const token = await tokenGeneration.generateToken(currentUser[0].dataValues); | ||
const { | ||
id, firstName, lastName, socialId, provider | ||
} = currentUser[0].dataValues; | ||
return res.status(200).json({ | ||
message: `Welcome to Authors Haven ${displayName} `, | ||
data: { | ||
token, | ||
id, | ||
firstName, | ||
lastName, | ||
socialId, | ||
provider, | ||
}, | ||
}); | ||
} | ||
next(); | ||
}, | ||
}; | ||
export default userExists; |
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: 'Mireille Niwemuhuza', | ||
name: { familyName: 'Niwemuhuza', givenName: 'Mireille' }, | ||
emails: | ||
[{ value: req.body.email, verified: true }], | ||
photos: | ||
[{ | ||
value: | ||
'https://lh4.googleusercontent.com/-FZSypt5JyRU/AAAAAAAAAAI/AAAAAAAAAAA/ACHi3repeJYC3C7JQWReWg7zqfLcOxh0Qg/mo/photo.jpg' | ||
}], | ||
provider: 'google' | ||
}; | ||
next(); | ||
}; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.