parent
fc67949151
commit
05f97c8300
1 changed files with 32 additions and 0 deletions
@ -0,0 +1,32 @@ |
|||||||
|
import { Injectable } from '@nestjs/common'; |
||||||
|
import Redis from 'ioredis'; |
||||||
|
|
||||||
|
@Injectable() |
||||||
|
export class RedisService { |
||||||
|
private readonly redis: Redis; |
||||||
|
|
||||||
|
constructor() { |
||||||
|
this.redis = new Redis({ |
||||||
|
host: 'localhost',
|
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
// Add a userId and JTI pair to the whitelist with TTL (Time-To-Live)
|
||||||
|
async addToWhitelist(userId: string, jti: string, ttl: number): Promise<void> { |
||||||
|
const key = `${userId}:${jti}`;
|
||||||
|
await this.redis.set(key, 'valid', 'EX', ttl); |
||||||
|
} |
||||||
|
|
||||||
|
// Check if the userId and JTI pair exists in the whitelist
|
||||||
|
async isWhitelisted(userId: string, jti: string): Promise<boolean> { |
||||||
|
const key = `${userId}:${jti}`; |
||||||
|
const value = await this.redis.get(key); |
||||||
|
return value === 'valid'; |
||||||
|
} |
||||||
|
|
||||||
|
// Delete a userId and JTI pair from the whitelist
|
||||||
|
async deleteFromWhitelist(userId: string, jti: string): Promise<void> { |
||||||
|
const key = `${userId}:${jti}`; |
||||||
|
await this.redis.del(key); |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue