Skip to content

Commit

Permalink
#164796889 twitter authentication (#44)
Browse files Browse the repository at this point in the history
* #164796889 twitter authentication

user can signup to application using twitter

[finishes #164796889]

* #164796889 add token to twitter authentication

add token and return response

[#finishes 164796889]

* #164796889-twitter-authentication

add twitter authentication to authors haven

[finishes #164796889]

* #164796889-twitter-authentication

fixing errors arising from rebasing

[finishes #164796889]

* #164796889 add username to token

add username to authentication token

[finishes #164796889]

* #164796889 removing repeating code

remove reoccuring serialize and deserialize codes

[finishes 164796889]
  • Loading branch information
Oluwaseyi000 authored and Temmyogunbo committed Apr 11, 2019
1 parent 3b28366 commit 0f6ee0d
Show file tree
Hide file tree
Showing 8 changed files with 392 additions and 152 deletions.
482 changes: 345 additions & 137 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,14 @@
"body-parser": "^1.18.3",
"cors": "^2.8.4",
"express": "^4.16.3",
"express-session": "^1.15.6",
"jsonwebtoken": "^8.5.1",
"nock": "^10.0.6",
"passport": "^0.4.0",
"passport-facebook": "^3.0.0",
"passport-google-oauth20": "^2.0.0",
"passport-local": "^1.0.0",
"passport-twitter": "^1.0.4",
"pg": "^7.9.0",
"pg-hstore": "^2.3.2",
"sequelize": "^5.2.0",
Expand Down
8 changes: 8 additions & 0 deletions src/config/passportConfig.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
let CALLBACK_URL = `http://${process.env.HOST}:${process.env.PORT}/api/v1/auth/facebook/callback`;
let CALLBACK_URL_TWITTER = `http://${process.env.HOST}:${process.env.PORT}/api/v1/auth/twitter/callback`;

if (process.env.NODE_ENV === 'production') {
CALLBACK_URL = `https://${process.env.HOST}:${process.env.PORT}/api/v1/auth/facebook/callback`;
CALLBACK_URL_TWITTER = `https://${process.env.HOST}:${process.env.PORT}/api/v1/auth/twitter/callback`;
}

export default {
Expand All @@ -17,4 +19,10 @@ export default {
callbackURL: CALLBACK_URL,
profileFields: ['id', 'email', 'displayName', 'photos'],
},
twitterAuth: {
consumerKey: process.env.TWITTER_CONSUMER_KEY,
consumerSecret: process.env.TWITTER_CONSUMER_SECRET,
callbackURL: CALLBACK_URL_TWITTER,
includeEmail: true,
}
};
4 changes: 2 additions & 2 deletions src/controllers/authentication/socialRedirect.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import Authenticator from '../../middlewares/authenticator';

const socialRedirect = (req, res) => {
const { id, email, role } = req.user;
const { id, email, role, username } = req.user;

const token = Authenticator.generateToken({ id, email, role });
const token = Authenticator.generateToken({ id, email, role, username });
res.cookie('jwt-token', token);
res.redirect('/api/v1');
};
Expand Down
29 changes: 17 additions & 12 deletions src/middlewares/socialCallback.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@ import { User } from '../models';

const strategyCallback = async (accessToken, refreshToken, profile, done) => {
const {
id, displayName, photos, emails
id, displayName, photos, emails, username
} = profile;

const [user] = await User.findOrCreate({
where: { email: emails[0].value },
defaults: {
fullName: displayName,
password: id,
email: emails[0].value,
username: emails[0].value,
imageUrl: photos[0].value,
},
});
return done(null, user.dataValues);
try {
const [user] = await User.findOrCreate({
where: { email: emails[0].value },
defaults: {
fullName: displayName,
password: id,
email: emails[0].value,
username,
imageUrl: photos[0].value,
},
});
return done(null, user.dataValues);
} catch (error) {
return done(null, error);
}
};


export default strategyCallback;
3 changes: 3 additions & 0 deletions src/middlewares/socialStrategy.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { Strategy as FacebookStrategy } from 'passport-facebook';
import { Strategy as GoogleStrategy } from 'passport-google-oauth20';
import { Strategy as TwitterStrategy } from 'passport-twitter';
import passportConfig from '../config/passportConfig';
import strategyCallback from './socialCallback';

const facebookStrategy = new FacebookStrategy(passportConfig.facebookAuth, strategyCallback);
const googleStrategy = new GoogleStrategy(passportConfig.googleAuth, strategyCallback);
const twitterStrategy = new TwitterStrategy(passportConfig.twitterAuth, strategyCallback);

export default {
facebookStrategy,
googleStrategy,
twitterStrategy
};
9 changes: 9 additions & 0 deletions src/routers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import socialRedirect from '../controllers/authentication/socialRedirect';
import { login, createUser } from '../controllers/authentication/user';
import checkBody from '../middlewares/signUpValidator';


const router = Router();

router.get('/', (req, res) => {
Expand Down Expand Up @@ -58,4 +59,12 @@ router.get('/auth/google/callback', passport.authenticate('google', { failureRed
router
.post('/signup', checkBody, createUser);

// route for twitter authentication
router.get('/auth/twitter', passport.authenticate('twitter'));

router.get('/auth/twitter/callback', passport.authenticate('twitter', { failureRedirect: '/api/v1/auth/login' }), socialRedirect);


router.post('/login', checkFields, passportAuth, login);

export default router;
7 changes: 6 additions & 1 deletion src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import express from 'express';
import bodyParser from 'body-parser';
import cors from 'cors';
import swaggerUI from 'swagger-ui-express';
import session from 'express-session';
import passport from 'passport';
import socialStrategy from './middlewares/socialStrategy';
import docs from '../swagger.json';
import router from './routers/index';
import passportConfig from './middlewares/localStrategy';
import socialStrategy from './middlewares/socialStrategy';


let httpServer;

Expand All @@ -30,6 +32,7 @@ export const startServer = port => new Promise((resolve, reject) => {
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cors());

app.use(session({ secret: 'this is', resave: true, saveUninitialized: true }));
app.use(passport.initialize());
app.use(passport.session());
passport.serializeUser((user, cb) => {
Expand All @@ -45,6 +48,7 @@ export const startServer = port => new Promise((resolve, reject) => {
app.use('/api/v1', router);
passport.use(socialStrategy.facebookStrategy);
passport.use(socialStrategy.googleStrategy);
passport.use(socialStrategy.twitterStrategy);

app.use('/docs', swaggerUI.serve, swaggerUI.setup(docs));

Expand All @@ -54,6 +58,7 @@ export const startServer = port => new Promise((resolve, reject) => {
next(error);
});


app.use((error, res, next) => {
res.status(error.status || 500).json({
error: {
Expand Down

0 comments on commit 0f6ee0d

Please sign in to comment.