I try to use a NestJS backend with a Nginx reverse proxy. I have coded an authentication part in my NestJS backend.
My problem is that when I used my frontend / backend in local mode, all is ok. When I use it through Nginx, I always retrieve a 401 error. I think it’s due to the LocalStrategy in NestJS
Here is the part in the local.strategy.ts file
import { Strategy } from 'passport-local';
import { PassportStrategy } from '@nestjs/passport';
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { AuthService } from '../auth.service';
@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy) {
constructor(private authService: AuthService) {
super({
usernameField: 'userLogin',
passwordField: 'userPassword',
});
}
async validate(userLogin: string, userPassword: string): Promise<any> {
const user = await this.authService.validateUser(userLogin, userPassword);
if (!user) {
throw new UnauthorizedException();
}
return user;
}
}
Here is the part in the app.controller.ts file
@Public()
@UseGuards(LocalAuthGuard)
@Post('auth/login')
async login(@Request() req) {
return this.authService.login(req.user);
}
But I don’t know how to change it. If somebody have an example it build be great. Thanks in advance.
I've tested with curl locally on server (without nginx). I saw my error (mysql access to test the user), it was not due to nginx configuration.