Skip to content

Commit

Permalink
Merge 6a7b367 into 8759dab
Browse files Browse the repository at this point in the history
  • Loading branch information
d-beloved committed Oct 1, 2018
2 parents 8759dab + 6a7b367 commit 952365e
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 42 deletions.
1 change: 0 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ if (!isProduction) {
}
app.use(router);


// catch 404 and forward to error handler
app.use((req, res, next) => {
const err = new Error('Not Found');
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
"errorhandler": "^1.5.0",
"express": "^4.16.3",
"express-jwt": "^5.3.1",
"express-session": "^1.15.6",
"fancy-log": "^1.3.2",
"jsonwebtoken": "^8.3.0",
"method-override": "^2.3.10",
Expand All @@ -45,7 +44,6 @@
"passport-google-oauth20": "^1.0.0",
"passport-jwt": "^4.0.0",
"passport-local": "^1.0.0",
"passport-twitter": "^1.0.4",
"pg": "^7.4.3",
"pg-hstore": "^2.3.2",
"pug": "^2.0.3",
Expand Down Expand Up @@ -73,6 +71,7 @@
"istanbul": "^0.4.5",
"mocha": "^5.2.0",
"mocha-lcov-reporter": "^1.3.0",
"nock": "^10.0.0",
"nodemon": "^1.18.4",
"nyc": "^13.0.1"
}
Expand Down
18 changes: 9 additions & 9 deletions server/config/passport.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import passport from 'passport';
import { Strategy as FacebookStrategy } from 'passport-facebook';
import { Strategy as GoogleStrategy } from 'passport-google-oauth20';
import configAuth from './socialConfig';
import SocialAuthController from '../controllers/socialAuth';


// the strategies for getting the user's facebook, twitter and google id
// the strategies for getting the user's facebook and google id

// pull in our app id and secret from our social config file
passport.use(new FacebookStrategy({

clientID: configAuth.facebookAuth.clientID,
clientSecret: configAuth.facebookAuth.clientSecret,
callbackURL: configAuth.facebookAuth.callbackURL,
profileFields: configAuth.facebookAuth.profileFields
clientID: process.env.FACEBOOK_APP_ID,
clientSecret: process.env.FACEBOOK_APP_SECRET,
callbackURL: process.env.FACEBOOK_CALLBACK_URL,
profileURL: 'https://graph.facebook.com/v2.5/me?fields=first_name,last_name,email',
profileFields: ['id', 'emails', 'name', 'photos', 'displayName']
}, SocialAuthController.passportCallback));

// pull in our app id and secret from our social config file
passport.use(new GoogleStrategy({

clientID: configAuth.googleAuth.clientID,
clientSecret: configAuth.googleAuth.clientSecret,
callbackURL: configAuth.googleAuth.callbackURL,
clientID: process.env.GOOGLE_APP_ID,
clientSecret: process.env.GOOGLE_APP_SECRET,
callbackURL: process.env.GOOGLE_CALLBACK_URL,
proxy: true
}, SocialAuthController.passportCallback));
17 changes: 0 additions & 17 deletions server/config/socialConfig.js

This file was deleted.

22 changes: 15 additions & 7 deletions server/controllers/socialAuth.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import sendgrid from '@sendgrid/mail';
import { User } from '../models';
import TokenHelper from '../utils/TokenHelper';
import EmailVerification from './emailVerificationController';

const key = process.env.SENDGRID_API_KEY;

sendgrid.setApiKey(key);

/**
* @class SocialAuthController
Expand All @@ -21,10 +26,10 @@ class SocialAuthController {
defaults: user,
}).spread((foundOrCreated, created) => {
const {
id, email, username, firstName, lastName, image
id, email, username, firstName, role, lastName, image
} = foundOrCreated.dataValues;
done(null, {
email, id, username, firstName, lastName, image, created,
email, id, username, firstName, role, lastName, image, created,
});
});
}
Expand All @@ -39,22 +44,25 @@ class SocialAuthController {
*/
static response(req, res) {
const user = {
email: req.user.email,
username: req.user.username,
lastName: req.user.lastName,
id: req.user.id,
role: req.user.role,
firstName: req.user.firstName,
lastName: req.user.lastName,
username: req.user.username,
email: req.user.email,
image: req.user.image
};
user.token = TokenHelper.generateToken(user);
if (req.user.created) {
EmailVerification.sendVerificationEmail(user);
return res.status(201).send({
message: 'you have successfully signed up',
message: 'Signup was successful, Please check your email to verify your account',
user,
status: 'success'
});
}
return res.status(200).send({
message: 'you are logged in',
message: 'You are logged in',
user,
status: 'success'
});
Expand Down
34 changes: 34 additions & 0 deletions server/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,40 @@
}
}
},
"/api/auth/facebook":{
"get": {
"tags": [
"Users"
],
"summary": "signs up a new user",
"description": "signs up a user using their existing Facebook account",
"responses": {
"201": {
"description": "user created successfully"
},
"400": {
"description": "This email has already been registered"
}
}
}
},
"/api/auth/google":{
"get": {
"tags": [
"Users"
],
"summary": "signs up a new user",
"description": "signs up a user using their existing Google account",
"responses": {
"201": {
"description": "user created successfully"
},
"400": {
"description": "This email has already been registered"
}
}
}
},
"/api/users/login": {
"post": {
"tags": [
Expand Down
2 changes: 0 additions & 2 deletions server/routes/api/socialauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,4 @@ router.get('/auth/google', passport.authenticate('google', { scope: ['profile',
router.get('/auth/google/callback',
passport.authenticate('google', { session: false }), SocialAuthController.response);

router.post('/auth/response', SocialAuthController.response);

export default router;
4 changes: 2 additions & 2 deletions server/tests/controllers/emailVerification.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const newUser = {
firstName: 'egwuenu',
lastName: 'gift',
username: 'lauragift',
email: 'laurandidi21@gmail.com',
email: 'laurandidi21@gmail.co',
password: 'adek'
};

Expand Down Expand Up @@ -103,7 +103,7 @@ describe('Verify User\'s email address after signup', () => {
it('Returns an error if user has already been verified', (done) => {
chai.request(app)
.post('/api/users/verify/resend-email')
.send({ email: 'laurandidi21@gmail.com' })
.send({ email: 'laurandidi21@gmail.co' })
.end((err, res) => {
expect(res).to.have.status(409);
expect(res.body.status).to.equal('error');
Expand Down
1 change: 1 addition & 0 deletions server/tests/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ import './authorRequests';
import './adminGetUser.test';
import './adminDeactivate';
import './subscription.test';
import './socialAuth.test';
67 changes: 67 additions & 0 deletions server/tests/controllers/socialAuth.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import chai, { expect } from 'chai';
import chaiHttp from 'chai-http';
import nock from 'nock';
import app from '../../..';

chai.use(chaiHttp);

// This is a test to show that the external services was called
describe('Signs up user with their Google account', () => {
before(() => {
nock('https://accounts.google.com')
.filteringPath(() => '/')
.get('/')
.reply(200, {
message: 'You are logged in',
user: {
id: 10,
role: 'user',
firstName: 'Mo\'rin',
lastName: 'd\'okla',
username: 'corn',
email: 'job.adelia.com',
image: 'image.png',
token: 'token'
},
status: 'success'
});

nock('https://www.facebook.com')
.filteringPath(() => '/')
.get('/')
.reply(200, {
message: 'You are logged in',
user: {
id: 10,
role: 'user',
firstName: 'Mo\'rin',
lastName: 'd\'okla',
username: 'corn',
email: 'job.adelia.com',
image: 'image.png',
token: 'token'
},
status: 'success'
});
});

it('Authenticates with google', (done) => {
chai.request(app)
.get('/api/auth/google')
.end((err, res) => {
expect(res.body.message).to.be.equal('You are logged in');
expect(res.body.status).to.be.equal('success');
done();
});
});

it('Authenticates with facebook', (done) => {
chai.request(app)
.get('/api/auth/facebook')
.end((err, res) => {
expect(res.body.message).to.be.equal('You are logged in');
expect(res.body.status).to.be.equal('success');
done();
});
});
});
2 changes: 1 addition & 1 deletion server/tests/utils/article.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('article notification utility', () => {

describe('articleNotification()', () => {
const msg = {
to: 'laurangift@gmail.com',
to: 'laurangift@gmail.co',
from: 'notifications@authorshaven.com',
subject: 'Latest Articles based on who you follow',
html: 'This is a test'
Expand Down
2 changes: 1 addition & 1 deletion server/tests/utils/comment.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('article notification utility', () => {

describe('articleNotification()', () => {
const msg = {
to: 'laurangift@gmail.com',
to: 'laurangift@gmail.co',
from: 'notifications@authorshaven.com',
subject: 'New Reply from Author\'s Haven!',
html: 'This is a test'
Expand Down

0 comments on commit 952365e

Please sign in to comment.