forked from Yara724/api
29 lines
926 B
TypeScript
29 lines
926 B
TypeScript
import { ExecutionContext, Injectable } from "@nestjs/common";
|
|
import { AuthGuard } from "@nestjs/passport";
|
|
import {
|
|
UserAuthErrorCode,
|
|
throwUserAuthError,
|
|
} from "src/auth/auth-services/user-auth-error";
|
|
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, linkToken, linkContext } = request.body;
|
|
const isValidUser = await this.userAuthService.validateUser(
|
|
username,
|
|
password,
|
|
{ linkToken, linkContext },
|
|
);
|
|
if (!isValidUser) {
|
|
throwUserAuthError(UserAuthErrorCode.OTP_INVALID);
|
|
}
|
|
request["user"] = isValidUser;
|
|
return true;
|
|
}
|
|
}
|