Passport 和 Passport Local req.isAuthenticated 总是返回 false [英] Passport and Passport Local req.isAuthenticated always returns false

查看:27
本文介绍了Passport 和 Passport Local req.isAuthenticated 总是返回 false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法追踪到这一点,但对于我的设置,即使在成功登录后,isAuthenticated 也始终返回 false.这是护照代码:

I haven't been able to track this down, but for my set up, isAuthenticated always returns false even after a successful login. Here's the passport code:

req.isAuthenticated = function() {
  var property = 'user';
  if (this._passport && this._passport.instance._userProperty) {
    property = this._passport.instance._userProperty;
  }

  return (this[property]) ? true : false;
};

但是快速环顾四周,我在本地策略的任何地方都没有看到 _userProperty 属性(抱歉,如果我看不够仔细),所以我想这可能就是为什么它总是如此返回错误?

But in a quick look around I don't see the _userProperty proeprty anywhere in the local strategy (sorry if I didn't look hard enough), so I suppose that might be why it's always returning false?

我会留下我的应用程序代码的代码示例,但我觉得快速查看我正在进行的工作的 repo 可能更容易:passport api token sessionless

I'd leave a code sample of my application code, but I feel it's probably easier to have a quick look at the repo for my work in progress: passport api token sessionless

最终,我的目标是让该样板项目的注销正常工作(目前还没有).

Ultimately, my goal is to have logout work properly for that boilerplate project (which it currently it doesn't).

推荐答案

如果我最初的问题没有那么有用,我深表歉意,但是...

Apologies if my original question is not that useful in the first place, but...

我发现我的护照、护照本地和护照本地猫鼬的组合,一个解决方案是简单地在我的猫鼬架构上创建一个失效方法(插入了passportLocalMongoose"",当我的 /logout 路由被点击时,我基本上删除了该用户的令牌.这是该方法:

I found that my combination of passport, passport-local, and passport-local-mongoose, a solution was to simply create an invalidation method on my mongoose Schema (that has the passportLocalMongoose "plugged in", and when my /logout route gets hit I essentially remove that user's token. Here's that method:

Account.statics.invalidateUserToken = function(email, cb) {
    var self = this;
    this.findOne({email: email}, function(err, usr) {
        if(err || !usr) {
            console.log('err');
        }
        usr.token = null;
        usr.save(function(err, usr) {
            if (err) {
                cb(err, null);
            } else {
                cb(false, 'removed');
            }
        });
    });
};

我认为在上下文中看到这一点更有趣,因此请再次参考问题中列出的存储库...希望这对某人有所帮助.

I presume it's more interesting to see this in context so again please feel free to refer to the repo listed in question...hope this helps someone.

此外,如果来自上述库之一的核心想要提出更好的方法,我当然愿意重构我的代码以使其符合习惯;如果没有,这种方法似乎有效.

Also, if a core from one of the aformentioned libs wants to suggest a better way I'd of course love to refactor my code to make it idiomatic; if not, this approach seemed to work.

这篇关于Passport 和 Passport Local req.isAuthenticated 总是返回 false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆