parent
05f97c8300
commit
65e3afd64f
12 changed files with 204 additions and 78 deletions
@ -1,9 +1,11 @@ |
||||
import { Module } from '@nestjs/common'; |
||||
import { JwtService } from '@nestjs/jwt'; |
||||
import { JwtAuthGuard } from './auth.guard';
|
||||
import { RedisService } from 'src/redis/redis.service'; |
||||
import { JwtStrategy } from './jwt.strategy'; |
||||
|
||||
@Module({ |
||||
providers: [JwtService, JwtAuthGuard], |
||||
providers: [JwtService, JwtAuthGuard,RedisService,JwtStrategy], |
||||
exports: [JwtService, JwtAuthGuard],
|
||||
}) |
||||
export class AuthModule {} |
||||
|
@ -0,0 +1,26 @@ |
||||
import { PassportStrategy } from '@nestjs/passport'; |
||||
import { Strategy, ExtractJwt } from 'passport-jwt'; |
||||
import { Injectable, UnauthorizedException } from '@nestjs/common'; |
||||
import { RedisService } from 'src/redis/redis.service'; |
||||
|
||||
@Injectable() |
||||
export class JwtStrategy extends PassportStrategy(Strategy) { |
||||
constructor(private readonly redisService: RedisService) { |
||||
super({ |
||||
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), |
||||
secretOrKey: process.env.JWT_SECRET, |
||||
}); |
||||
} |
||||
|
||||
async validate(payload: any): Promise<any> { |
||||
const { jti, id } = payload; |
||||
|
||||
const response = await this.redisService.isWhitelisted(id, jti) |
||||
console.log(response) |
||||
if (!response) { |
||||
throw new UnauthorizedException('Token is not whitelisted'); |
||||
} |
||||
|
||||
return payload;
|
||||
} |
||||
} |
@ -1,30 +1,33 @@ |
||||
import { Module } from '@nestjs/common'; |
||||
import { SequelizeModule } from '@nestjs/sequelize'; |
||||
import { ConfigModule, ConfigService } from '@nestjs/config'; |
||||
import { JwtModule } from '@nestjs/jwt'; |
||||
import { PassportModule } from '@nestjs/passport'; |
||||
import { UsersService } from './users.service'; |
||||
import { UsersController } from './users.controller'; |
||||
import { User } from './entities/user.entity'; |
||||
import { JwtAuthGuard } from 'src/users/guard/auth.guard'; |
||||
import { Module } from "@nestjs/common"; |
||||
import { SequelizeModule } from "@nestjs/sequelize"; |
||||
import { ConfigModule, ConfigService } from "@nestjs/config"; |
||||
import { JwtModule } from "@nestjs/jwt"; |
||||
import { PassportModule } from "@nestjs/passport"; |
||||
import { UsersService } from "./users.service"; |
||||
import { UsersController } from "./users.controller"; |
||||
import { User } from "./entities/user.entity"; |
||||
import { JwtAuthGuard } from "src/users/guard/auth.guard"; |
||||
import { JwtStrategy } from "./guard/jwt.strategy";
|
||||
import { RedisService } from "src/redis/redis.service"; |
||||
|
||||
@Module({ |
||||
imports: [SequelizeModule.forFeature([User]), |
||||
PassportModule.register({ defaultStrategy: 'jwt' }), |
||||
imports: [ |
||||
SequelizeModule.forFeature([User]), |
||||
PassportModule.register({ defaultStrategy: "jwt" }), |
||||
JwtModule.registerAsync({ |
||||
imports: [ConfigModule], |
||||
inject: [ConfigService], |
||||
useFactory: (config: ConfigService) => { |
||||
return { |
||||
secret: config.get<string>('JWT_SECRET'), |
||||
secret: config.get<string>("JWT_SECRET"), |
||||
signOptions: { |
||||
expiresIn: config.get<string | number>('JWT_EXPIRES', '1h'), |
||||
expiresIn: config.get<string | number>("JWT_EXPIRES", "1h"), |
||||
}, |
||||
}; |
||||
}, |
||||
}), |
||||
],
|
||||
], |
||||
controllers: [UsersController], |
||||
providers: [UsersService,JwtAuthGuard], |
||||
providers: [UsersService, JwtAuthGuard, RedisService, JwtStrategy], |
||||
}) |
||||
export class UsersModule {} |
||||
|
Loading…
Reference in new issue