feat: Use bun's redis client

This commit is contained in:
Wanjohi
2025-10-25 22:19:33 +03:00
parent c5d0242d50
commit 01eb63b36f
2 changed files with 13 additions and 21 deletions

View File

@@ -1,7 +1,6 @@
import { z } from "zod/v4";
import { RedisClient } from "bun";
import { subjects } from "../subjects.js";
import {redis} from 'bun';
import { issuer } from "@openauthjs/openauth";
import { RedisStorage } from "./src/storage.js";
import { Team } from "@nestri/core/team/index";
@@ -9,11 +8,12 @@ import { CodeProvider } from "@openauthjs/openauth/provider/code";
//!TODO: Add an auth storage adapter, maybe redis or postgres
const storage = RedisStorage({
redis: RedisClient.create(),
client: new RedisClient(process.env.REDIS_URL),
});
const app = issuer({
subjects,
storage,
providers: {
email: CodeProvider({
async request(_req, state, form, error) {

View File

@@ -19,11 +19,8 @@
*
* @packageDocumentation
*/
import {
joinKey,
StorageAdapter,
} from "@openauthjs/openauth/storage/storage";
import { Cluster, Redis } from "@nestri/core/redis/client";
import { redis as RedisClient } from "bun";
import { joinKey, StorageAdapter } from "@openauthjs/openauth/storage/storage";
const separator = String.fromCharCode(0x1f);
@@ -31,16 +28,16 @@ const separator = String.fromCharCode(0x1f);
* Configure the Redis connection that's created.
*/
export interface RedisStorageOptions {
redis: Cluster | Redis;
keyPrefix?: string;
defaultTTL?: number;
client: typeof RedisClient;
}
/**
* Creates a Cloudflare KV store.
* @param options - The config for the adapter.
*/
export function RedisStorage(options: RedisStorageOptions): StorageAdapter {
const { redis } = options;
const { client } = options;
const prefix = "storage";
function createKey(key: string[]): string {
@@ -54,7 +51,7 @@ export function RedisStorage(options: RedisStorageOptions): StorageAdapter {
return {
async get(key: string[]) {
const redisKey = createKey(key);
const value = await redis.get(redisKey);
const value = await client.get(redisKey);
if (!value) return;
@@ -70,21 +67,16 @@ export function RedisStorage(options: RedisStorageOptions): StorageAdapter {
if (ttlSeconds) {
// SET key value EX ttlSeconds
await options.redis.set(
redisKey,
JSON.stringify(value),
"EX",
ttlSeconds,
);
await client.set(redisKey, JSON.stringify(value), "EX", ttlSeconds);
} else {
// No expiry → simple SET
await options.redis.set(redisKey, JSON.stringify(value));
await client.set(redisKey, JSON.stringify(value));
}
},
async remove(key: string[]) {
const redisKey = createKey(key);
await options.redis.del(redisKey);
await client.del(redisKey);
},
async *scan(prefix: string[]) {
@@ -95,7 +87,7 @@ export function RedisStorage(options: RedisStorageOptions): StorageAdapter {
do {
// SCAN cursor MATCH pattern COUNT 100
const [nextCursor, keys] = await redis.scan(
const [nextCursor, keys] = await client.scan(
cursor,
"MATCH",
scanPattern,
@@ -107,7 +99,7 @@ export function RedisStorage(options: RedisStorageOptions): StorageAdapter {
if (keys.length === 0) {
continue;
}
const values = await redis.mget(...keys);
const values = await client.mget(...keys);
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
if (!key) {