mirror of
https://github.com/AmruthPillai/Reactive-Resume.git
synced 2025-11-18 02:31:56 +10:00
refactor(v4.0.0-alpha): beginning of a new era
This commit is contained in:
35
apps/server/src/auth/strategy/two-factor.strategy.ts
Normal file
35
apps/server/src/auth/strategy/two-factor.strategy.ts
Normal file
@ -0,0 +1,35 @@
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { ConfigService } from "@nestjs/config";
|
||||
import { PassportStrategy } from "@nestjs/passport";
|
||||
import type { Request } from "express";
|
||||
import { ExtractJwt, Strategy, StrategyOptions } from "passport-jwt";
|
||||
|
||||
import { Config } from "@/server/config/schema";
|
||||
import { UserService } from "@/server/user/user.service";
|
||||
|
||||
import { Payload } from "../utils/payload";
|
||||
|
||||
@Injectable()
|
||||
export class TwoFactorStrategy extends PassportStrategy(Strategy, "two-factor") {
|
||||
constructor(
|
||||
private readonly configService: ConfigService<Config>,
|
||||
private readonly userService: UserService,
|
||||
) {
|
||||
const extractors = [(request: Request) => request?.cookies?.Authentication];
|
||||
|
||||
super({
|
||||
secretOrKey: configService.get<string>("ACCESS_TOKEN_SECRET"),
|
||||
jwtFromRequest: ExtractJwt.fromExtractors(extractors),
|
||||
ignoreExpiration: false,
|
||||
} as StrategyOptions);
|
||||
}
|
||||
|
||||
async validate(payload: Payload) {
|
||||
const user = await this.userService.findOneById(payload.id);
|
||||
|
||||
// If the user has 2FA disabled, this will follow the same route as JWT Strategy
|
||||
if (!user.twoFactorEnabled) return user;
|
||||
|
||||
if (payload.isTwoFactorAuth) return user;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user