Hello I have an error in my passport code:
The code passed is incorrect or expired.
500
TokenError: The code passed is incorrect or expired.
at Strategy.OAuth2Strategy.parseErrorResponse (/usr/app/node_modules/passport-oauth2/lib/strategy.js:329:12)
at Strategy.OAuth2Strategy._createOAuthError (/usr/app/node_modules/passport-oauth2/lib/strategy.js:376:16)
at /usr/app/node_modules/passport-oauth2/lib/strategy.js:166:45
at /usr/app/node_modules/passport-github/lib/strategy.js:77:16
at /usr/app/node_modules/oauth/lib/oauth2.js:209:7
at passBackControl (/usr/app/node_modules/oauth/lib/oauth2.js:134:9)
at IncomingMessage.<anonymous> (/usr/app/node_modules/oauth/lib/oauth2.js:157:7)
at emitNone (events.js:111:20)
at IncomingMessage.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1064:12)
at _combinedTickCallback (internal/process/next_tick.js:139:11)
at process._tickCallback (internal/process/next_tick.js:181:9)
I have this problem on all computers except mine so I guess there is a token linked to my computer but not to the other ones.
This problem is present on twitter passport authentication
My strategies:
const passport = require('passport')
const TwitterStrategy = require('passport-twitter').Strategy;
const User = require('../models/User');
const keys = require('../config/keys')
passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.deserializeUser((id, done) => {
User.findById(id).then((user) => {
done(null, user);
});
});
passport.use(new TwitterStrategy({
consumerKey: keys.twitter.consumerKey,
consumerSecret: keys.twitter.consumerSecret,
callbackURL: "http://localhost:8080/auth/twitter/callback"
},
function(accessToken, refreshToken, profile, done) {
User.findOne({userid: profile.id}).then((currentUser) => {
if(currentUser){
done(null, currentUser);
} else {
new User({
userid: profile.id,
name: profile.displayName
}).save().then((newUser) => {
console.log('created new user: ', newUser);
done(null, newUser);
});
}
});
}));
module.exports = passport;
My routes:
const express = require('express');
const router = express.Router();
const passportTwitter = require('../auth/twitter');
const User = require('../models/User');
/* LOGIN ROUTER */
router.get('/login', function(req, res, next) {
res.render('login', { title: 'Please Sign In with:' });
});
/* LOGOUT ROUTER */
router.get('/logout', function(req, res){
req.logout();
res.redirect('/');
});
/* TWITTER ROUTER */
router.get('/twitter',
passportTwitter.authenticate('twitter'));
router.get('/twitter/callback',
passportTwitter.authenticate('twitter', { failureRedirect: '/login' }),
function(req, res) {
res.redirect('/users');
});
module.exports = router;
I know that this error is pretty common but I've been going from stackoverflow to github for 3 days to fix it and I'm not moving forward. So if you have a solution I would be very grateful.
If you want to try it yourself, I made a github: https://github.com/SPierre-Eloy/Passport_twitter_api/tree/master
EDIT : I reduced the code to isolate the problem to a single strategy, that of twitter.
SOLVED. It was MongoDB that posed problem, I temporarily removed it the time to find a lasting solution