peta-auth 0.1.2 → 0.1.3
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/README.md +14 -4
- package/dist/elysia.d.mts +2 -30
- package/dist/elysia.mjs +2 -2
- package/dist/hono.d.mts +1 -0
- package/dist/hono.mjs +2 -2
- package/dist/nuxt.d.mts +1 -0
- package/dist/nuxt.mjs +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -157,11 +157,21 @@ Without a generic parameter, session data defaults to `Record<string, unknown>`.
|
|
|
157
157
|
|
|
158
158
|
### `requireSession()` guard
|
|
159
159
|
|
|
160
|
-
Returns 401 if the session has no user data.
|
|
160
|
+
Returns 401 if the session has no user data. Optionally checks a specific session key:
|
|
161
161
|
|
|
162
|
-
|
|
162
|
+
```ts
|
|
163
|
+
// Guard on any session data
|
|
164
|
+
app.use('/api/*', requireSession())
|
|
165
|
+
|
|
166
|
+
// Guard on a specific key (e.g. session.userId must be truthy)
|
|
167
|
+
app.use('/admin/*', requireSession('userId'))
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
Works per-framework:
|
|
171
|
+
|
|
172
|
+
- **Hono**: `app.use('/protected/*', requireSession())` — path-patterned middleware
|
|
163
173
|
- **Elysia**: `app.use(requireSession())` — guards all routes defined after it
|
|
164
|
-
- **Nuxt**: `requireSession(event, session)` — throws `createError({ statusCode: 401 })`
|
|
174
|
+
- **Nuxt**: `requireSession(event, session)` or `requireSession(event, session, 'userId')` — throws `createError({ statusCode: 401 })`
|
|
165
175
|
|
|
166
176
|
### Session object
|
|
167
177
|
|
|
@@ -354,7 +364,7 @@ Session data is serialized, encrypted with AES-256-CBC, integrity-protected with
|
|
|
354
364
|
## Scripts
|
|
355
365
|
|
|
356
366
|
```bash
|
|
357
|
-
bun test #
|
|
367
|
+
bun test # 74 tests across 12 files
|
|
358
368
|
bun run build # tsdown → dist/ (21 files, 30 kB)
|
|
359
369
|
bun run prepublish # build + publish
|
|
360
370
|
```
|
package/dist/elysia.d.mts
CHANGED
|
@@ -34,35 +34,7 @@ declare function session<T extends Record<string, unknown> = Record<string, unkn
|
|
|
34
34
|
standaloneSchema: {};
|
|
35
35
|
response: {};
|
|
36
36
|
}>;
|
|
37
|
-
declare function requireSession(): (app: Elysia) => Elysia
|
|
38
|
-
|
|
39
|
-
store: {};
|
|
40
|
-
derive: {};
|
|
41
|
-
resolve: {};
|
|
42
|
-
}, {
|
|
43
|
-
typebox: {};
|
|
44
|
-
error: {};
|
|
45
|
-
}, {
|
|
46
|
-
schema: {};
|
|
47
|
-
standaloneSchema: {};
|
|
48
|
-
macro: {};
|
|
49
|
-
macroFn: {};
|
|
50
|
-
parser: {};
|
|
51
|
-
response: {};
|
|
52
|
-
}, {}, {
|
|
53
|
-
derive: {};
|
|
54
|
-
resolve: {};
|
|
55
|
-
schema: {};
|
|
56
|
-
standaloneSchema: {};
|
|
57
|
-
response: {};
|
|
58
|
-
}, {
|
|
59
|
-
derive: {};
|
|
60
|
-
resolve: {};
|
|
61
|
-
schema: {};
|
|
62
|
-
standaloneSchema: {};
|
|
63
|
-
response: {
|
|
64
|
-
200: Response;
|
|
65
|
-
};
|
|
66
|
-
}>;
|
|
37
|
+
declare function requireSession(): (app: Elysia) => Elysia;
|
|
38
|
+
declare function requireSession<K extends string>(key: K): (app: Elysia) => Elysia;
|
|
67
39
|
//#endregion
|
|
68
40
|
export { requireSession, session };
|
package/dist/elysia.mjs
CHANGED
|
@@ -13,10 +13,10 @@ function session(options) {
|
|
|
13
13
|
}, options) };
|
|
14
14
|
});
|
|
15
15
|
}
|
|
16
|
-
function requireSession() {
|
|
16
|
+
function requireSession(key) {
|
|
17
17
|
return (app) => app.onBeforeHandle((context) => {
|
|
18
18
|
const session = context.session;
|
|
19
|
-
if (!Object.keys(session).some((k) => k !== "save" && k !== "destroy" && k !== "updateConfig")) return new Response(JSON.stringify({ error: "unauthorized" }), {
|
|
19
|
+
if (!(key ? !!session[key] : Object.keys(session).some((k) => k !== "save" && k !== "destroy" && k !== "updateConfig"))) return new Response(JSON.stringify({ error: "unauthorized" }), {
|
|
20
20
|
status: 401,
|
|
21
21
|
headers: { "Content-Type": "application/json" }
|
|
22
22
|
});
|
package/dist/hono.d.mts
CHANGED
|
@@ -8,5 +8,6 @@ declare function session<T extends Record<string, unknown> = Record<string, unkn
|
|
|
8
8
|
};
|
|
9
9
|
}>;
|
|
10
10
|
declare function requireSession(): MiddlewareHandler;
|
|
11
|
+
declare function requireSession<K extends string>(key: K): MiddlewareHandler;
|
|
11
12
|
//#endregion
|
|
12
13
|
export { requireSession, session };
|
package/dist/hono.mjs
CHANGED
|
@@ -11,10 +11,10 @@ function session(options) {
|
|
|
11
11
|
await next();
|
|
12
12
|
});
|
|
13
13
|
}
|
|
14
|
-
function requireSession() {
|
|
14
|
+
function requireSession(key) {
|
|
15
15
|
return createMiddleware(async (c, next) => {
|
|
16
16
|
const s = c.var.session;
|
|
17
|
-
if (!Object.keys(s).some((k) => k !== "save" && k !== "destroy" && k !== "updateConfig")) return c.json({ error: "unauthorized" }, 401);
|
|
17
|
+
if (!(key ? !!s[key] : Object.keys(s).some((k) => k !== "save" && k !== "destroy" && k !== "updateConfig"))) return c.json({ error: "unauthorized" }, 401);
|
|
18
18
|
await next();
|
|
19
19
|
});
|
|
20
20
|
}
|
package/dist/nuxt.d.mts
CHANGED
|
@@ -4,5 +4,6 @@ import { H3Event } from "h3";
|
|
|
4
4
|
//#region src/nuxt.d.ts
|
|
5
5
|
declare function useSession<T extends Record<string, unknown> = Record<string, unknown>>(event: H3Event, options: SessionOptions): Promise<T & IronSession>;
|
|
6
6
|
declare function requireSession(_event: H3Event, session: IronSession): void;
|
|
7
|
+
declare function requireSession<K extends string>(_event: H3Event, session: IronSession, key: K): void;
|
|
7
8
|
//#endregion
|
|
8
9
|
export { requireSession, useSession };
|
package/dist/nuxt.mjs
CHANGED
|
@@ -14,8 +14,8 @@ function useSession(event, options) {
|
|
|
14
14
|
cookieOptions: options?.cookieOptions
|
|
15
15
|
});
|
|
16
16
|
}
|
|
17
|
-
function requireSession(_event, session) {
|
|
18
|
-
if (!Object.keys(session).some((k) => k !== "save" && k !== "destroy" && k !== "updateConfig")) throw createError({
|
|
17
|
+
function requireSession(_event, session, key) {
|
|
18
|
+
if (!(key ? !!session[key] : Object.keys(session).some((k) => k !== "save" && k !== "destroy" && k !== "updateConfig"))) throw createError({
|
|
19
19
|
statusCode: 401,
|
|
20
20
|
statusMessage: "unauthorized"
|
|
21
21
|
});
|