使用 angular $http 请求时,passport req.isAuthenticated 总是 return false

passport req.isAuthenticated always return false when using angular $http request

我正在使用 passportjs 进行用户身份验证。

通过使用 postman,我可以成功登录并且 req.isAuthenticated 在登录后的后续请求中总是 returns me true。

通过使用 angular $http,但是,登录工作正常,但 req.isAuthenticated 总是 returns me false 在子序列请求中。我的 angular app(localhost:3000)node app(heroku) 位于不同的域。我的想法是它可能与 CORS 或 session cookie 有关,因为我在浏览器中没有看到任何 cookie。

我做了什么

  1. 我尝试将请求 header 设置为允许 CORS。
  2. 我还尝试在 angular $http 和节点应用程序中设置 Access-Control-Allow-Credentials

不幸的是,所有尝试都失败了T_T

Nodejs 设置

    app.use(function(req, res, next) {
        res.header('Access-Control-Allow-Credentials', true);
        res.header('Access-Control-Allow-Origin', req.headers.origin);
        res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
        res.header('Access-Control-Allow-Headers', 'X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept');
        if ('OPTIONS' == req.method) {
            res.send(200);
        } else {
            next();
        }
    });

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

    // used to deserialize the user
    passport.deserializeUser(function(id, done) {
        Account.findById(id, function(err, account) {
            done(err, account);
        });
    });

    passport.use('local-signup', new LocalStrategy({
        usernameField: 'email'
    }, function(email, password, done) {
        ....
    }));

    passport.use('local-login', new LocalStrategy({
        usernameField: 'email'
    }, function(email, password, done) {
        ....
    }));

    app.use(morgan('dev')); // log every request to the console

    app.use(bodyParser.urlencoded({ extended: false }));

    app.use(bodyParser.json());

    app.use(cookieParser(config.app.sessionSecret));

    app.use(session({
        secret: config.app.sessionSecret,
        resave: false,
        saveUninitialized: true,
        cookie: { httpOnly: true, maxAge: 2419200000 }
    }));

    // Passport for account authentication
    app.use(passport.initialize());
    app.use(passport.session()); // persistent login sessions

    ///////////////////////////////////////////////////////////////////route controller function
    authenticate: function(req, res) {
        if (!req.isAuthenticated()) {
            res.redirect('/login');
        } else {
            res.status(200).send('successful');
        }
    }

Angular

    $http.post('http://xxxxx/login', 
      {  email: $scope.user.email,
         password: $scope.user.password
      })
    .then(function(response) {
        ...
    }, function(response) {
        ...
    });

    $http.get('http://xxxxx/authenticate',
        { withCredentials: true }).success(function() {
            ...
        })
        .error(function() {
            ... // always get here with 302 redirect
        });

我的questions/things我不明白

  1. 是不是因为session浏览器没有设置cookie导致的问题
  2. 如果它与 CORS 相关,我是否遗漏了任何设置?
  3. 还有什么????

我根据@robertklep 的评论自己解决了这个问题。基本上passportjs的设置没有问题。这完全是关于如何在 angular 中发送 withCredentials 标志。而不是调用 $http 的 get 或 post 方法。我使用以下格式并且它适用于我

$http({
    method: 'GET',
    url: 'xxxx',
    data: {...},
    withCredentials: true
}).success ....

参考: https://docs.angularjs.org/api/ng/service/$http#usage

如果您的登录服务请求包含 'withCredentials' 配置选项,如果您提供正确的登录详细信息,passport.js 会将凭据附加到请求中。

$http({
method: 'POST',
url: 'http://xxxxx/login',
data: {...},
withCredentials: true})