UNPKG

@10abdullahbutt/auth-module

Version:

A NestJS-style authentication module with JWT and role-based access control.

23 lines (20 loc) 800 B
import { Injectable } from '@nestjs/common'; import { PassportStrategy } from '@nestjs/passport'; import { ExtractJwt, Strategy } from 'passport-jwt'; import { User } from '../interfaces/user.interface'; import { ConfigService } from '@nestjs/config'; @Injectable() export class JwtStrategy extends PassportStrategy(Strategy) { constructor(private readonly configService: ConfigService) { super({ jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), ignoreExpiration: false, secretOrKey: configService.get<string>('JWT_SECRET', 'secret'), passReqToCallback: false, }); } async validate(payload: any): Promise<User> { // You can add more validation logic here return { userId: payload.sub, username: payload.username, roles: payload.roles }; } }