From a71b39090e23d2a3de195c70b42799fd76497c73 Mon Sep 17 00:00:00 2001 From: Louis Auzuret Date: Mon, 20 Jun 2022 15:48:55 +0200 Subject: [PATCH] feat: jwt strategy for passport --- back/src/auth/jwt.strategy.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 back/src/auth/jwt.strategy.ts diff --git a/back/src/auth/jwt.strategy.ts b/back/src/auth/jwt.strategy.ts new file mode 100644 index 0000000..a03932e --- /dev/null +++ b/back/src/auth/jwt.strategy.ts @@ -0,0 +1,20 @@ + +import { ExtractJwt, Strategy } from 'passport-jwt'; +import { PassportStrategy } from '@nestjs/passport'; +import { Injectable } from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; + +@Injectable() +export class JwtStrategy extends PassportStrategy(Strategy) { + constructor(private configService: ConfigService) { + super({ + jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), + ignoreExpiration: false, + secretOrKey: configService.get('JWT_SECRET'), + }); + } + + async validate(payload: any) { + return { id: payload.id, username: payload.username }; + } +} \ No newline at end of file