parent
05f97c8300
commit
65e3afd64f
12 changed files with 204 additions and 78 deletions
@ -1,9 +1,11 @@ |
|||||||
import { Module } from '@nestjs/common'; |
import { Module } from '@nestjs/common'; |
||||||
import { JwtService } from '@nestjs/jwt'; |
import { JwtService } from '@nestjs/jwt'; |
||||||
import { JwtAuthGuard } from './auth.guard';
|
import { JwtAuthGuard } from './auth.guard';
|
||||||
|
import { RedisService } from 'src/redis/redis.service'; |
||||||
|
import { JwtStrategy } from './jwt.strategy'; |
||||||
|
|
||||||
@Module({ |
@Module({ |
||||||
providers: [JwtService, JwtAuthGuard], |
providers: [JwtService, JwtAuthGuard,RedisService,JwtStrategy], |
||||||
exports: [JwtService, JwtAuthGuard],
|
exports: [JwtService, JwtAuthGuard],
|
||||||
}) |
}) |
||||||
export class AuthModule {} |
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 { Module } from "@nestjs/common"; |
||||||
import { SequelizeModule } from '@nestjs/sequelize'; |
import { SequelizeModule } from "@nestjs/sequelize"; |
||||||
import { ConfigModule, ConfigService } from '@nestjs/config'; |
import { ConfigModule, ConfigService } from "@nestjs/config"; |
||||||
import { JwtModule } from '@nestjs/jwt'; |
import { JwtModule } from "@nestjs/jwt"; |
||||||
import { PassportModule } from '@nestjs/passport'; |
import { PassportModule } from "@nestjs/passport"; |
||||||
import { UsersService } from './users.service'; |
import { UsersService } from "./users.service"; |
||||||
import { UsersController } from './users.controller'; |
import { UsersController } from "./users.controller"; |
||||||
import { User } from './entities/user.entity'; |
import { User } from "./entities/user.entity"; |
||||||
import { JwtAuthGuard } from 'src/users/guard/auth.guard'; |
import { JwtAuthGuard } from "src/users/guard/auth.guard"; |
||||||
|
import { JwtStrategy } from "./guard/jwt.strategy";
|
||||||
|
import { RedisService } from "src/redis/redis.service"; |
||||||
|
|
||||||
@Module({ |
@Module({ |
||||||
imports: [SequelizeModule.forFeature([User]), |
imports: [ |
||||||
PassportModule.register({ defaultStrategy: 'jwt' }), |
SequelizeModule.forFeature([User]), |
||||||
|
PassportModule.register({ defaultStrategy: "jwt" }), |
||||||
JwtModule.registerAsync({ |
JwtModule.registerAsync({ |
||||||
imports: [ConfigModule], |
imports: [ConfigModule], |
||||||
inject: [ConfigService], |
inject: [ConfigService], |
||||||
useFactory: (config: ConfigService) => { |
useFactory: (config: ConfigService) => { |
||||||
return { |
return { |
||||||
secret: config.get<string>('JWT_SECRET'), |
secret: config.get<string>("JWT_SECRET"), |
||||||
signOptions: { |
signOptions: { |
||||||
expiresIn: config.get<string | number>('JWT_EXPIRES', '1h'), |
expiresIn: config.get<string | number>("JWT_EXPIRES", "1h"), |
||||||
}, |
}, |
||||||
}; |
}; |
||||||
}, |
}, |
||||||
}), |
}), |
||||||
],
|
], |
||||||
controllers: [UsersController], |
controllers: [UsersController], |
||||||
providers: [UsersService,JwtAuthGuard], |
providers: [UsersService, JwtAuthGuard, RedisService, JwtStrategy], |
||||||
}) |
}) |
||||||
export class UsersModule {} |
export class UsersModule {} |
||||||
|
Loading…
Reference in new issue