OBS: it's a code from nest auth tutorial: https://docs.nestjs.com/recipes/passport
Git repo: https://github.com/Mario-aj/nestjs-auth-systems/tree/main/nest-auth-tutorial
So basically it's working fine at the first time I do a login request, but the second and subsequent times it's returning 401, I just want to understand if this behavior is expected or not, because when I restart the server it works fine for the first one and then, the error again and again and again.
There is some way to fix or adjust this behavior? I want to call my login route how many times I want with the same user, and it must still authenticate my user.
import { Controller, Get, Request, Post, UseGuards } from '@nestjs/common';
import { JwtAuthGuard } from './auth/jwt-auth.guard';
import { LocalAuthGuard } from './auth/local-auth.guard';
import { AuthService } from './auth/auth.service';
import { PublicRoute } from './utils/public-routes.decorator';
@Controller()
export class AppController {
constructor(private authService: AuthService) {}
@PublicRoute()
@Post('auth/login')
@UseGuards(LocalAuthGuard)
async login(@Request() req) {
return this.authService.login(req.user);
}
@UseGuards(JwtAuthGuard)
@Get('profile')
getProfile(@Request() req) {
return req.user;
}
}
import { ExecutionContext, Injectable } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { Observable } from 'rxjs';
@Injectable()
export class LocalAuthGuard extends AuthGuard('local') {
canActivate(
context: ExecutionContext,
): boolean | Promise<boolean> | Observable<boolean> {
return super.canActivate(context);
}
}
import { ExecutionContext, Injectable } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { Observable } from 'rxjs';
@Injectable()
export class LocalAuthGuard extends AuthGuard('local') {
}
Nest is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with TypeScript and combines elements of OOP (Object Oriented Programming), FP (Functional Programming), and FRP (Functional Reactive Programming).