Giter Club home page Giter Club logo

nestjs-jwts's Introduction

NestJs Jwt Authentication example with access token and refresh token - Integration and End-to-end tests included

This is an example of how to implement an authentication system in NestJs using passport.js, and json web tokens (JWT).

I've included integration tests in the auth module under "test" folder.

The e2e tests on the other hand are in the root test folder.

yarn test # run integration test
yarn test:e2e # run e2e tests

The code reflects what was explained in the video:

IMAGE ALT TEXT HERE

nestjs-jwts's People

Contributors

ant0x1 avatar vladwulf avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

nestjs-jwts's Issues

caActivate error

Sometimes when i start project i have next error

The inferred type of 'canActivate' cannot be named without a reference to '.pnpm/[email protected]/node_modules/rxjs'. This is likely not portable. A type annotation is necessary.

image

``
import { ExecutionContext, Injectable } from '@nestjs/common';
import { Reflector } from '@nestjs/core';
import { AuthGuard } from '@nestjs/passport';

@Injectable()
export class AtGuard extends AuthGuard('jwt') {
constructor(private reflector: Reflector) {
super();
}

canActivate(context: ExecutionContext) {
const isPublic = this.reflector.getAllAndOverride('isPublic', [
context.getHandler(),
context.getClass(),
]);

if (isPublic) return true;

return super.canActivate(context);

}
}

Error: Unknown authentication strategy "jwt-refresh"

I was following the tutorial as close as possible, I even have the exact same code

import { AuthGuard } from '@nestjs/passport';

export class RefreshTokenGuard extends AuthGuard('jwt-refresh') {
  constructor() {
    super();
  }
}

this is the error that I get

[Nest] 11492  - 21/12/2021, 11:45:25 pm   ERROR [ExceptionsHandler] Unknown authentication strategy "jwt-refresh"
Error: Unknown authentication strategy "jwt-refresh"
    at attempt (D:\dev\alejandria\jwt-test\node_modules\passport\lib\middleware\authenticate.js:193:39)
    at authenticate (D:\dev\alejandria\jwt-test\node_modules\passport\lib\middleware\authenticate.js:370:7)
    at D:\dev\alejandria\jwt-test\node_modules\@nestjs\passport\dist\auth.guard.js:91:3
    at new Promise (<anonymous>)
    at D:\dev\alejandria\jwt-test\node_modules\@nestjs\passport\dist\auth.guard.js:83:83
    at RefreshTokenGuard.<anonymous> (D:\dev\alejandria\jwt-test\node_modules\@nestjs\passport\dist\auth.guard.js:49:36)
    at Generator.next (<anonymous>)
    at fulfilled (D:\dev\alejandria\jwt-test\node_modules\@nestjs\passport\dist\auth.guard.js:17:58)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)

Strategy jwt-refresh is confusing

Thanks for the tutorial, it is awsome.

But jwt-refresh Strategy for me looks strange.
Why you provide refresh-token in a Header Authorization with Bearer (tag?).
Replacing Access-tokeen only for one link with the same Bearer tag looks strange.

Maby is better to put refresh-token in body? for this refresh action?

Thanks again for tutorial!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.