Express-Passport本地身份验证不适用于Safari浏览器

时间:2017-08-02 12:41:08

标签: javascript node.js safari passport.js express-session

请注意我是节点js的初学者,所以请提前道歉。

        app.use(express.static(__dirname + uiDir));
        app.use(cookieParser('SecretKey')); 
        app.use(bodyParser.json());
        app.use(bodyParser.urlencoded({extended: true}));
        app.use(session({
           secret: 'SecretKey',
            resave: true,
            saveUninitialized: true,
            rolling:'true',
            cookie:{
                maxAge: 60*1000*30, 
                secure:true,
                httpOnly: true,
            }
        }));

        app.use(passport.initialize());
        app.use(passport.session()); 
        app.use(helmet({
            frameguard: {
                action: 'sameorigin'
            }
        }));
        app.use(flash()); 



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

        passport.deserializeUser(function(user, done) {
            done(null, user);
        });     
       passport.use('local',new LocalStrategy(

                function(username, password, done) {

                var loginqry = {
                    jsonrpc: '2.0',
                                id: 12,
                        method: 'call',
                        params: [ 'toIntergrateLater', 'users', 
                                 'authenticate',

                                  {"username":username,
                                  "password":password
                                  } 

                        ]
                };

                BackendAuthservice.auth(loginqry,function(data){                

                   if(data.result.error=="Wrong password"){                                        
                     return done(null, false, { message: 'Incorrect username or password.' });
                    }
                    if(data.result.error=="User not found"){
                         return done(null, false, { message: 'User not found' });
                    }   

                    else{
                        data.result.status = true;
                        return done(null,data );
                    }
                });
      }));

此代码适用于IE,Firefox,Chrome,Mac-google-chrome但当我访问MAC Safari(req.user)时未定义。请帮忙

来自safari的传入请求,我在护照-js的成功重定向中获取req.user未定义

1 个答案:

答案 0 :(得分:0)

尝试在完成回调中传递用户而不是 user.username 。 如果在serialize用户中传递了user.username,则deserialize方法必须将username作为参数。

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