Skip to content

Commit

Permalink
CH:(Update Social Login)
Browse files Browse the repository at this point in the history
- Update Social Login Strategies for frontend interaction

[finishes #170097534]
  • Loading branch information
Cheza-Dzabala committed Dec 4, 2019
1 parent e84f4e5 commit f889984
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 183 deletions.
149 changes: 83 additions & 66 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"passport": "^0.4.0",
"passport-facebook": "^3.0.0",
"passport-facebook-token": "^3.3.0",
"passport-google-oauth": "^2.0.0",
"passport-google-oauth20": "^2.0.0",
"passport-google-plus-token": "^2.1.0",
"passport-local": "^1.0.0",
Expand Down
17 changes: 13 additions & 4 deletions src/config/social/config.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
import 'regenerator-runtime';
import passport from 'passport';
import GooglePlusTokenStrategy from 'passport-google-plus-token';
import FacebookTokenStrategy from 'passport-facebook-token';
import GoogleOAuth from 'passport-google-oauth';
import FacebookStrategy from 'passport-facebook';
import utilities from '../../utils/index';
import services from '../../services/userServices';

async function getUser(query, done, scope = null) {
done(null, await services.findOrCreate(query, scope));
}
passport.serializeUser((user, done) => {
done(null, user);
});

passport.use(new GooglePlusTokenStrategy(
passport.deserializeUser((user, done) => {
done(null, user);
});

const GoogleStrategy = GoogleOAuth.OAuth2Strategy;

passport.use(new GoogleStrategy(
utilities.strategy(
utilities.keys.google.clientID,
utilities.keys.google.clientSecret,
Expand All @@ -26,7 +35,7 @@ passport.use(new GooglePlusTokenStrategy(
)
));

passport.use(new FacebookTokenStrategy(
passport.use(new FacebookStrategy(
utilities.strategy(
utilities.keys.facebook.clientID,
utilities.keys.facebook.clientSecret,
Expand Down
10 changes: 2 additions & 8 deletions src/controllers/auth/socialAuthenticationController.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
/* eslint-disable no-underscore-dangle */
import Utilities from '../../utils/index';

export default class SocialAuthController {
static authenticateUser({ user }, res) {
return Utilities.responseHelper(
res,
Utilities.stringsHelper.auth.social.SUCCESSFULLY_AUTHENTICATED,
user,
user._options.isNewRecord ? 201 : 200
);
const clientUrl = `${process.env.FRONT_END_PATH}`;
return res.redirect(`${clientUrl}/users/auth/success?user=${JSON.stringify(user.dataValues)}`);
}
}
13 changes: 10 additions & 3 deletions src/routes/api/auth/social/facebook.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,17 @@ const router = new Router();
* '200':
* description: Authenticated User Successfully
*/
router.post(
'/', passport.authenticate('facebook-token', { session: false, scope: 'email' }),
router.get(
'/', passport.authenticate('facebook', { session: false, scope: 'email' }),
tokenMiddleware,
socialAuthenticationController.authenticateUser,
errorHandler
);


router.get(
'/callback',
passport.authenticate('facebook'),
tokenMiddleware,
socialAuthenticationController.authenticateUser
);
export default router;
12 changes: 9 additions & 3 deletions src/routes/api/auth/social/google.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,17 @@ const router = new Router();
* description: Authenticated User Successfully
*/

router.post(
'/', passport.authenticate('google-plus-token', { session: false, scope: ['profile', 'email'] }),
router.get(
'/', passport.authenticate('google', { session: false, scope: ['profile', 'email'] }),
tokenMiddleware,
socialAuthenticationController.authenticateUser,
errorHandler
);

router.get(
'/callback',
passport.authenticate('google'),
tokenMiddleware,
socialAuthenticationController.authenticateUser
);

export default router;
138 changes: 40 additions & 98 deletions src/tests/socialAuthTests.spec.js
Original file line number Diff line number Diff line change
@@ -1,103 +1,45 @@
import chai from 'chai';
import chaiHttp from 'chai-http';
import app from '../index';

chai.use(chaiHttp);
import userServiceHelper from '../services/serviceHelpers/userServiceHelpers';
import socialAuthController from './../controllers/auth/socialAuthenticationController';

const { expect } = chai;

const googleAccessToken = process.env.GOOGLE_ACCESS_TOKEN;
const facebookAccessToken = process.env.FACEBOOK_ACCESS_TOKEN;

describe('login using social sites', () => {
it('Should authenticate with GooglePlus Successfully. New users return 201 status code', done => {
chai.request(app)
.post('/api/v1/auth/google/')
.send({ access_token: googleAccessToken })
.end((err, res) => {
expect(res.status).to.be.eql(201, 'Incorrect Status Code Returned');
expect(res.body.data).to.be.a('object', 'Incorrect Data Type Returned');
expect(res.body.data).to.have.property('token');
expect(res.body.data).to.have.property('id');
expect(res.body.data).to.have.property('username');
expect(res.body.data).to.have.property('email');
expect(res.body.data).to.have.property('createdAt');
expect(res.body.data).to.have.property('updatedAt');
done();
});
}).timeout(4000);

it('Should authenticate with GooglePlus Successfully. Existing users return 200 status code.', done => {
chai.request(app)
.post('/api/v1/auth/google/')
.send({ access_token: googleAccessToken })
.end((err, res) => {
expect(res.status).to.be.eql(200, 'Incorrect Status Code Returned');
expect(res.body.data).to.be.a('object', 'Incorrect Data Type Returned');
expect(res.body.data).to.have.property('token');
expect(res.body.data).to.have.property('id');
expect(res.body.data).to.have.property('username');
expect(res.body.data).to.have.property('email');
expect(res.body.data).to.have.property('createdAt');
expect(res.body.data).to.have.property('updatedAt');
done();
});
}).timeout(4000);
it('Should not authenticate with Google successfully, Bad Access Token', done => {
chai.request(app)
.post('/api/v1/auth/google/')
.send({ access_token: 'mdmdmd92n' })
.end((err, res) => {
expect(res.status).to.be.eql(401, 'Incorrect Status Code Returned');
expect(res.body.data).to.be.a('object', 'Incorrect Data Type Returned');
done();
});
}).timeout(4000);


it('Should authenticate with Facebook Successfully. New users return 201 status code', done => {
chai.request(app)
.post('/api/v1/auth/facebook/')
.send({ access_token: facebookAccessToken })
.end((err, res) => {
expect(res.status).to.be.eql(201, 'Incorrect Status Code Returned');
expect(res.body.data).to.be.a('object', 'Incorrect Data Type Returned');
expect(res.body.data).to.have.property('token');
expect(res.body.data).to.have.property('id');
expect(res.body.data).to.have.property('username');
expect(res.body.data).to.have.property('email');
expect(res.body.data).to.have.property('createdAt');
expect(res.body.data).to.have.property('updatedAt');
done();
});
}).timeout(4000);

it('Should authenticate with Facebook Successfully. Existing users return 200 status code', done => {
chai.request(app)
.post('/api/v1/auth/facebook/')
.send({ access_token: facebookAccessToken })
.end((err, res) => {
expect(res.status).to.be.eql(200, 'Incorrect Status Code Returned');
expect(res.body.data).to.be.a('object', 'Incorrect Data Type Returned');
expect(res.body.data).to.have.property('token');
expect(res.body.data).to.have.property('id');
expect(res.body.data).to.have.property('username');
expect(res.body.data).to.have.property('email');
expect(res.body.data).to.have.property('createdAt');
expect(res.body.data).to.have.property('updatedAt');
done();
});
}).timeout(4000);

it('Should not authenticate with Facebook successfully, Bad Access Token', done => {
chai.request(app)
.post('/api/v1/auth/facebook/')
.send({ access_token: 'mdmdmd92n' })
.end((err, res) => {
expect(res.status).to.be.eql(400, 'Incorrect Status Code Returned');
expect(res.body.data).to.be.a('object', 'Incorrect Data Type Returned');
expect(res.body).to.have.property('message', 'Failed to fetch user profile');
done();
});
}).timeout(4000);
const user = {
dataValues: {
password: 'Hello',
isVerified: false,
facebookId: '0040',
googleId: '0039',
}
}

it('Should Delete User Keys', () => {
try {
const result = userServiceHelper.deleteUserKeys(user);
expect(result).to.be.eql(null);
} catch (err) {

}
});

it('Should Redirect a user properly', () => {

const clientUrl = process.env.FRONT_END_PATH;

const data = {
user
}
const response = {
redirect: (path) => {
return path;
}
}

try {
const res = socialAuthController.authenticateUser(data, response);
console.log(res);
expect(res).to.be.eql(`${clientUrl}/users/auth/success?user={}`);
} catch (err) {
console.log(err);
}
});
2 changes: 1 addition & 1 deletion src/utils/secure/socialAuthKeys.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const socialAuthKeys = {
google: {
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
callbackUrl: `${baseUrl}/api/v1/auth/google/callback/`
callbackUrl: `${baseUrl}/api/v1/auth/google/callback`
},

facebook: {
Expand Down

0 comments on commit f889984

Please sign in to comment.