@lithia-js/better-auth 1.0.0-canary.14 → 1.0.0-canary.16

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 CHANGED
@@ -3,58 +3,75 @@
3
3
  <img alt="Lithia logo" src="https://raw.githubusercontent.com/lithia-framework/lithia/canary/.github/assets/logo.svg" height="128">
4
4
  </a>
5
5
  <h1>Lithia</h1>
6
- <p><strong>The Node.js framework that makes API development feel like magic</strong></p>
6
+ <p><strong>The high-performance Node.js framework powered by Rust.</strong></p>
7
7
 
8
- <a href="https://www.npmjs.com/package/lithia"><img alt="NPM version" src="https://img.shields.io/npm/v/lithia.svg?style=for-the-badge&labelColor=000000"></a>
9
- <a href="https://github.com/lithia-framework/lithia/blob/main/LICENSE"><img alt="License" src="https://img.shields.io/npm/l/lithia.svg?style=for-the-badge&labelColor=000000"></a>
8
+ <p>Build APIs with magic, speed, and Type Safety by default.</p>
9
+
10
+ <a href="https://www.npmjs.com/package/@lithia-js/core"><img alt="NPM version" src="https://img.shields.io/npm/v/@lithia-js/core.svg?style=for-the-badge&labelColor=000000"></a>
11
+ <a href="https://github.com/lithia-framework/lithia/blob/main/LICENSE"><img alt="License" src="https://img.shields.io/npm/l/@lithia-js/core.svg?style=for-the-badge&labelColor=000000"></a>
10
12
  <a href="https://opencollective.com/lithiajs"><img alt="Support Lithia" src="https://img.shields.io/badge/Support%20Lithia-blueviolet.svg?style=for-the-badge&logo=OpenCollective&labelColor=000000&logoWidth=20"></a>
11
13
 
12
14
  </div>
13
15
 
14
- ## Getting Started
16
+ ## 🚀 Quick Start
15
17
 
16
- Lithia is a next-generation Node.js framework that enables you to build powerful APIs with file-based routing, built-in WebSocket support, and a beautiful development interface.
18
+ Build your first Lithia app in seconds:
17
19
 
18
- - Visit our [Learn Lithia](https://lithiajs.com/docs) guide to get started.
19
- - Check out the [Examples](https://github.com/lithia-framework/lithia/tree/main/examples) to see Lithia in action.
20
+ ```bash
21
+ npx create-lithia@latest my-app
22
+ cd my-app
23
+ npm run dev
24
+ ```
20
25
 
21
- ## Documentation
26
+ ## 🪄 Magic in Action
22
27
 
23
- Visit [https://lithiajs.com/docs](https://lithiajs.com/docs) to view the full documentation.
28
+ Lithia uses a clean, file-based routing convention. No boilerplate, just focus on your logic.
24
29
 
25
- ## Community
30
+ ```typescript
31
+ // routes/hello/route.get.ts
32
+ import type { RouteHandler } from "@lithia-js/core";
26
33
 
27
- The Lithia community can be found on [GitHub Discussions](https://github.com/lithia-framework/lithia/discussions) where you can ask questions, voice ideas, and share your projects with other people.
34
+ const Hello: RouteHandler = async (req, res) => {
35
+ const name = req.query.name || "World";
36
+ return res.json({ message: `Hello, ${name}!` });
37
+ }
28
38
 
29
- ## Contributing
39
+ export default Hello;
40
+ // Available at: GET /hello
41
+ ```
30
42
 
31
- Contributions to Lithia are welcome and highly appreciated. However, before you jump right into it, we would like you to review our [Contribution Guidelines](CONTRIBUTING.md) to make sure you have a smooth experience contributing to Lithia.
43
+ ## Features
32
44
 
33
- ---
45
+ * **Intuitive Routing**: Method-based file naming (`.get.ts`, `.post.ts`) for automatic route registration.
46
+ * **Native Performance**: Core engine written in Rust for lightning-fast request handling.
47
+ * **Full-stack Ready**: Native support for WebSockets, Auth, and Drizzle ORM.
48
+ * **Modern Stack**: Ships with Biome and TypeScript pre-configured for the best DX.
49
+
50
+ ## 📖 Documentation
34
51
 
35
- ## Support the Project
52
+ Everything you need to know is at [lithiajs.com/docs](https://lithiajs.com/docs).
36
53
 
37
- If Lithia makes your life easier, consider supporting it:
54
+ ---
55
+
56
+ ## 🤝 Community & Support
38
57
 
39
- - **Star** this repository
40
- - **Share** on social media
41
- - **Sponsor** via [OpenCollective](https://opencollective.com/lithiajs)
42
- - **Report bugs** and suggest improvements
58
+ * **Discussions**: [GitHub Discussions](https://github.com/lithia-framework/lithia/discussions)
59
+ * **Contribution**: We love PRs! See [CONTRIBUTING.md](https://www.google.com/search?q=CONTRIBUTING.md)
60
+ * **Sponsor**: Support the magic on [OpenCollective](https://opencollective.com/lithiajs)
43
61
 
44
62
  ---
45
63
 
46
64
  ## License
47
65
 
48
- Lithia is [MIT licensed](LICENSE). Free for personal and commercial use.
66
+ Lithia is [MIT licensed](https://www.google.com/search?q=LICENSE). Built with ❤️ by Lucas Arch and the community.
49
67
 
50
68
  ---
51
69
 
52
70
  <div align="center">
53
- <p><strong>Built with ❤️ by the Lithia community</strong></p>
54
- <p>
55
- <a href="https://github.com/lithia-framework/lithia">GitHub</a> •
56
- <a href="https://lithiajs.com">Documentation</a> •
57
- <a href="https://opencollective.com/lithiajs">OpenCollective</a>
58
- <a href="https://github.com/lithia-framework/lithia/discussions">Discussions</a>
59
- </p>
60
- </div>
71
+ <p>
72
+ <a href="https://github.com/lithia-framework/lithia">GitHub</a> •
73
+ <a href="https://lithiajs.com">Documentation</a> •
74
+ <a href="https://opencollective.com/lithiajs">OpenCollective</a> •
75
+ <a href="https://github.com/lithia-framework/lithia/discussions">Discussions</a>
76
+ </p>
77
+ </div>
@@ -0,0 +1,14 @@
1
+ /**
2
+ * @fileoverview Better-Auth Handler Bridge for Lithia.js.
3
+ * This handler proxies incoming HTTP requests to the Better-Auth engine,
4
+ * handling the translation between Lithia/Node.js types and Web Standard Fetch types.
5
+ */
6
+ import { type RouteHandler } from "@lithia-js/core";
7
+ import type { Auth } from "better-auth/types";
8
+ /**
9
+ * Creates a Lithia RouteHandler to serve Better-Auth API routes.
10
+ * It translates headers, body, and methods to a Web Standard Request.
11
+ * * @param auth The Better-Auth instance.
12
+ * @returns A RouteHandler compatible with Lithia's routing system.
13
+ */
14
+ export declare function BetterAuth(auth: Auth): RouteHandler;
@@ -0,0 +1,63 @@
1
+ /**
2
+ * @fileoverview Better-Auth Handler Bridge for Lithia.js.
3
+ * This handler proxies incoming HTTP requests to the Better-Auth engine,
4
+ * handling the translation between Lithia/Node.js types and Web Standard Fetch types.
5
+ */
6
+ import { RouteNotFoundError } from "@lithia-js/core";
7
+ /**
8
+ * Creates a Lithia RouteHandler to serve Better-Auth API routes.
9
+ * It translates headers, body, and methods to a Web Standard Request.
10
+ * * @param auth The Better-Auth instance.
11
+ * @returns A RouteHandler compatible with Lithia's routing system.
12
+ */
13
+ export function BetterAuth(auth) {
14
+ return async (req, res) => {
15
+ // 1. Better-Auth typically only handles GET and POST
16
+ if (!["POST", "GET"].includes(req.method)) {
17
+ res.status(405).send("Method Not Allowed");
18
+ return;
19
+ }
20
+ const rawBody = await req.body();
21
+ const hasBody = rawBody && Object.keys(rawBody).length > 0;
22
+ // 2. Map Node.js headers to Web Standard Headers
23
+ const headers = new Headers();
24
+ Object.entries(req.headers).forEach(([key, value]) => {
25
+ if (value) {
26
+ if (Array.isArray(value)) {
27
+ value.forEach((v) => {
28
+ headers.append(key, v);
29
+ });
30
+ }
31
+ else {
32
+ headers.append(key, String(value));
33
+ }
34
+ }
35
+ });
36
+ // 3. Construct the Web Standard Request for Better-Auth
37
+ const authReq = new Request(`http://internal${req.pathname}`, {
38
+ method: req.method,
39
+ headers,
40
+ body: hasBody ? JSON.stringify(rawBody) : undefined,
41
+ });
42
+ // 4. Execute Better-Auth logic
43
+ const authRes = await auth.handler(authReq);
44
+ // 5. Proxy back the response headers
45
+ // We skip hop-by-hop headers that Node.js/Lithia manages automatically
46
+ authRes.headers.forEach((value, key) => {
47
+ const lowerKey = key.toLowerCase();
48
+ if (lowerKey !== "transfer-encoding" && lowerKey !== "content-length") {
49
+ res.setHeader(key, value);
50
+ }
51
+ });
52
+ // 6. Handle specific status cases
53
+ if (authRes.status === 404) {
54
+ throw new RouteNotFoundError(`Auth route '${req.pathname}' not found.`);
55
+ }
56
+ // 7. Stream the body back to the client
57
+ const responseBody = authRes.body
58
+ ? await new Response(authRes.body).text()
59
+ : undefined;
60
+ res.status(authRes.status).send(responseBody);
61
+ };
62
+ }
63
+ //# sourceMappingURL=handle-auth.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"handle-auth.mjs","sourceRoot":"","sources":["../src/hand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,QAAQ,EAAE,EAAE;YAC7D,MAAM,EAAE,GAAG,CAAC,MAAM;YAClB,OAAO;YACP,IAAI,EAAE,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,SAAS;SACnD,CAAC,CAAC;QAEH,+BAA+B;QAC/B,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;QAE5C,qCAAqC;QACrC,uEAAuE;QACvE,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,GAAG,EAAE,EAAE;YACtC,MAAM,QAAQ,GAAG,GAAG,CAAC,WAAW,EAAE,CAAC;YACnC,IAAI,QAAQ,KAAK,mBAAmB,IAAI,QAAQ,KAAK,gBAAgB,EAAE,CAAC;gBACvE,GAAG,CAAC,SAAS,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;YAC3B,CAAC;QACF,CAAC,CAAC,CAAC;QAEH,kCAAkC;QAClC,IAAI,OAAO,CAAC,MAAM,KAAK,GAAG,EAAE,CAAC;YAC5B,MAAM,IAAI,kBAAkB,CAAC,eAAe,GAAG,CAAC,QAAQ,cAAc,CAAC,CAAC;QACzE,CAAC;QAED,wCAAwC;QACxC,MAAM,YAAY,GAAG,OAAO,CAAC,IAAI;YAChC,CAAC,CAAC,MAAM,IAAI,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE;YACzC,CAAC,CAAC,SAAS,CAAC;QAEb,GAAG,CAAC,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;IAC/C,CAAC,CAAC;AACH,CAAC"}
@@ -0,0 +1,48 @@
1
+ /**
2
+ * @fileoverview Better-Auth Integration Middleware for Lithia.js.
3
+ * Provides session validation and context-based session access via hooks.
4
+ */
5
+ import { type RouteMiddleware } from "@lithia-js/core";
6
+ import type { Auth } from "better-auth/types";
7
+ /**
8
+ * Options to configure the behavior of the authentication middleware.
9
+ */
10
+ interface AuthMiddlewareOptions {
11
+ error: {
12
+ /** Whether to throw an UnauthorizedError if the session is missing. */
13
+ throw: boolean;
14
+ /** Custom error message for the unauthorized response. */
15
+ message: string;
16
+ };
17
+ }
18
+ /**
19
+ * The structure of the authentication data stored in AsyncLocalStorage.
20
+ */
21
+ export type AuthContext<T extends Auth = Auth> = {
22
+ session: Awaited<ReturnType<T["api"]["getSession"]>>;
23
+ };
24
+ /**
25
+ * Middleware to authenticate requests using Better-Auth.
26
+ * It injects the session into the execution context for subsequent handlers.
27
+ * * @param auth The Better-Auth instance.
28
+ * @param options Configuration for error handling.
29
+ * @returns A standard Lithia RouteMiddleware.
30
+ */
31
+ export declare function authenticated(auth: Auth, options?: AuthMiddlewareOptions): RouteMiddleware;
32
+ /**
33
+ * Internal helper to access the raw authentication context.
34
+ * * @template T The Auth instance type.
35
+ * @throws {NotInAuthContext} If called outside an authenticated route.
36
+ */
37
+ export declare function getAuthContext<T extends Auth = Auth>(): AuthContext<T>;
38
+ /**
39
+ * Hook to retrieve the current user session.
40
+ * Must be used in a route protected by the 'authenticated' middleware.
41
+ * * @template T The Auth instance type.
42
+ * @returns The active user session data.
43
+ * @example
44
+ * const session = useSession();
45
+ * console.log(session.user.email);
46
+ */
47
+ export declare function useSession<T extends Auth = Auth>(): AuthContext<T>["session"];
48
+ export {};
@@ -0,0 +1,81 @@
1
+ /**
2
+ * @fileoverview Better-Auth Integration Middleware for Lithia.js.
3
+ * Provides session validation and context-based session access via hooks.
4
+ */
5
+ import { AsyncLocalStorage } from "node:async_hooks";
6
+ import { LithiaError, UnauthorizedError, } from "@lithia-js/core";
7
+ import { fromNodeHeaders } from "better-auth/node";
8
+ /**
9
+ * Global key for the Auth context to ensure singleton behavior.
10
+ */
11
+ const AUTH_CONTEXT_KEY = Symbol.for("lithia.auth_context.v1");
12
+ /**
13
+ * Retrieves the global AsyncLocalStorage instance for authentication.
14
+ */
15
+ function getGlobalAuthStore() {
16
+ const globalAny = globalThis;
17
+ if (!globalAny[AUTH_CONTEXT_KEY]) {
18
+ globalAny[AUTH_CONTEXT_KEY] = new AsyncLocalStorage();
19
+ }
20
+ return globalAny[AUTH_CONTEXT_KEY];
21
+ }
22
+ const authContextStore = getGlobalAuthStore();
23
+ /**
24
+ * Middleware to authenticate requests using Better-Auth.
25
+ * It injects the session into the execution context for subsequent handlers.
26
+ * * @param auth The Better-Auth instance.
27
+ * @param options Configuration for error handling.
28
+ * @returns A standard Lithia RouteMiddleware.
29
+ */
30
+ export function authenticated(auth, options = {
31
+ error: {
32
+ message: "No valid authentication session found",
33
+ throw: true,
34
+ },
35
+ }) {
36
+ return async (req, _, next) => {
37
+ // 1. Resolve session from incoming Node.js headers
38
+ const session = await auth.api.getSession({
39
+ headers: fromNodeHeaders(req.headers),
40
+ });
41
+ // 2. Handle missing sessions
42
+ if (!session && options.error.throw) {
43
+ throw new UnauthorizedError(options.error.message);
44
+ }
45
+ // 3. Run the rest of the request within the Auth context
46
+ await authContextStore.run({ session }, async () => {
47
+ await next();
48
+ });
49
+ };
50
+ }
51
+ class NotInAuthContext extends LithiaError {
52
+ constructor() {
53
+ super('Lithia Auth hooks must be used within an authenticated route. Did you forget to add the "authenticated" middleware?');
54
+ }
55
+ }
56
+ /**
57
+ * Internal helper to access the raw authentication context.
58
+ * * @template T The Auth instance type.
59
+ * @throws {NotInAuthContext} If called outside an authenticated route.
60
+ */
61
+ export function getAuthContext() {
62
+ const context = authContextStore.getStore();
63
+ if (!context) {
64
+ throw new NotInAuthContext();
65
+ }
66
+ return context;
67
+ }
68
+ /**
69
+ * Hook to retrieve the current user session.
70
+ * Must be used in a route protected by the 'authenticated' middleware.
71
+ * * @template T The Auth instance type.
72
+ * @returns The active user session data.
73
+ * @example
74
+ * const session = useSession();
75
+ * console.log(session.user.email);
76
+ */
77
+ export function useSession() {
78
+ const context = getAuthContext();
79
+ return context.session;
80
+ }
81
+ //# sourceMappingURL=use-auth.mjs.map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gBAAgB,CAAC,GAAG,CAAC,EAAE,OAAO,EAAE,EAAE,KAAK,IAAI,EAAE;YAClD,MAAM,IAAI,EAAE,CAAC;QACd,CAAC,CAAC,CAAC;IACJ,CAAC,CAAC;AACH,CAAC;AAED,MAAM,gBAAiB,SAAQ,WAAW;IACzC;QACC,KAAK,CACJ,qHAAqH,CACrH,CAAC;IACH,CAAC;CACD;AAED;;;;GAIG;AACH,MAAM,UAAU,cAAc;IAC7B,MAAM,OAAO,GAAG,gBAAgB,CAAC,QAAQ,EAAgC,CAAC;IAC1E,IAAI,CAAC,OAAO,EAAE,CAAC;QACd,MAAM,IAAI,gBAAgB,EAAE,CAAC;IAC9B,CAAC;IACD,OAAO,OAAO,CAAC;AAChB,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,UAAU;IACzB,MAAM,OAAO,GAAG,cAAc,EAAK,CAAC;IACpC,OAAO,OAAO,CAAC,OAAO,CAAC;AACxB,CAAC"}
package/dist/index.d.mts CHANGED
@@ -1,2 +1,2 @@
1
- export { handleAuth } from "./handler.mjs";
2
- export { authenticated, useSession } from "./useAuth.mjs";
1
+ export { BetterAuth } from "./handle-auth.mjs";
2
+ export { authenticated, useSession } from "./hooks/use-auth.mjs";
package/dist/index.mjs CHANGED
@@ -1,3 +1,3 @@
1
- export { handleAuth } from "./handler.mjs";
2
- export { authenticated, useSession } from "./useAuth.mjs";
1
+ export { BetterAuth } from "./handle-auth.mjs";
2
+ export { authenticated, useSession } from "./hooks/use-auth.mjs";
3
3
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.mts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC"}
1
+ {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.mts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAC/C,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC"}
package/package.json CHANGED
@@ -1,42 +1,59 @@
1
1
  {
2
2
  "name": "@lithia-js/better-auth",
3
- "version": "1.0.0-canary.14",
4
- "private": false,
5
- "type": "module",
3
+ "version": "1.0.0-canary.16",
4
+ "description": "The official Better Auth adapter for the Lithia.js framework.",
5
+ "keywords": [
6
+ "lithia",
7
+ "lithia-js",
8
+ "better-auth",
9
+ "authentication",
10
+ "auth-adapter",
11
+ "backend",
12
+ "typescript",
13
+ "native-speed"
14
+ ],
6
15
  "author": "Lucas Arch <luketsx@icloud.com>",
7
- "homepage": "https://lithiajs.com",
8
16
  "license": "MIT",
17
+ "homepage": "https://lithiajs.com",
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/lithia-framework/lithia.git",
21
+ "directory": "packages/better-auth"
22
+ },
23
+ "bugs": {
24
+ "url": "https://github.com/lithia-framework/lithia/issues"
25
+ },
26
+ "type": "module",
9
27
  "main": "dist/index.mjs",
10
- "types": "dist/index.d.mts",
11
28
  "module": "dist/index.mjs",
29
+ "types": "dist/index.d.mts",
12
30
  "exports": {
13
31
  ".": {
14
32
  "types": "./dist/index.d.mts",
33
+ "import": "./dist/index.mjs",
15
34
  "default": "./dist/index.mjs"
16
35
  }
17
36
  },
18
37
  "files": [
19
- "dist"
38
+ "dist",
39
+ "README.md",
40
+ "LICENSE"
20
41
  ],
21
- "dependencies": {
22
- "better-auth": "^1.4.17",
23
- "@lithia-js/core": "1.0.0-canary.14"
24
- },
25
42
  "engines": {
26
43
  "node": ">=20"
27
44
  },
45
+ "peerDependencies": {
46
+ "better-auth": "^1.4.17",
47
+ "@lithia-js/core": "1.0.0-canary.16"
48
+ },
49
+ "devDependencies": {
50
+ "@lithia-js/core": "1.0.0-canary.16"
51
+ },
28
52
  "publishConfig": {
29
53
  "registry": "https://registry.npmjs.org/",
30
54
  "access": "public",
31
55
  "provenance": true
32
56
  },
33
- "repository": {
34
- "type": "git",
35
- "url": "git+https://github.com/lithia-framework/lithia.git"
36
- },
37
- "bugs": {
38
- "url": "https://github.com/lithia-framework/lithia/issues"
39
- },
40
57
  "scripts": {
41
58
  "build": "tsc -p tsconfig.build.json"
42
59
  }
@@ -1,3 +0,0 @@
1
- import { type RouteHandler } from "@lithia-js/core/server";
2
- import type { Auth } from "better-auth/types";
3
- export declare function handleAuth(auth: Auth): RouteHandler;
package/dist/handler.mjs DELETED
@@ -1,37 +0,0 @@
1
- import { RouteNotFoundError } from "@lithia-js/core/server";
2
- export function handleAuth(auth) {
3
- return async (req, res) => {
4
- if (!["POST", "GET"].includes(req.method)) {
5
- res.status(405).send("Method Not Allowed");
6
- return;
7
- }
8
- const body = await req.body();
9
- const headers = new Headers();
10
- Object.entries(req.headers).forEach(([key, value]) => {
11
- if (value)
12
- headers.append(key, value.toString());
13
- });
14
- if (body && Object.keys(body).length > 0) {
15
- headers.set("Content-Type", "application/json");
16
- }
17
- const authReq = new Request(`http://internal${req.pathname}`, {
18
- method: req.method,
19
- headers,
20
- body: body && Object.keys(body).length > 0 ? JSON.stringify(body) : undefined,
21
- });
22
- const authRes = await auth.handler(authReq);
23
- authRes.headers.forEach((value, key) => {
24
- if (!["transfer-encoding", "content-length"].includes(key.toLowerCase())) {
25
- res.setHeader(key, value);
26
- }
27
- });
28
- if (authRes.status === 404) {
29
- throw new RouteNotFoundError(req.pathname);
30
- }
31
- const text = authRes.body
32
- ? await new Response(authRes.body).text()
33
- : undefined;
34
- res.status(authRes.status).send(text);
35
- };
36
- }
37
- //# sourceMappingURL=handler.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"handler.mjs","sourceRoot":"","sources":["../src/handler.mts"],"names":[],"mappings":"AAAA,OAAO,EAAqB,kBAAkB,EAAE,MAAM,wBAAwB,CAAC;AAG/E,MAAM,UAAU,UAAU,CAAC,IAAU;IACpC,OAAO,KAAK,EAAE,GAA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IACC,CAAC,CAAC,mBAAmB,EAAE,gBAAgB,CAAC,CAAC,QAAQ,CAAC,GAAG,CAAC,WAAW,EAAE,CAAC,EACnE,CAAC;gBACF,GAAG,CAAC,SAAS,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;YAC3B,CAAC;QACF,CAAC,CAAC,CAAC;QAEH,IAAI,OAAO,CAAC,MAAM,KAAK,GAAG,EAAE,CAAC;YAC5B,MAAM,IAAI,kBAAkB,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;QAC5C,CAAC;QAED,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI;YACxB,CAAC,CAAC,MAAM,IAAI,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE;YACzC,CAAC,CAAC,SAAS,CAAC;QAEb,GAAG,CAAC,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACvC,CAAC,CAAC;AACH,CAAC"}
@@ -1,15 +0,0 @@
1
- import { type Middleware } from "@lithia-js/core/server";
2
- import type { Auth } from "better-auth/types";
3
- interface AuthMiddlewareOptions {
4
- error: {
5
- throw: boolean;
6
- message: string;
7
- };
8
- }
9
- export type AuthContext<T extends Auth = Auth> = {
10
- session: Awaited<ReturnType<T["api"]["getSession"]>>;
11
- };
12
- export declare function authenticated(auth: Auth, options?: AuthMiddlewareOptions): Middleware;
13
- export declare function getAuthContext<T extends Auth = Auth>(): AuthContext<T>;
14
- export declare function useSession<T extends Auth = Auth>(): AuthContext<T>["session"];
15
- export {};
package/dist/useAuth.mjs DELETED
@@ -1,35 +0,0 @@
1
- import { AsyncLocalStorage } from "node:async_hooks";
2
- import { RequestError } from "@lithia-js/core/server";
3
- import { fromNodeHeaders } from "better-auth/node";
4
- const authContext = new AsyncLocalStorage();
5
- export function authenticated(auth, options = {
6
- error: {
7
- message: "No valid authentication session found",
8
- throw: true,
9
- },
10
- }) {
11
- return async (req, _, next) => {
12
- const session = await auth.api.getSession({
13
- headers: fromNodeHeaders(req.headers),
14
- });
15
- if (!session)
16
- if (options.error.throw) {
17
- throw new RequestError(401, options.error.message);
18
- }
19
- await authContext.run({ session }, async () => {
20
- await next();
21
- });
22
- };
23
- }
24
- export function getAuthContext() {
25
- const context = authContext.getStore();
26
- if (!context) {
27
- throw new Error("No authentication context available");
28
- }
29
- return context;
30
- }
31
- export function useSession() {
32
- const context = getAuthContext();
33
- return context.session;
34
- }
35
- //# sourceMappingURL=useAuth.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"useAuth.mjs","sourceRoot":"","sources":["../src/useAuth.mts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AACrD,OAAO,EAAmB,YAAY,EAAE,MAAM,wBAAwB,CAAC;AACvE,OAAO,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AAcnD,MAAM,WAAW,GAAG,IAAI,iBAAiB,EAAe,CAAC;AAEzD,MAAM,UAAU,aAAa,CAC5B,IAAU,EACV,UAAiC;IAChC,KAAK,EAAE;QACN,OAAO,EAAE,uCAAuC;QAChD,KAAK,EAAE,IAAI;KACX;CACD;IAED,OAAO,KAAK,EAAE,GAAG,EAAE,CAAC,EAAE,IAAI,EAAE,EAAE;QAC7B,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC;YACzC,OAAO,EAAE,eAAe,CAAC,GAAG,CAAC,OAAO,CAAC;SACrC,CAAC,CAAC;QAEH,IAAI,CAAC,OAAO;YACX,IAAI,OAAO,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC;gBACzB,MAAM,IAAI,YAAY,CAAC,GAAG,EAAE,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;YACpD,CAAC;QAEF,MAAM,WAAW,CAAC,GAAG,CAAC,EAAE,OAAO,EAAE,EAAE,KAAK,IAAI,EAAE;YAC7C,MAAM,IAAI,EAAE,CAAC;QACd,CA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