-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable Social Login using facebook [Delivers #167574878]
- Loading branch information
Adekoreday
committed
Aug 5, 2019
1 parent
871972a
commit dcf9ec5
Showing
21 changed files
with
511 additions
and
13 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,41 @@ | ||
import { | ||
getUserAgent, | ||
createSocialUser, | ||
serverResponse, | ||
getSocialUserData | ||
} from '../helpers'; | ||
/** | ||
* @export | ||
* @class Auth | ||
*/ | ||
class Auth { | ||
/** | ||
* @name SocialLogin | ||
* @async | ||
* @static | ||
* @memberof Auth | ||
* @param {Object} request express request object | ||
* @param {Object} response express response object | ||
* @returns {JSON} JSON object with details of new user | ||
*/ | ||
static async SocialLogin(request, response) { | ||
const { devicePlatform, userAgent } = getUserAgent(request); | ||
const { ip } = request; | ||
const { givenName, familyName, email } = getSocialUserData( | ||
request, | ||
'facebook' | ||
); | ||
const data = { | ||
firstName: givenName, | ||
lastName: familyName, | ||
email, | ||
devicePlatform, | ||
userAgent, | ||
ipAddress: ip | ||
}; | ||
const Data = await createSocialUser(data); | ||
delete Data.password; | ||
serverResponse(response, 200, Data); | ||
} | ||
} | ||
export default Auth; |
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,45 @@ | ||
import models from '../database/models'; | ||
import generateToken from './generateToken'; | ||
import dateHelper from './dateHelper'; | ||
|
||
const { User, Session } = models; | ||
const { expiryDate } = dateHelper; | ||
/** | ||
* @name createSocialUser | ||
* @description function that creates a new user in via socialLogin | ||
* @param { Object } data to check in the database | ||
* @returns { Object } value indicating if username exists | ||
*/ | ||
const createSocialUser = async (data) => { | ||
const { | ||
firstName, | ||
lastName, | ||
email, | ||
ipAddress, | ||
userAgent, | ||
devicePlatform | ||
} = data; | ||
const expiresAt = expiryDate(devicePlatform); | ||
const users = await User.findOrCreate({ | ||
where: { email }, | ||
defaults: { | ||
firstName, | ||
lastName, | ||
} | ||
}); | ||
const result = { ...users[0].dataValues }; | ||
const { id } = result; | ||
const token = generateToken(id); | ||
await Session.create({ | ||
userId: id, | ||
token, | ||
expiresAt, | ||
userAgent, | ||
ipAddress, | ||
devicePlatform | ||
}); | ||
result.token = token; | ||
return result; | ||
}; | ||
export default createSocialUser; |
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,17 @@ | ||
import passport from 'passport'; | ||
import { Strategy as FacebookStrategy } from 'passport-facebook'; | ||
import callback from '../middlewares/passportCallback'; | ||
|
||
export default () => { | ||
passport.use( | ||
new FacebookStrategy( | ||
{ | ||
clientID: process.env.FACEBOOK_APP_ID, | ||
clientSecret: process.env.FACEBOOK_APP_SECRET, | ||
callbackURL: process.env.REDIRECT_URL, | ||
profileFields: ['id', 'first_name', 'last_name', 'photos', 'email'] | ||
}, | ||
callback | ||
) | ||
); | ||
}; |
Oops, something went wrong.