Skip to content

Commit

Permalink
bg(oauth 2): fix twitter signIn/signUp
Browse files Browse the repository at this point in the history
- setup twitter app
- setup twitter client id and secret
- refractored app to fix issues
- add twitter env variables to heroku

[#159069548]
  • Loading branch information
Ben Onah committed Jul 19, 2018
1 parent 1c08dfe commit c50be88
Show file tree
Hide file tree
Showing 11 changed files with 3,567 additions and 3,483 deletions.
9 changes: 9 additions & 0 deletions app.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@
},
"GOOGLE_CALLBACK_URL": {
"required": true
},
"TWITTER_CUSTOMER_KEY": {
"required": true
},
"TWITTER_CUSTOMER_SECRET": {
"required": true
},
"TWITTER_CALLBACK_URL": {
"required": true
}
},
"formation": {
Expand Down
3 changes: 1 addition & 2 deletions app/controllers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,11 @@ const signin = (req, res) => {
} else {
const { user } = req;
const token = Tokenizer(user);
const url = `/#!/auth?${token}--${user.name}--${user._id}`;
const url = `/#!/auth?${token}----${user.name}---${user._id}`;
res.redirect(url);
}
};


/**
* @param {object} req - request object provided by express
* @param {object} res - response object provided by express
Expand Down
6 changes: 3 additions & 3 deletions config/env/development.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ module.exports = {
callbackURL: process.env.FB_CALLBACK_URL
},
twitter: {
clientID: '401685871994-3i58in34j7qpngdka5ci54hh441n9a3q.apps.googleusercontent.com","project_id":"et-cfh-210520","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://accounts.google.com/o/oauth2/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"sHJSbinFDieBRDG2SKceCJN1',
clientSecret: 'sHJSbinFDieBRDG2SKceCJN1',
callbackURL: 'http://cardsforhumanity.com:3000/auth/twitter/callback'
clientID: process.env.TWITTER_CUSTOMER_KEY,
clientSecret: process.env.TWITTER_CUSTOMER_SECRET,
callbackURL: process.env.TWITTER_CALLBACK_URL
},
google: {
clientID: process.env.GOOGLE_CLIENT_ID,
Expand Down
6 changes: 3 additions & 3 deletions config/env/production.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ module.exports = {
callbackURL: process.env.FB_CALLBACK_URL
},
twitter: {
clientID: 'CONSUMER_KEY',
clientSecret: 'CONSUMER_SECRET',
callbackURL: 'http://cfh.io/auth/twitter/callback'
clientID: process.env.TWITTER_CUSTOMER_KEY,
clientSecret: process.env.TWITTER_CUSTOMER_SECRET,
callbackURL: process.env.TWITTER_CALLBACK_URL
},
google: {
clientID: process.env.GOOGLE_CLIENT_ID,
Expand Down
11 changes: 11 additions & 0 deletions config/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import compress from 'compression';
import helpers from 'view-helpers';
import bodyParser from 'body-parser';
import methodOverride from 'method-override';
import session from 'express-session';
import config from './config';

/* eslint no-console: 0 */
Expand Down Expand Up @@ -37,6 +38,16 @@ export default (app, passport) => {
// bodyParser should be above methodOverride
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.set('trust proxy', 1); // trust first proxy
app.use(session({
secret: process.env.SECRET_KEY,
resave: false,
saveUninitialized: true,
cookie: { secure: false }
}));
// app.use(express.session({ secret: process.env.SECRET_KEY })); // session secret
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(methodOverride());

// dynamic helpers
Expand Down
21 changes: 12 additions & 9 deletions config/passport.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@

import mongoose from 'mongoose';
import { Strategy as LocalStrategy } from 'passport-local';
import { Strategy as TwitterStrategy } from 'passport-twitter';
// import { Strategy as TwitterStrategy } from 'passport-twitter';
import { Strategy as FacebookStrategy } from 'passport-facebook';
import { OAuth2Strategy as GoogleStrategy } from 'passport-google-oauth';
import config from './config';

const TwitterStrategy = require('passport-twitter').Strategy;

const User = mongoose.model('User');

export default (passport) => {
Expand Down Expand Up @@ -55,13 +57,14 @@ export default (passport) => {

// Use twitter strategy
passport.use(new TwitterStrategy({
consumerKey: process.env.TWITTER_CONSUMER_KEY || config.twitter.clientID,
consumerSecret: process.env.TWITTER_CONSUMER_SECRET || config.twitter.clientSecret,
callbackURL: config.twitter.callbackURL
consumerKey: config.twitter.clientID,
consumerSecret: config.twitter.clientSecret,
callbackURL: config.twitter.callbackURL,
userProfileURL: 'https://api.twitter.com/1.1/account/verify_credentials.json?include_email=true',
},
((token, tokenSecret, profile, done) => {
User.findOne({
'twitter.id_str': profile.id
email: profile.emails[0].value
}, (err, user) => {
if (err) {
return done(err);
Expand All @@ -70,6 +73,8 @@ export default (passport) => {
user = new User({
name: profile.displayName,
username: profile.username,
avatar: profile.photos[0].value,
email: profile.emails[0].value,
provider: 'twitter',
twitter: profile._json
});
Expand All @@ -92,7 +97,7 @@ export default (passport) => {
},
((accessToken, refreshToken, profile, done) => {
User.findOne({
'facebook.id': profile.id
email: (profile.emails && profile.emails[0].value)
}, (err, user) => {
if (err) {
return done(err);
Expand All @@ -101,9 +106,7 @@ export default (passport) => {
user = new User({
name: profile.displayName,
email: (profile.emails && profile.emails[0].value) || '',
username: profile.username,
avatar: profile.photos ? profile.photos[0].value : '',
// password: Math.random().toString(36).substring(2),
provider: 'facebook',
facebook: profile
});
Expand All @@ -126,7 +129,7 @@ export default (passport) => {
},
((accessToken, refreshToken, profile, done) => {
User.findOne({
'google.id': profile.id
email: profile.emails[0].value
}, (err, user) => {
if (err) {
return done(err);
Expand Down
10 changes: 7 additions & 3 deletions config/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ export default (router, passport, app) => {
.post('/users/invite', ensureUser, users.invite)
.post('/game/:id/start', ensureUser, game)
.get('/profile', ensureUser, users.fetchProfile)
.post('/game/:id/start', ensureUser, game);
.get('/signout', users.signout);

// Setting up the game api
api
.post('/game/:id/start', auth, game);

router.get('/signin', users.signin);
router.get('/signup', users.signup);
router.get('/chooseavatars', users.checkAvatar);
router.get('/signout', users.signout);

// Setting up the users api
router.post('/users', users.create);
Expand All @@ -37,7 +40,7 @@ export default (router, passport, app) => {
router.get('/users/:userId', users.show);

// Setting the facebook oauth routes
router.get('/auth/facebook/', passport.authenticate('facebook', {
router.get('/auth/facebook', passport.authenticate('facebook', {
scope: ['email'],
failureRedirect: '/signin',
}), users.signin);
Expand Down Expand Up @@ -97,6 +100,7 @@ export default (router, passport, app) => {


app.use((err, req, res, next) => {
console.log(err);
// error from the '/api' namespaced routes
if (err.status) return res.status(err.status).json({ message: err.message });
// Treat as 404
Expand Down
Loading

0 comments on commit c50be88

Please sign in to comment.