Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when use bearer strategy #17

Closed
krivochenko opened this issue Sep 3, 2018 · 4 comments
Closed

Error when use bearer strategy #17

krivochenko opened this issue Sep 3, 2018 · 4 comments
Labels
question Further information is requested

Comments

@krivochenko
Copy link

I'm submitting a...


[ ] Regression 
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

After update to 5.0.1 I get strange error when use @UseGuards(AuthGuard('bearer'))

{ Error: Pool is closed.
        at Pool.Object.<anonymous>.Pool.getConnection (C:\www\gmc\back\node_modules\mysql\lib\Pool.js:25:15)
        at C:\www\gmc\back\src\driver\mysql\MysqlDriver.ts:566:27
        at new Promise (<anonymous>)
        at MysqlDriver.Object.<anonymous>.MysqlDriver.obtainMasterConnection (C:\www\gmc\back\src\driver\mysql\MysqlDriver.ts:559:16)
        at MysqlQueryRunner.Object.<anonymous>.MysqlQueryRunner.connect (C:\www\gmc\back\src\driver\mysql\MysqlQueryRunner.ts:79:58)
        at MysqlQueryRunner.<anonymous> (C:\www\gmc\back\src\driver\mysql\MysqlQueryRunner.ts:143:55)
        at step (C:\www\gmc\back\node_modules\typeorm\driver\mysql\MysqlQueryRunner.js:42:23)
        at Object.next (C:\www\gmc\back\node_modules\typeorm\driver\mysql\MysqlQueryRunner.js:23:53)
        at C:\www\gmc\back\node_modules\typeorm\driver\mysql\MysqlQueryRunner.js:17:71
        at new Promise (<anonymous>) code: 'POOL_CLOSED' }

Code of my BearerStrategy:

import { Strategy } from 'passport-http-bearer';
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { OauthClientTokenService } from './client/token/oauth-client-token.service';
import { PassportStrategy } from '@nestjs/passport';

@Injectable()
export class BearerStrategy extends PassportStrategy(Strategy) {
  constructor(private readonly clientTokenService: OauthClientTokenService) {
    super();
  }

  validate(token: any, done) {
    this.clientTokenService.findByAccessToken(token).then(async (clientToken) => {
      if (!!clientToken) {
        const user = await clientToken.user;
        done(null, user);
      } else {
        done(new UnauthorizedException(), false);
      }
    }).catch((error) => {
      console.log(error);
      done(error, false);
    });
  }
}

Method findByAccessToken correctly returns token-entity. But querying of user causes error.

Environment


Nest version: 5.3.0

 
For Tooling issues:
- Node version: 10.1.0
- Platform:  Windows
@kamilmysliwiec
Copy link
Member

Honestly, I don't think that this issue has something to do with @nestjs/passport

@kamilmysliwiec kamilmysliwiec added the question Further information is requested label Sep 5, 2018
@krivochenko
Copy link
Author

krivochenko commented Sep 5, 2018

I several type have changed version of @nestjs/passport from ^1.1.0 to ^5.0.1 and back. I observe issue only on ^5.0.1. In nearest time I`ll try to extract some part of my project for reproduce bug.

@kamilmysliwiec
Copy link
Member

kamilmysliwiec commented Sep 9, 2018

Looking forward to it :)

@krivochenko
Copy link
Author

In the end I refactor my code and it solved my issue:
Before:

  validate(token: any, done) {
    this.clientTokenService.findByAccessToken(token).then(async (clientToken) => {
      if (!!clientToken) {
        const user = await clientToken.user;
        done(null, user);
      } else {
        done(new UnauthorizedException(), false);
      }
    }).catch((error) => {
      done(error, false);
    });
  }

After:

  async validate(token: any, done) {
    const clientToken = await this.clientTokenService.findByAccessToken(token);
    if (!!clientToken) {
      const user = await clientToken.user;
      done(null, user);
    } else {
      done(new UnauthorizedException(), false);
    }
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants