desker/apps/api/src/auth/strategies/jwt.strategy.ts

30 lines
766 B
TypeScript

import { Injectable } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { ExtractJwt, Strategy } from 'passport-jwt';
import { ConfigService } from '@nestjs/config';
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
constructor(configService: ConfigService) {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
ignoreExpiration: false,
secretOrKey: configService.get('JWT_SECRET') || 'secret',
});
}
validate(payload: {
sub: string;
email: string;
role: string;
organizationId: string;
}) {
return {
id: payload.sub,
email: payload.email,
role: payload.role,
organizationId: payload.organizationId,
};
}
}