Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up connect-flash middleware for use by passport

I want to access the message that my passport strategy specifies in its callback, like this: done(null, false, { message: 'No such user.' });. I found out so far that these messages can be displayed by passing the option failureFlash: true into the passport.authenticate() function, the usage of which again requires connect-flash middleware to be installed. So I installed the module and added var flash = require('connect-flash); to my source, as well as app.use(flash()); to the configuration method of my express app. But it still crashes with the error that a method .flash() is not available. What did I miss?

More code:

var http = require('http');
var express = require('express');
var passport = require('passport');
var googleStrategy = require('passport-google').Strategy;
var flash = require('connect-flash');

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

passport.deserializeUser(function (obj, done) {
    done(null, obj);
});

passport.use(new googleStrategy(
    { 
        returnURL: 'http://localhost:123456/auth/google/return', 
        realm: 'http://localhost:123456/' 
    },
    function(identifier, profile, done) {
        process.nextTick(function () {
            User.findOne({ ID: identifier }, function (err, user) {
                if (err) {
                    return done(err);
                }

                if (!user) {
                    return done(null, false, { message: 'no such user' });
                }

                return done(null, user);
            });
        });
    })
);

var app = express();

app.configure(function() {
    app.set('view engine', 'ejs');
    app.set('views', __dirname + '/views');
    app.use(express.logger());
    app.use(express.cookieParser());
    app.use(express.bodyParser());
    app.use(express.methodOverride());
    app.use(express.session({ secret: 'veryverysecretsecret' }));
    app.use(passport.initialize());
    app.use(passport.session());
    app.use(app.router);
    app.use(flash());
});

app.get(
    '/auth/google',
    passport.authenticate('google', { failureRedirect: '/login', failureFlash: true }),
    function (req, res) {
        res.redirect('/');
    }
);

app.get('/auth/google/return',
    passport.authenticate('google', { failureRedirect: '/login', failureFlash: true }),
    function (req, res) {
        res.redirect('/');
    }
);

http.createServer(app).listen(123456);
like image 893
Janis F Avatar asked Jan 11 '23 21:01

Janis F


1 Answers

Passport needs flash to be configured before itself.

To fix your problem, just change the order in app.configure(), and move the passport use below it, like so:

app.configure(function() {
    app.set('view engine', 'ejs');
    app.set('views', __dirname + '/views');
    app.use(express.logger());
    app.use(express.cookieParser());
    app.use(express.bodyParser());
    app.use(express.methodOverride());
    app.use(express.session({ secret: 'veryverysecretsecret' }));
    app.use(flash());
    app.use(passport.initialize());
    app.use(passport.session());
    app.use(app.router);
});

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

passport.deserializeUser(function (obj, done) {
    done(null, obj);
});

passport.use(new googleStrategy(
    { 
        returnURL: 'http://localhost:123456/auth/google/return', 
        realm: 'http://localhost:123456/' 
    },
    function(identifier, profile, done) {
        process.nextTick(function () {
            User.findOne({ ID: identifier }, function (err, user) {
                if (err) {
                    return done(err);
                }

                if (!user) {
                    return done(null, false, { message: 'no such user' });
                }

                return done(null, user);
            });
        });
    })
);

Glad it helped!

like image 68
Jason Nichols Avatar answered Jan 16 '23 20:01

Jason Nichols