2026-07-23 10:25:32 +03:30
|
|
|
import { Injectable } from '@nestjs/common';
|
|
|
|
|
import { ConfigService } from '@nestjs/config';
|
|
|
|
|
import { PassportStrategy } from '@nestjs/passport';
|
|
|
|
|
import { ExtractJwt, Strategy } from 'passport-jwt';
|
|
|
|
|
import { PrismaService } from '../prisma/prisma.service';
|
2026-07-30 18:19:53 +03:30
|
|
|
import { displayName } from '../common/mobile.util';
|
2026-07-23 10:25:32 +03:30
|
|
|
|
|
|
|
|
export type JwtPayload = {
|
|
|
|
|
sub: string;
|
2026-07-30 18:19:53 +03:30
|
|
|
mobile: string;
|
2026-07-23 10:25:32 +03:30
|
|
|
role: string;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class JwtStrategy extends PassportStrategy(Strategy) {
|
|
|
|
|
constructor(
|
|
|
|
|
config: ConfigService,
|
|
|
|
|
private readonly prisma: PrismaService,
|
|
|
|
|
) {
|
|
|
|
|
super({
|
|
|
|
|
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
|
|
|
|
|
ignoreExpiration: false,
|
|
|
|
|
secretOrKey: config.get<string>('JWT_SECRET', 'dev-secret'),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async validate(payload: JwtPayload) {
|
|
|
|
|
const user = await this.prisma.user.findUnique({
|
|
|
|
|
where: { id: payload.sub },
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
2026-07-30 18:19:53 +03:30
|
|
|
mobile: true,
|
|
|
|
|
firstName: true,
|
|
|
|
|
lastName: true,
|
2026-07-23 10:25:32 +03:30
|
|
|
role: true,
|
2026-08-10 15:29:04 +03:30
|
|
|
permissions: true,
|
2026-07-23 10:25:32 +03:30
|
|
|
isActive: true,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!user || !user.isActive) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-30 18:19:53 +03:30
|
|
|
return {
|
|
|
|
|
...user,
|
|
|
|
|
name: displayName(user.firstName, user.lastName),
|
|
|
|
|
};
|
2026-07-23 10:25:32 +03:30
|
|
|
}
|
|
|
|
|
}
|