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

docs(techniques/authentication): add refresh-token implementation steps #1468

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

dinuka-rp
Copy link

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

[ ] Bugfix
[x] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] Other... Please describe:

What is the current behavior?

nestjs/jwt#122

Issue Number:

What is the new behavior?

Add documentation for refresh-token mechanism.

Does this PR introduce a breaking change?

[ ] Yes
[x] No

Other information

This NodeJS refresh-token implementation was used as reference to come up with the following refresh-token mechanism for NestJS.


When the JWT strategy is in play, the token will expire within a short time frame and the user will have to re-enter authentication details to generate a new JWT. Instead, the user can be sent a refresh-token together with a JWT at the time of authentication. This refresh-token would preferably have a different secret and a longer expiration time.

Since the refresh-token is generated at the same time as the JWT follwing a similar mechanism, it's convenint to keep this within the AuthModule.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small typo, convenint should be convenient


When the JWT strategy is in play, the token will expire within a short time frame and the user will have to re-enter authentication details to generate a new JWT. Instead, the user can be sent a refresh-token together with a JWT at the time of authentication. This refresh-token would preferably have a different secret and a longer expiration time.

Since the refresh-token is generated at the same time as the JWT follwing a similar mechanism, it's convenint to keep this within the AuthModule.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Since the refresh-token is generated at the same time as the JWT follwing a similar mechanism, it's convenint to keep this within the AuthModule.
Since the refresh-token is generated at the same time as the JWT following a similar mechanism, it's convenient to keep this within the `AuthModule`.

@@ -921,6 +921,159 @@ export class JwtStrategy extends PassportStrategy(Strategy, 'myjwt')

Then, you refer to this via a decorator like `@UseGuards(AuthGuard('myjwt'))`.

#### Refresh-Token Functionality

When the JWT strategy is in play, the token will expire within a short time frame and the user will have to re-enter authentication details to generate a new JWT. Instead, the user can be sent a refresh-token together with a JWT at the time of authentication. This refresh-token would preferably have a different secret and a longer expiration time.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@adamwdennis
Copy link

Thanks for this.. It took me a full day to find this answer to my problem with access/refresh tokens... i started by adding a custom claim 'token_type', but this makes SO much more sense (signing refresh tokens with different secrets). Thank you.

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

Successfully merging this pull request may close these issues.

5 participants