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

[Feature Request] Custom Callback #1

Closed
cdiaz opened this issue Apr 29, 2018 · 1 comment
Closed

[Feature Request] Custom Callback #1

cdiaz opened this issue Apr 29, 2018 · 1 comment

Comments

@cdiaz
Copy link

cdiaz commented Apr 29, 2018

By default, if authentication fails, the AuthGuard will respond with a generic unauthorized error message.

But In some cases the built-in options are not sufficient to handle an authentication request.
Then it should be possible provide a Custom Callback to allow the application to handle success or failure.
For example:

const CustomCallback = (err, user, info) => {
  let message
  if (err) {
    return Promise.reject(err || new UnauthorizedException());
  } else if (typeof info != 'undefined' || !user) {
    switch (info.message) {
      case 'No auth token':
      case 'invalid signature':
      case 'jwt malformed':
      case 'invalid token':
        message = "You must provide a valid authenticated access token"
        break
      case 'jwt expired':
        message = "Your session has expired. Please log in again"
        break
    }
    return Promise.reject(new UnauthorizedException(message))
  }
  Promise.resolve(user);
}
  @Get('data')
  @UseGuards(AuthGuard('jwt', { session: false }, CustomCallback))
  getSensitiveData() {
    // this route is restricted
  }
@kamilmysliwiec
Copy link
Member

kamilmysliwiec commented May 1, 2018

Fixed in v1.0.5
Example:

@Get('data')
@UseGuards(AuthGuard('jwt', { session: false, callback }))
getSensitiveData() {
   // this route is restricted
}

when the callback looks like below:

const callback = (err, user, info) => {
   if (err || !user) {
      throw err || new UnauthorizedException();
   }
   return user;
}

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

No branches or pull requests

2 participants