mirror of
https://gitee.com/qqtt-ah/ai-crm.git
synced 2026-11-24 13:09:20 +08:00
27 lines
716 B
TypeScript
27 lines
716 B
TypeScript
import { Injectable, UnauthorizedException } from '@nestjs/common';
|
|
import { PassportStrategy } from '@nestjs/passport';
|
|
import { ExtractJwt, Strategy } from 'passport-jwt';
|
|
|
|
export interface AdminJwtPayload {
|
|
sub: number;
|
|
username: string;
|
|
}
|
|
|
|
@Injectable()
|
|
export class JwtStrategy extends PassportStrategy(Strategy) {
|
|
constructor() {
|
|
super({
|
|
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
|
|
ignoreExpiration: false,
|
|
secretOrKey: process.env.JWT_SECRET || 'change-me-in-production',
|
|
});
|
|
}
|
|
|
|
validate(payload: AdminJwtPayload) {
|
|
if (!payload?.sub) {
|
|
throw new UnauthorizedException();
|
|
}
|
|
return { adminId: payload.sub, username: payload.username };
|
|
}
|
|
}
|