diff --git a/src/redis/redis.service.ts b/src/redis/redis.service.ts new file mode 100644 index 0000000..44b4a39 --- /dev/null +++ b/src/redis/redis.service.ts @@ -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 { + 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 { + 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 { + const key = `${userId}:${jti}`; + await this.redis.del(key); + } +}