Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#167574878 Facebook Social Login #17

Merged
merged 1 commit into from
Aug 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ 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
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,
createSocialUsers,
serverResponse,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'serverResponse' is defined but never used. Allowed unused vars must match /should|expect/ no-unused-vars

getSocialUserData
} from '../helpers';
/**
* @export
* @class Auth
*/
class Auth {
/**
* @name facebookSocialLogin
* @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 facebookSocialLogin(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 user = await createSocialUsers(data);
delete user.password;
serverResponse(response, 200, user);
}
}
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';
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parsing error: 'import' and 'export' may appear only with 'sourceType: module'

import { Strategy as FacebookStrategy } from 'passport-facebook';
import passportCallback from '../middlewares/passportCallback';

export default () => {
passport.use(
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unexpected newline after '(' function-paren-newline

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']
},
passportCallback
)
);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unexpected newline before ')' function-paren-newline

};
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.