import { Injectable, UnauthorizedException } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { Strategy } from 'passport-local';
import { AuthService } from '../auth.service';
@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy) {
constructor(private readonly authService: AuthService) {
super();
}
async validate(email: string, password: string): Promise
const user = await this.authService.validateUser(email, password);
if (!user) {
throw new UnauthorizedException();
}
return user;
}
}
This is my code
It'll return the Unauthorized when I request the interface and add email and password
Please, use our Discord channel (support) for such questions. We are using GitHub to track bugs, feature requests, and potential improvements.
I connot connect the website, maybe because I'm in China ?
Hi @MutelyMilo. This section of the docs shows how you can change the default username/password fields and send other options to a passport strategy:
https://docs.nestjs.com/techniques/authentication#customize-passport
(Customize Passport in the sidebar in case the link does not go to the correct section of the page)
Taking your example, it would look like this:
@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy) {
constructor(private readonly authService: AuthService) {
super({ usernameField: "email" })
}
async validate(email: string, password: string) {
// ...
}
}
Most helpful comment
Hi @MutelyMilo. This section of the docs shows how you can change the default username/password fields and send other options to a passport strategy:
https://docs.nestjs.com/techniques/authentication#customize-passport
(Customize Passport in the sidebar in case the link does not go to the correct section of the page)
Taking your example, it would look like this: