33 lines
893 B
TypeScript
33 lines
893 B
TypeScript
import { Injectable, UnauthorizedException } from '@nestjs/common';
|
|||
|
|
import { ConfigService } from '@nestjs/config';
|
||
|
|
import { PassportStrategy } from '@nestjs/passport';
|
||
|
|
import { ExtractJwt, Strategy } from 'passport-jwt';
|
||
|
|
|
||
|
|
export type JwtPayload = {
|
||
|
|
sub: string;
|
||
|
|
organizationId: string;
|
||
|
|
role: string;
|
||
|
|
};
|
||
|
|
|
||
|
|
@Injectable()
|
||
|
|
export class JwtStrategy extends PassportStrategy(Strategy) {
|
||
|
|
constructor(config: ConfigService) {
|
||
|
|
super({
|
||
|
|
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
|
||
|
|
ignoreExpiration: false,
|
||
|
|
secretOrKey: config.get<string>('JWT_SECRET') ?? 'dev-secret',
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
validate(payload: JwtPayload & { typ?: string; mobile?: string }) {
|
||
|
|
if (payload.typ === 'portal') {
|
||
|
|
throw new UnauthorizedException();
|
||
|
|
}
|
||
|
|
return {
|
||
|
|
userId: payload.sub,
|
||
|
|
organizationId: payload.organizationId,
|
||
|
|
role: payload.role,
|
||
|
|
};
|
||
|
|
}
|
||
|
|
}
|