I have seen similar posts with this problem, but none of those answers seemed to fix my issue. Including the missing hyphen in a couple answers.
When I send the user to Twitter to get authenticated using Passport, the broswer says I have no GET handler for my callbackURL. I am super confused since I believe I do have it handled, but its not working.
In my app.js file I have:
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var passport = require('passport');
var session = require('express-session');
app.set('views', './views');
app.set('view engine', 'jade');
app.use(express.static('public'));
app.use(express.static("node_modules/bootstrap/dist"));
app.use(express.static("node_modules/jquery/dist"));
app.use(express.static('img'));
app.use(require('express-session')({
secret:'prince', resave: false, saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
var authRouter = require('./admin/auth');
app.use("/admin/login", authRouter);
var frontPageFlow = require('./index');
app.get('/', frontPageFlow, function (req, res) {
res.render('index', {
title: "Crowd District"
});
});
In my Auth.js file I have:
var express = require('express');
var passport = require('passport');
var TwitterStrategy = require('passport-twitter').Strategy;
var router = express.Router();
module.exports = router;
router.get('/auth/twitter',
passport.authenticate('twitter'),
function (req, res) {
res.render('login')
});
router.get('/auth/twitter/callback',
passport.authenticate('twitter', {
successRedirect: '/',
failureRedirect: '/login'
}));
router.route('/')
.get(function (req, res) {
res.render("login");
})
.post(passport.authenticate('twitter', {
successRedirect: '/',
failureRedirect: '/admin/login'
})
);
passport.use(new TwitterStrategy({
consumerKey: 'Consumer key here',
consumerSecret: 'Consumer secret here',
callbackURL: 'http://localhost:9000/auth/twitter/callback'
},
function(token, tokenSecret, profile, cb) {
User.findOrCreate({ twitterId: profile.displayName }, function (err, user) {
if (err) { return cb(null, profile); }
if (!user){
user = new User ({
name: profile.displayName,
email: profile.emails[0].value,
username: profile.username,
provider: 'twitter'
});
user.save(function (err) {
if (err) console.log(err);
console.log("successful user entry!");
return done(err, user);
});
} else {
return done(err, user);
}
});
}
));
And my userModel.js
var mongoose = require('mongoose');
var express = require('express');
var router = express.Router();
module.exports = router;
var schemaOptions = {
collection: "users"
};
var schema = new mongoose.Schema({
id : String,
token : String,
displayName : String,
username : String
}, schemaOptions);
module.exports = mongoose.model('User', schema);
No matter what I do, or rearrange the order of operations, I continue to get in my callback:
Cannot GET /auth/twitter/callback?oauth_token=enfwejngfon2804&oauth_verifier=LK35h5988gieunrgbr4ghghi
Thank you very much in advance!
Your route paths are incorrect because you are using app.use("/admin/login", authRouter);
which makes all routes in that file have a prefix '/admin/login'
, so instead of '/auth/twitter/callback'
, you code is expecting '/admin/login/auth/twitter/callback'
. Get all your paths to line up and you'll be in better shape. I recommend avoiding mount prefixes when calling app.use
. They just lead to this kind of confusion. Just use app.use(authRouter);
and make your route paths absolute paths. They are much more straightforward to work with.