mirror of
https://git.meshkee.com/BaloutPastry/backend.git
synced 2026-08-11 22:31:00 +04:30
46 lines
1.2 KiB
TypeScript
46 lines
1.2 KiB
TypeScript
import { Injectable, UnauthorizedException } from '@nestjs/common';
|
|
import { ConfigService } from '@nestjs/config';
|
|
import { PassportStrategy } from '@nestjs/passport';
|
|
import { UserRole } from '@prisma/client';
|
|
import { ExtractJwt, Strategy } from 'passport-jwt';
|
|
import { PrismaService } from '../../prisma/prisma.service';
|
|
import { AuthUser } from '../auth.types';
|
|
|
|
type JwtPayload = {
|
|
sub: string;
|
|
role: UserRole;
|
|
};
|
|
|
|
@Injectable()
|
|
export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') {
|
|
constructor(
|
|
config: ConfigService,
|
|
private readonly prisma: PrismaService,
|
|
) {
|
|
super({
|
|
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
|
|
ignoreExpiration: false,
|
|
secretOrKey: config.getOrThrow<string>('JWT_ACCESS_SECRET'),
|
|
});
|
|
}
|
|
|
|
async validate(payload: JwtPayload): Promise<AuthUser> {
|
|
const user = await this.prisma.user.findUnique({
|
|
where: { id: payload.sub },
|
|
});
|
|
|
|
if (!user || user.disabled) {
|
|
throw new UnauthorizedException('نشست نامعتبر است');
|
|
}
|
|
|
|
return {
|
|
id: user.id,
|
|
cellNumber: user.cellNumber,
|
|
role: user.role,
|
|
firstName: user.firstName,
|
|
lastName: user.lastName,
|
|
title: user.title,
|
|
};
|
|
}
|
|
}
|