Files
mp-pilates/packages/server/src/auth/jwt.strategy.ts
2026-04-04 15:39:26 +08:00

30 lines
807 B
TypeScript

import { Injectable } from '@nestjs/common'
import { PassportStrategy } from '@nestjs/passport'
import { ExtractJwt, Strategy } from 'passport-jwt'
import { ConfigService } from '@nestjs/config'
import { UserRole } from '@mp-pilates/shared'
import { JwtPayload } from './auth.service'
export interface AuthenticatedUser {
sub: string
role: UserRole
}
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
constructor(configService: ConfigService) {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
ignoreExpiration: false,
secretOrKey: configService.getOrThrow<string>('JWT_SECRET'),
})
}
validate(payload: JwtPayload): { sub: string; role: UserRole } {
return {
sub: payload.sub,
role: payload.role,
}
}
}