import { RedisService } from '@/common/modules/redis/providers/redis.service'; import { Injectable } from '@nestjs/common'; @Injectable() export class AuthRedisProvider { constructor( /** * Injecting Redis Service */ private readonly redisService: RedisService, ) {} async storeRefreshToken(jti: string, userId: number, ttl: number) { await this.redisService.set(`rt:${jti}`, userId.toString(), ttl); } async consumeRefreshToken(jti: string, userId: number): Promise { const consumed = await this.redisService.client.eval( `if redis.call('GET', KEYS[1]) == ARGV[1] then return redis.call('DEL', KEYS[1]) end return 0`, 1, `rt:${jti}`, JSON.stringify(userId.toString()), ); return consumed === 1; } async revokeRefreshToken(jti: string) { await this.redisService.del(`rt:${jti}`); } async blacklistAccessToken(jti: string, ttl: number) { if (ttl <= 0) return; await this.redisService.set(`at:bl:${jti}`, '1', ttl); } async isAccessTokenBlacklisted(jti: string) { return !!(await this.redisService.get(`at:bl:${jti}`)); } }