Passport.authenticate() gets stucks

65 views Asked by At

I am trying to make a "Connect with Twitter" feature for my website, and it is the first time that I use Passport.js.

I have a problem since a while now : when trying the api, the passport.authenticate function of the /twitter/callback page gets stucks and I can't do anything to solve this problem

I am running my app on my localhost system. Here is my code for the index.js page. The home.ejs just contains a template for EJS where I show the username.

var express = require('express');
var app = express();

const cookieParser = require('cookie-parser');
app.use(cookieParser('thissecretrocks'));

const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());

const session = require('express-session');
app.use(session({
    resave: true,
    saveUninitialized: true,
    secret: 'bla bla bla' 
}));

const mongoose = require('mongoose');
mongoose.connect('mongodb+srv://Something', {useNewUrlParser: true, useUnifiedTopology: true});
const db = {};
db.users = require('./db/users');

const passport = require('passport');
app.use(passport.initialize());
app.use(passport.session());

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

passport.serializeUser(function (user, done) {
    done(null, user.id);
});

passport.deserializeUser(function (id, done) {
    db.users.findById(id, function (err, user) {
        done(err, user);
    });
});

passport.use(new TwitterStrategy({
    consumerKey: '5KSomethingMx',
    consumerSecret: 'tld1Something6M2q',
    callbackURL: "http://127.0.0.1:3000/auth/twitter/callback",
},
function(token, tokenSecret, profile, cb) {
    console.log(profile);
    /*const newUser = new db.users({
        twitter: profile.id
    });
    newUser.save();*/
}
));

app.use('/cdn', express.static('cdn'));

app.get('/', function(req, res) {
    res.render('home.ejs', {username: req.user.username});
});

app.get('/fail', function(req, res) {
    console.log('FAIL');
    res.render('home.ejs', {username: 'req.user.username'});
});

app.get('/auth/twitter', passport.authenticate('twitter'));

app.get('/auth/twitter/callback', passport.authenticate('twitter', { failureRedirect: '/fail' }), function(req, res) {
    res.redirect('/');
});

app.listen(3000);
0

There are 0 answers