mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-11-26 08:34:55 +00:00
28 lines
892 B
TypeScript
28 lines
892 B
TypeScript
import { Injectable, UnauthorizedException } from '@nestjs/common';
|
|
import { ConfigService } from '@nestjs/config';
|
|
import { PassportStrategy } from '@nestjs/passport';
|
|
import { ExtractJwt, Strategy } from 'passport-jwt';
|
|
import { UserService } from '~/user/services';
|
|
import { IJwtPayload } from '../interfaces';
|
|
|
|
@Injectable()
|
|
export class AccessTokenStrategy extends PassportStrategy(Strategy, 'access-token') {
|
|
constructor(configService: ConfigService, private userService: UserService) {
|
|
super({
|
|
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
|
|
ignoreExpiration: false,
|
|
secretOrKey: configService.get<string>('JWT_ACCESS_TOKEN_SECRET'),
|
|
});
|
|
}
|
|
|
|
async validate(payload: IJwtPayload) {
|
|
const user = await this.userService.findUser({ id: payload.sub });
|
|
|
|
if (!user) {
|
|
throw new UnauthorizedException();
|
|
}
|
|
|
|
return payload;
|
|
}
|
|
}
|