From 05f97c830031d42ebd266969693a26c25c523908 Mon Sep 17 00:00:00 2001 From: aliMohtarami Date: Mon, 27 Jan 2025 14:51:20 +0330 Subject: [PATCH] redis connection --- src/redis/redis.service.ts | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/redis/redis.service.ts 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); + } +}