forked from Yara724/api
28 lines
795 B
TypeScript
28 lines
795 B
TypeScript
import {
|
|
ExecutionContext,
|
|
Injectable,
|
|
NotAcceptableException,
|
|
} from "@nestjs/common";
|
|
import { AuthGuard } from "@nestjs/passport";
|
|
import { UserAuthService } from "src/auth/auth-services/user.auth.service";
|
|
|
|
@Injectable()
|
|
export class LocalUserAuthGuard extends AuthGuard("local") {
|
|
constructor(private readonly userAuthService: UserAuthService) {
|
|
super();
|
|
}
|
|
async canActivate(context: ExecutionContext): Promise<boolean> {
|
|
const request = context.switchToHttp().getRequest();
|
|
const { username, password } = request.body;
|
|
let isValidUser = await this.userAuthService.validateUser(
|
|
username,
|
|
password,
|
|
);
|
|
if (!isValidUser) {
|
|
throw new NotAcceptableException("otp is wrong");
|
|
}
|
|
request["user"] = isValidUser;
|
|
return true;
|
|
}
|
|
}
|