Skip to content

Commit

Permalink
feautre(FacebookSocialLogin):
Browse files Browse the repository at this point in the history
Enable Social Login using facebook

[Delivers #167574878]
  • Loading branch information
Adekoreday committed Aug 5, 2019
1 parent 871972a commit dcf9ec5
Show file tree
Hide file tree
Showing 21 changed files with 511 additions and 13 deletions.
4 changes: 4 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ DB_URL_PRODUCTION=postgres://authors:haven@localhost:2800/haven
NODE_ENV=development
BASE_URL=/api/v5
JWT_KEY=passkey
REDIRECT_URL=http://localhost:5000/api/v1/auth/facebook/callback
FACEBOOK_APP_ID=4342d2e47a9265845478yuy343y
FACEBOOK_APP_SECRET=5y4384y545510
SECRET_SESSION=secret-cat
89 changes: 86 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1",
"morgan": "^1.9.1",
"passport": "^0.4.0",
"passport-facebook": "^3.0.0",
"pg": "^7.11.0",
"pg-hstore": "^2.3.3",
"sequelize": "^5.10.2",
Expand Down Expand Up @@ -60,6 +62,7 @@
"lint-staged": "^9.2.1",
"mocha": "^6.1.4",
"mocha-lcov-reporter": "^1.3.0",
"nock": "^10.0.6",
"nodemon": "^1.19.1",
"nyc": "^14.1.1",
"prettier-eslint-cli": "^5.0.0",
Expand Down
41 changes: 41 additions & 0 deletions server/controllers/Auth.js
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;
64 changes: 63 additions & 1 deletion server/docs/authors-haven-api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,30 @@ paths:
application/json:
schema:
"$ref": "#/components/schemas/serverResponse"

/api/v1/auth/facebook:
get:
summary: Route for signing new using facebook
description: Allow new user to Login using facebook
responses:
302:
description: redirect url
/api/v1/auth/facebook/callback:
get:
summary: redirect url for facebook login/signup
description: Allow existing users to signup or login
responses:
200:
description: Login successful
content:
application/json:
schema:
"$ref": "#/components/FacebookUserSignUp"
400:
description: Bad request
content:
application/json:
schema:
"$ref": "#/components/schemas/fbserverResponse"
components:
securitySchemes:
BearerAuth:
Expand Down Expand Up @@ -130,6 +153,40 @@ components:
confirmPassword:
type: string
example: incorrect
FacebookUserSignUp:
type: object
required:
- firstName
- lastName
- userName
- email
- password
- token
properties:
id:
type: string
example: 1
firstName:
type: string
example: Adekay
lastName:
type: string
example: Ade
userName:
type: string
example: korey
email:
type: string
example: riri.adeyemo@andela.com
password:
type: string
example: incorrect
confirmPassword:
type: string
example: incorrect
token:
type: string
example: bhddmdmddddddddddddmddnddb
schemas:
loginResponse:
type: object
Expand All @@ -148,3 +205,8 @@ components:
properties:
error:
type: string
fbserverResponse:
type: object
properties:
message:
type: string
45 changes: 45 additions & 0 deletions server/helpers/createSocialUsers.js
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,
email
}
});
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;
17 changes: 17 additions & 0 deletions server/helpers/facebook.js
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
)
);
};
Loading

0 comments on commit dcf9ec5

Please sign in to comment.