Why is `req.user` undefined for socket requests in Sails.js, Angular.js and Passport.js?

前端 未结 3 1618
萌比男神i
萌比男神i 2021-01-14 18:30

I am trying to get the questions for the current user with a request like this:

http://localhost:1337/question/forme         


        
3条回答
  •  庸人自扰
    2021-01-14 19:09

    I spent a large time figuring out all the moving parts to get passport working with sails on my blog

    I'll repost the relevant parts below, with an additional policy change I made.

    I created a passport.js file to manage my passport configuration/strategy registration

    /config/passport.js
    
    var passport = require('passport'),
    LocalStrategy = require('passport-local').Strategy;
    
    passport.serializeUser(function(user, done) {
        done(null, user.id);
    });
    
    passport.deserializeUser(function(id, done) {
        User.findOneById(id).done(function (err, user) {
            done(err, user);
        });
    });
    
    passport.use(new LocalStrategy({
            usernameField: 'email',
            passwordField: 'password'
        },
        function(email, password, done) {
        User.findOne({ email: email}).done(function(err, user) {
              if (err) { return done(err); }
                if (!user) { return done(null, false, { message: 'Unknown user ' + email }); }
                if (user.password != password) { return done(null, false, { message: 'Invalid password' }); }
                return done(null, user);
            });
        }
    )); 
    

    And then I used a policy to verify authentication. It makes use of the passport sessions if a socket request is made.

    /api/policies/isAuthenticated.js
    module.exports = function(req, res, next) {
    
      // User is allowed, proceed to the next policy, 
      // or if this is the last policy, the controller
        // Sockets
        if(req.isSocket)
        {
            if(req.session &&
                req.session.passport &&
                req.session.passport.user)
            {
                //Use this:
    
                // Initialize Passport
                sails.config.passport.initialize()(req, res, function () {
                    // Use the built-in sessions
                    sails.config.passport.session()(req, res, function () {
                        // Make the user available throughout the frontend
                        //res.locals.user = req.user;
                        //the user should be deserialized by passport now;
                        next();
                    });
                });
    
                //Or this if you dont care about deserializing the user:
                //req.user = req.session.passport.user;
                //return next();
    
            }
            else{
                res.json(401);
            }
    
    
        }
        else if (req.isAuthenticated()) {
            return next();
        }
        else{
            // User is not allowed
            // (default res.forbidden() behavior can be overridden in `config/403.js`)
            return res.redirect('/account/login');
        }
    };
    

提交回复
热议问题