Files
kycnotme/web/src/lib/redis/redisPreGeneratedSecretTokens.ts

35 lines
1.0 KiB
TypeScript
Raw Normal View History

2025-05-19 10:23:36 +00:00
import { RedisGenericManager } from './redisGenericManager'
class RedisPreGeneratedSecretTokens extends RedisGenericManager {
2025-06-09 10:00:55 +00:00
private readonly prefix = 'pregenerated_user_secret_token:'
2025-05-19 10:23:36 +00:00
/**
* Stores a pre-generated token with expiration
* @param token The pre-generated token
*/
async storePreGeneratedToken(token: string): Promise<void> {
2025-06-09 10:00:55 +00:00
await this.redisClient.set(`${this.prefix}${token}`, '1', {
2025-05-19 10:23:36 +00:00
EX: this.expirationTime,
})
}
/**
* Validates and consumes a pre-generated token
* @param token The token to validate
* @returns true if token was valid and consumed, false otherwise
*/
async validateAndConsumePreGeneratedToken(token: string): Promise<boolean> {
2025-06-09 10:00:55 +00:00
const key = `${this.prefix}${token}`
2025-05-19 10:23:36 +00:00
const exists = await this.redisClient.exists(key)
if (exists) {
await this.redisClient.del(key)
return true
}
return false
}
}
export const redisPreGeneratedSecretTokens = await RedisPreGeneratedSecretTokens.createAndConnect({
2025-06-09 10:00:55 +00:00
expirationTime: 60 * 5, // 5 minutes in seconds
2025-05-19 10:23:36 +00:00
})