mirror of
https://gitee.com/qqtt-ah/ai-crm.git
synced 2026-11-24 20:29:20 +08:00
init
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
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 };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user