question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JWT: How to access it after using UseGuards?

See original GitHub issue

I’m submitting a…


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

Current behavior

I have no idea how to access a JWT token and it’s missing in doc/github example.

Let’s take this one: https://github.com/nestjs/nest/blob/master/sample/19-auth/src/auth/auth.controller.ts#L16

Here, we’re securing the route:

  @Get('data')
  @UseGuards(AuthGuard('jwt'))
  findAll() {
    // this route is restricted
  }

But how can we have access to the current JWT (to retrieve user id or email for example).

Environment


Nest version: 5.0.0

For Tooling issues:
- Node version: 8.9.4
- Platform:  Linux

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7

github_iconTop GitHub Comments

13reactions
adrien2pcommented, May 13, 2018

If you don’t pass any options to auth guard, you should have access to the user by calling req.user

5reactions
zhmushancommented, Jun 6, 2018

Thanks @maxime1992 . Everything suddenly understood, after seeing export const Usr = createParamDecorator((data,req)=> req.user); This is a huge help for me, thanks again.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Authentication | NestJS - A progressive Node.js framework
For this use case, clients will start by authenticating with a username and password. Once authenticated, the server will issue a JWT that...
Read more >
Getting User Data by using Guards (Roles, JWT)
It works since I use @UseGuards(AuthGuard('jwt'), RolesGuard) as decorator. I extending AuthGuard for my RolesGuard and overwriting the ...
Read more >
How to implement JWT authentication in NestJS
For this demonstration, we'll use Yarn. Terminal Yarn. Choose yarn and press the Enter key. Now, wait while Yarn installs all the ...
Read more >
How to implement NestJS JWT Authentication using JWT ...
In this post, we will learn how to implement NestJS JWT Authentication using Passport JWT Strategy. JWT stands for JSON Web Tokens.
Read more >
NestJS JWT Authentication with Refresh Tokens Complete ...
We're using MongoDB as the database for our application. ... We did that so we can access it when we need to refresh...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found