forked from Yara724/api
31 lines
884 B
TypeScript
31 lines
884 B
TypeScript
import {
|
|
CanActivate,
|
|
ExecutionContext,
|
|
Injectable,
|
|
} from "@nestjs/common";
|
|
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 implements CanActivate {
|
|
constructor(private readonly userAuthService: UserAuthService) {}
|
|
|
|
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;
|
|
}
|
|
}
|