@hammadj/better-auth-redis-storage 1.5.0-beta.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +12 -0
- package/LICENSE.md +20 -0
- package/dist/index.d.mts +38 -0
- package/dist/index.mjs +51 -0
- package/package.json +39 -0
- package/src/index.ts +1 -0
- package/src/redis-storage.ts +75 -0
- package/tsconfig.json +10 -0
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
|
|
2
|
+
> @hammadj/better-auth-redis-storage@1.5.0-beta.9 build /Users/hammadjutt/Dev/better-auth-fork/packages/redis-storage
|
|
3
|
+
> tsdown
|
|
4
|
+
|
|
5
|
+
[34mℹ[39m tsdown [2mv0.20.1[22m powered by rolldown [2mv1.0.0-rc.1[22m
|
|
6
|
+
[34mℹ[39m entry: [34msrc/index.ts[39m
|
|
7
|
+
[34mℹ[39m tsconfig: [34mtsconfig.json[39m
|
|
8
|
+
[34mℹ[39m Build start
|
|
9
|
+
[34mℹ[39m [2mdist/[22m[1mindex.mjs[22m [2m1.30 kB[22m [2m│ gzip: 0.57 kB[22m
|
|
10
|
+
[34mℹ[39m [2mdist/[22m[32m[1mindex.d.mts[22m[39m [2m0.95 kB[22m [2m│ gzip: 0.46 kB[22m
|
|
11
|
+
[34mℹ[39m 2 files, total: 2.25 kB
|
|
12
|
+
[32m✔[39m Build complete in [32m2749ms[39m
|
package/LICENSE.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
Copyright (c) 2024 - present, Bereket Engida
|
|
3
|
+
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
5
|
+
this software and associated documentation files (the “Software”), to deal in
|
|
6
|
+
the Software without restriction, including without limitation the rights to
|
|
7
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
8
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
9
|
+
subject to the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
16
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
17
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
18
|
+
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
19
|
+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
20
|
+
DEALINGS IN THE SOFTWARE.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import Redis from "ioredis";
|
|
2
|
+
|
|
3
|
+
//#region src/redis-storage.d.ts
|
|
4
|
+
interface RedisStorageConfig {
|
|
5
|
+
/**
|
|
6
|
+
* Redis client instance from ioredis
|
|
7
|
+
*/
|
|
8
|
+
client: Redis;
|
|
9
|
+
/**
|
|
10
|
+
* Optional key prefix for all keys stored in Redis
|
|
11
|
+
* @default "better-auth:"
|
|
12
|
+
*/
|
|
13
|
+
keyPrefix?: string | undefined;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Creates a Redis secondary storage for Better Auth using ioredis.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* import { Redis } from "ioredis";
|
|
21
|
+
* import { redisStorage } from "@better-auth/redis-storage";
|
|
22
|
+
*
|
|
23
|
+
* const redis = new Redis({
|
|
24
|
+
* host: "localhost",
|
|
25
|
+
* port: 6379,
|
|
26
|
+
* });
|
|
27
|
+
*
|
|
28
|
+
* const auth = betterAuth({
|
|
29
|
+
* secondaryStorage: redisStorage({ client: redis }),
|
|
30
|
+
* });
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* @param config - Configuration object containing the Redis client and optional key prefix
|
|
34
|
+
* @returns SecondaryStorage implementation for Better Auth
|
|
35
|
+
*/
|
|
36
|
+
declare function redisStorage(config: RedisStorageConfig): any;
|
|
37
|
+
//#endregion
|
|
38
|
+
export { type RedisStorageConfig, redisStorage };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
//#region src/redis-storage.ts
|
|
2
|
+
/**
|
|
3
|
+
* Creates a Redis secondary storage for Better Auth using ioredis.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* import { Redis } from "ioredis";
|
|
8
|
+
* import { redisStorage } from "@better-auth/redis-storage";
|
|
9
|
+
*
|
|
10
|
+
* const redis = new Redis({
|
|
11
|
+
* host: "localhost",
|
|
12
|
+
* port: 6379,
|
|
13
|
+
* });
|
|
14
|
+
*
|
|
15
|
+
* const auth = betterAuth({
|
|
16
|
+
* secondaryStorage: redisStorage({ client: redis }),
|
|
17
|
+
* });
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* @param config - Configuration object containing the Redis client and optional key prefix
|
|
21
|
+
* @returns SecondaryStorage implementation for Better Auth
|
|
22
|
+
*/
|
|
23
|
+
function redisStorage(config) {
|
|
24
|
+
const { client, keyPrefix = "better-auth:" } = config;
|
|
25
|
+
const prefixKey = (key) => {
|
|
26
|
+
return `${keyPrefix}${key}`;
|
|
27
|
+
};
|
|
28
|
+
return {
|
|
29
|
+
async get(key) {
|
|
30
|
+
return client.get(prefixKey(key));
|
|
31
|
+
},
|
|
32
|
+
async set(key, value, ttl) {
|
|
33
|
+
const prefixedKey = prefixKey(key);
|
|
34
|
+
if (ttl !== void 0 && ttl > 0) await client.setex(prefixedKey, ttl, value);
|
|
35
|
+
else await client.set(prefixedKey, value);
|
|
36
|
+
},
|
|
37
|
+
async delete(key) {
|
|
38
|
+
await client.del(prefixKey(key));
|
|
39
|
+
},
|
|
40
|
+
async listKeys() {
|
|
41
|
+
return (await client.keys(`${keyPrefix}*`)).map((key) => key.replace(keyPrefix, ""));
|
|
42
|
+
},
|
|
43
|
+
async clear() {
|
|
44
|
+
const keys = await client.keys(`${keyPrefix}*`);
|
|
45
|
+
await client.del(...keys);
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
//#endregion
|
|
51
|
+
export { redisStorage };
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hammadj/better-auth-redis-storage",
|
|
3
|
+
"version": "1.5.0-beta.9",
|
|
4
|
+
"description": "Redis storage for Better Auth secondary storage",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/META-DREAMER/better-auth.git",
|
|
9
|
+
"directory": "packages/redis-storage"
|
|
10
|
+
},
|
|
11
|
+
"main": "./dist/index.mjs",
|
|
12
|
+
"module": "./dist/index.mjs",
|
|
13
|
+
"types": "./dist/index.d.mts",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"dev-source": "./src/index.ts",
|
|
17
|
+
"types": "./dist/index.d.mts",
|
|
18
|
+
"default": "./dist/index.mjs"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"peerDependencies": {
|
|
22
|
+
"ioredis": "^5.0.0",
|
|
23
|
+
"@hammadj/better-auth-core": "1.5.0-beta.9"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@types/node": "^25.0.10",
|
|
27
|
+
"ioredis": "^5.9.2",
|
|
28
|
+
"tsdown": "^0.20.1",
|
|
29
|
+
"typescript": "^5.9.3",
|
|
30
|
+
"vitest": "^4.0.18",
|
|
31
|
+
"@hammadj/better-auth-core": "1.5.0-beta.9"
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"build": "tsdown",
|
|
35
|
+
"dev": "tsdown --watch",
|
|
36
|
+
"test": "vitest",
|
|
37
|
+
"typecheck": "tsc --noEmit"
|
|
38
|
+
}
|
|
39
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { type RedisStorageConfig, redisStorage } from "./redis-storage";
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import type { SecondaryStorage } from "@better-auth/core/db";
|
|
2
|
+
import type Redis from "ioredis";
|
|
3
|
+
|
|
4
|
+
export interface RedisStorageConfig {
|
|
5
|
+
/**
|
|
6
|
+
* Redis client instance from ioredis
|
|
7
|
+
*/
|
|
8
|
+
client: Redis;
|
|
9
|
+
/**
|
|
10
|
+
* Optional key prefix for all keys stored in Redis
|
|
11
|
+
* @default "better-auth:"
|
|
12
|
+
*/
|
|
13
|
+
keyPrefix?: string | undefined;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Creates a Redis secondary storage for Better Auth using ioredis.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* import { Redis } from "ioredis";
|
|
22
|
+
* import { redisStorage } from "@better-auth/redis-storage";
|
|
23
|
+
*
|
|
24
|
+
* const redis = new Redis({
|
|
25
|
+
* host: "localhost",
|
|
26
|
+
* port: 6379,
|
|
27
|
+
* });
|
|
28
|
+
*
|
|
29
|
+
* const auth = betterAuth({
|
|
30
|
+
* secondaryStorage: redisStorage({ client: redis }),
|
|
31
|
+
* });
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* @param config - Configuration object containing the Redis client and optional key prefix
|
|
35
|
+
* @returns SecondaryStorage implementation for Better Auth
|
|
36
|
+
*/
|
|
37
|
+
export function redisStorage(config: RedisStorageConfig) {
|
|
38
|
+
const { client, keyPrefix = "better-auth:" } = config;
|
|
39
|
+
|
|
40
|
+
const prefixKey = (key: string): string => {
|
|
41
|
+
return `${keyPrefix}${key}`;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
return {
|
|
45
|
+
async get(key: string) {
|
|
46
|
+
return client.get(prefixKey(key));
|
|
47
|
+
},
|
|
48
|
+
|
|
49
|
+
async set(key: string, value: string, ttl?: number | undefined) {
|
|
50
|
+
const prefixedKey = prefixKey(key);
|
|
51
|
+
if (ttl !== undefined && ttl > 0) {
|
|
52
|
+
await client.setex(prefixedKey, ttl, value);
|
|
53
|
+
} else {
|
|
54
|
+
await client.set(prefixedKey, value);
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
|
|
58
|
+
async delete(key: string) {
|
|
59
|
+
await client.del(prefixKey(key));
|
|
60
|
+
},
|
|
61
|
+
|
|
62
|
+
async listKeys(): Promise<string[]> {
|
|
63
|
+
const keys = await client.keys(`${keyPrefix}*`);
|
|
64
|
+
return keys.map((key) => key.replace(keyPrefix, ""));
|
|
65
|
+
},
|
|
66
|
+
|
|
67
|
+
async clear(): Promise<void> {
|
|
68
|
+
const keys = await client.keys(`${keyPrefix}*`);
|
|
69
|
+
await client.del(...keys);
|
|
70
|
+
},
|
|
71
|
+
} satisfies SecondaryStorage & {
|
|
72
|
+
listKeys: () => Promise<string[]>;
|
|
73
|
+
clear: () => Promise<void>;
|
|
74
|
+
};
|
|
75
|
+
}
|