简体   繁体   English

使用KoaJS和PassportJS自动登录用户

[英]Automatically logging in a user with KoaJS and PassportJS

I'm trying to automatically log in a user with PassportJS . 我正在尝试使用PassportJS自动登录用户。

This is my current code: 这是我当前的代码:

myRouter.get('/signin', function* (next) {

    user = {...};

    var res = this.res; // needed for the function below
    this.req.login(user, function(err) {
        if (err)
            console.log('error logging in user - '+err);
        return res.redirect('/'); // <--- line 439
    });
});

But when I run it, I get the error: 但是当我运行它时,我得到了错误:

  error logging in user - TypeError: undefined is not a function
  TypeError: undefined is not a function
      at /srv/www/domain.com/app.js:439:32
      at /srv/www/domain.com/node_modules/koa-passport/node_modules/passport/lib/http/request.js:49:48
      at pass (/srv/www/domain.com/node_modules/koa-passport/node_modules/passport/lib/authenticator.js:293:14)
      at Authenticator.serializeUser (/srv/www/domain.com/node_modules/koa-passport/node_modules/passport/lib/authenticator.js:295:5)
      at Object.req.login.req.logIn (/srv/www/domain.com/node_modules/koa-passport/node_modules/passport/lib/http/request.js:48:29)
      at Object.<anonymous> (/srv/www/domain.com/app.js:434:26)
      at GeneratorFunctionPrototype.next (native)
      at Object.dispatch (/srv/www/domain.com/node_modules/koa-router/lib/router.js:317:14)
      at GeneratorFunctionPrototype.next (native)
      at Object.<anonymous> (/srv/www/domain.com/node_modules/koa-common/node_modules/koa-mount/index.js:56:23)

A quick semi-derp moment and I realize to redirect in koa it does not use res but this , you must do the following: 快速半DERP的时刻,我意识到在重定向koa它不使用res ,但this ,你必须做到以下几点:

var res = this; // needed for the next function
this.req.login(user, function(err) {
    if (err)
        console.log('error logging in user - '+err);
    return res.redirect('/');
});

Your code is fine, it is just that res is called response , so just change var res = this.res; 您的代码很好,只是将res称为response ,所以只需更改var res = this.res; in var res = this.response; var res = this.response; and it will work fine. 它将正常工作。 res does exist, but it is the Node http module response, not the Koa Response object, and therefore does not have any redirect method. res的确存在,但是它是Node http模块的响应,而不是Koa Response对象,因此没有任何redirect方法。 The redirect is aliased to this , which is why you can use this.redirect , but it really is a Response method. redirectthis别名,这就是为什么您可以使用this.redirect ,但它实际上是一种Response方法。 Take a look at http://koajs.com/#context for more details. 有关更多详细信息,请访问http://koajs.com/#context

To avoid having to assign this , or response , you could just bind this to your function, I think it is cleaner in most cases: 为了避免分配this ,或者response ,你可以只绑定this给你的函数,我认为这是在大多数情况下清洁:

myRouter.get('/signin', function* (next) {

    user = {...};

    this.req.login(user, function(err) {
        if (err)
            console.log('error logging in user - '+err);
        return this.redirect('/'); // <--- line 439
    }.bind(this));
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM