I have this functionality working in another project and I've reused that code in this project but i can't seem to get it to work. I've checked every line of code, crossed my T's and dotted my lower case j's and still can't figure out why it wont work.
All that happens on the login page is when I click the twitter or facebook button, it just reloads the page. I feel like its something with my routes but i've tried using different variations.
My jade file
form(method="POST", action="/auth/twitter")
button.btn.btn-primary(type='submit') Login with Twitter
form(method="POST", action="/auth/facebook")
button.btn.btn-primary(type='submit') Login with Facebook
my app.js file
var needUserFile = require('./auth/user');
app.get('/auth/twitter/callback', needUserFile,
passport.authenticate('twitter', { successRedirect: '/',
failureRedirect: '/login' }));
app.get('/auth/facebook/callback',
passport.authenticate('facebook', { failureRedirect: '/auth/login' }),
function(req, res) {
console.log("I am being hit yo");
// Successful authentication, redirect home.
res.redirect('/');
});
// This router kept out of middleware so people can access the auth routes
var authRouter = require('./auth/auth');
app.use('/auth/login', authRouter);
// Will make sure user is Auth'd before seeing other routes
app.use(function (req, res, next) {
if (req.isAuthenticated()) {
next();
return;
}
res.redirect('/auth/login');
});
my auth.js file
var express = require('express');
var passport = require('passport');
var router = express.Router();
require('./twitterAuth');
require('./facebookAuth');
module.exports = router;
router.route('/')
.get(function (req, res) {
res.render("login");
});
router.route('/twitter')
.post(passport.authenticate('twitter', {
successRedirect: '/',
failureRedirect: '/admin/login'
// successFlash: 'Welcome!',
// failureFlash: true
}));
router.route('/facebook')
.post(passport.authenticate('facebook', {
successRedirect: '/',
failureRedirect: '/admin/login'
// successFlash: 'Welcome!',
// failureFlash: true
}));
my facebookAuth.js file
var express = require('express');
var passport = require('passport');
var FacebookStrategy = require('passport-facebook').Strategy;
var router = express.Router();
module.exports = router;
passport.serializeUser (function (user, cb) {
cb(null, user);
console.log('serialized a facebook user');
});
passport.deserializeUser (function (obj, cb) {
cb(null, obj);
console.log('deserialized a facebook user');
});
// Configuration of the passport strategy
passport.use(new FacebookStrategy({
clientID: 'CLIENTID',
clientSecret: 'CLIENTSECRET',
callbackURL: 'http://localhost:9000/auth/facebook/callback'
},
// This takes the info sent back from facebook
// If user does not exist in DB, it creates a new user
function(accessToken, refreshToken, profile, done) {
console.log(accessToken);
console.log(refreshToken);
console.log(profile);
console.log(done);
console.log("successfully auth'd a FACEBOOK user!");
done(null, profile);
}
));
my twitterAuth.js file
var express = require('express');
var passport = require('passport');
var TwitterStrategy = require('passport-twitter').Strategy;
var router = express.Router();
var userDB = require('../data/userDB');
var User = require('./userModel');
module.exports = router;
// // This will be to save a session --> not needed now
passport.serializeUser (function (user, cb) {
cb(null, user);
console.log('serialized a user');
});
passport.deserializeUser (function (obj, cb) {
cb(null, obj);
console.log('deserialized the user');
});
// Configuration of the passport strategy
passport.use(new TwitterStrategy({
consumerKey: 'CONSUMERKEY',
consumerSecret: 'CONSUMERSECRET',
callbackURL: 'http://localhost:9000/auth/twitter/callback'
},
// This takes the info sent back from twitter
// If user does not exist in DB, it creates a new user
function(accessToken, refreshToken, profile, done) {
User.findOne({ id: profile.id }, function(err, user) {
if(err) {
console.log(err); // handle errors!
}
if (!err && user !== null) {
done(null, user);
} else {
user = new User({
id: profile.id,
token: accessToken,
displayName: profile.displayName,
username: profile.username
});
user.save(function(err) {
if(err) {
console.log(err); // handle errors!
} else {
console.log("saving user ...");
done(null, user);
}
});
}
});
console.log("successfully saved a user!");
}));
via Andrew Little
No comments:
Post a Comment