@next-safe-action/adapter-better-auth 0.1.1 → 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 +12 -12
- package/dist/index.d.mts +12 -12
- package/dist/index.mjs +7 -7
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
<a href="https://github.com/next-safe-action/next-safe-action/packages/adapter-better-auth"><h1>adapter-better-auth</h1></a>
|
|
4
4
|
</div>
|
|
5
5
|
|
|
6
|
-
This adapter offers a way to seamlessly integrate [next-safe-action](https://github.com/next-safe-action/next-safe-action) with [Better Auth](https://www.better-auth.com). It provides a `
|
|
6
|
+
This adapter offers a way to seamlessly integrate [next-safe-action](https://github.com/next-safe-action/next-safe-action) with [Better Auth](https://www.better-auth.com). It provides a `betterAuth()` function that fetches the session, blocks unauthenticated requests, and injects fully-typed `{ user, session }` data into the action context.
|
|
7
7
|
|
|
8
8
|
## Requirements
|
|
9
9
|
|
|
@@ -37,12 +37,12 @@ export const auth = betterAuth({
|
|
|
37
37
|
```ts
|
|
38
38
|
// src/lib/safe-action.ts
|
|
39
39
|
import { createSafeActionClient } from "next-safe-action";
|
|
40
|
-
import {
|
|
40
|
+
import { betterAuth } from "@next-safe-action/adapter-better-auth";
|
|
41
41
|
import { auth } from "./auth";
|
|
42
42
|
|
|
43
43
|
export const actionClient = createSafeActionClient();
|
|
44
44
|
|
|
45
|
-
export const authClient = actionClient.use(
|
|
45
|
+
export const authClient = actionClient.use(betterAuth(auth));
|
|
46
46
|
```
|
|
47
47
|
|
|
48
48
|
### 3. Use it in your actions
|
|
@@ -71,7 +71,7 @@ export const updateProfile = authClient
|
|
|
71
71
|
|
|
72
72
|
## How it works
|
|
73
73
|
|
|
74
|
-
`
|
|
74
|
+
`betterAuth()` creates a pre-validation middleware for the safe action client's `.use()` chain:
|
|
75
75
|
|
|
76
76
|
1. **Fetches the session** by calling `auth.api.getSession({ headers: await headers() })`
|
|
77
77
|
2. **Blocks unauthenticated requests** by calling `unauthorized()` from `next/navigation` when no session exists
|
|
@@ -100,17 +100,17 @@ Pass an `authorize` callback to customize the authorization flow. The session is
|
|
|
100
100
|
|
|
101
101
|
```ts
|
|
102
102
|
import { unauthorized } from "next/navigation";
|
|
103
|
-
import {
|
|
103
|
+
import { betterAuth } from "@next-safe-action/adapter-better-auth";
|
|
104
104
|
import { auth } from "./auth";
|
|
105
105
|
|
|
106
106
|
// Role-based access
|
|
107
107
|
export const adminClient = actionClient.use(
|
|
108
|
-
|
|
109
|
-
authorize: ({
|
|
110
|
-
if (!
|
|
108
|
+
betterAuth(auth, {
|
|
109
|
+
authorize: ({ authData, next }) => {
|
|
110
|
+
if (!authData || authData.user.role !== "admin") {
|
|
111
111
|
unauthorized();
|
|
112
112
|
}
|
|
113
|
-
return next({ ctx: { auth:
|
|
113
|
+
return next({ ctx: { auth: authData } });
|
|
114
114
|
},
|
|
115
115
|
})
|
|
116
116
|
);
|
|
@@ -119,7 +119,7 @@ export const adminClient = actionClient.use(
|
|
|
119
119
|
### `authorize` callback parameters
|
|
120
120
|
|
|
121
121
|
- `auth`: the Better Auth server instance
|
|
122
|
-
- `
|
|
122
|
+
- `authData`: the pre-fetched session data (`{ user, session } | null`)
|
|
123
123
|
- `ctx`: the current action context from preceding middleware
|
|
124
124
|
- `next`: call this to continue the middleware chain, pass `{ ctx }` to inject context
|
|
125
125
|
|
|
@@ -143,7 +143,7 @@ export const auth = betterAuth({
|
|
|
143
143
|
|
|
144
144
|
## API reference
|
|
145
145
|
|
|
146
|
-
### `
|
|
146
|
+
### `betterAuth(auth, opts?)`
|
|
147
147
|
|
|
148
148
|
Creates a middleware function for use with the safe action client's `.use()` method.
|
|
149
149
|
|
|
@@ -158,7 +158,7 @@ Creates a middleware function for use with the safe action client's `.use()` met
|
|
|
158
158
|
|
|
159
159
|
- `BetterAuthContext<Options>`: the context shape added by the middleware (`{ auth: { user, session } }`)
|
|
160
160
|
- `AuthorizeFn<Options, NextCtx>`: the `authorize` callback signature
|
|
161
|
-
- `
|
|
161
|
+
- `BetterAuthOpts<Options, NextCtx>`: the options object type for `betterAuth`
|
|
162
162
|
|
|
163
163
|
## Documentation
|
|
164
164
|
|
package/dist/index.d.mts
CHANGED
|
@@ -3,7 +3,7 @@ import { Auth, BetterAuthOptions } from "better-auth";
|
|
|
3
3
|
|
|
4
4
|
//#region src/index.types.d.ts
|
|
5
5
|
/**
|
|
6
|
-
* The default context shape added by `
|
|
6
|
+
* The default context shape added by `betterAuth`.
|
|
7
7
|
* Contains `auth.user` and `auth.session`, fully typed from the better-auth instance.
|
|
8
8
|
*/
|
|
9
9
|
type BetterAuthContext<O extends BetterAuthOptions> = {
|
|
@@ -14,16 +14,16 @@ type BetterAuthContext<O extends BetterAuthOptions> = {
|
|
|
14
14
|
* Receives the pre-fetched session data, current context, and the `next` function.
|
|
15
15
|
*/
|
|
16
16
|
type AuthorizeFn<O extends BetterAuthOptions, NC extends object, Ctx extends object = object> = (args: {
|
|
17
|
-
|
|
17
|
+
authData: Auth<O>["$Infer"]["Session"] | null;
|
|
18
18
|
ctx: Ctx;
|
|
19
19
|
next: <C extends object>(opts?: {
|
|
20
20
|
ctx?: C;
|
|
21
21
|
}) => Promise<MiddlewareResult<any, C>>;
|
|
22
22
|
}) => Promise<MiddlewareResult<any, NC>>;
|
|
23
23
|
/**
|
|
24
|
-
* Options for `
|
|
24
|
+
* Options for `betterAuth`.
|
|
25
25
|
*/
|
|
26
|
-
type
|
|
26
|
+
type BetterAuthOpts<O extends BetterAuthOptions, NC extends object, Ctx extends object = object> = {
|
|
27
27
|
authorize: AuthorizeFn<O, NC, Ctx>;
|
|
28
28
|
};
|
|
29
29
|
//#endregion
|
|
@@ -42,21 +42,21 @@ type BetterAuthMiddlewareOpts<O extends BetterAuthOptions, NC extends object, Ct
|
|
|
42
42
|
* @example
|
|
43
43
|
* ```ts
|
|
44
44
|
* // Default: fetch session, unauthorized() if absent
|
|
45
|
-
* actionClient.use(
|
|
45
|
+
* actionClient.use(betterAuth(auth));
|
|
46
46
|
*
|
|
47
47
|
* // Custom: check role
|
|
48
|
-
* actionClient.use(
|
|
49
|
-
* authorize: ({
|
|
50
|
-
* if (!
|
|
48
|
+
* actionClient.use(betterAuth(auth, {
|
|
49
|
+
* authorize: ({ authData, next }) => {
|
|
50
|
+
* if (!authData || authData.user.role !== "admin") {
|
|
51
51
|
* unauthorized();
|
|
52
52
|
* }
|
|
53
|
-
* return next({ ctx: { auth:
|
|
53
|
+
* return next({ ctx: { auth: authData } });
|
|
54
54
|
* },
|
|
55
55
|
* }));
|
|
56
56
|
* ```
|
|
57
57
|
*/
|
|
58
|
-
declare function
|
|
59
|
-
declare function
|
|
58
|
+
declare function betterAuth<O extends BetterAuthOptions>(auth: Auth<O>): MiddlewareFn<any, any, object, BetterAuthContext<O>>;
|
|
59
|
+
declare function betterAuth<O extends BetterAuthOptions, NC extends object, Ctx extends object>(auth: Auth<O>, opts: BetterAuthOpts<O, NC, Ctx>): MiddlewareFn<any, any, Ctx, NC>;
|
|
60
60
|
//#endregion
|
|
61
|
-
export { type AuthorizeFn, type BetterAuthContext, type
|
|
61
|
+
export { type AuthorizeFn, type BetterAuthContext, type BetterAuthOpts, betterAuth };
|
|
62
62
|
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.mjs
CHANGED
|
@@ -2,22 +2,22 @@ import { createMiddleware } from "next-safe-action";
|
|
|
2
2
|
import { headers } from "next/headers.js";
|
|
3
3
|
import { unauthorized } from "next/navigation.js";
|
|
4
4
|
//#region src/index.ts
|
|
5
|
-
function
|
|
5
|
+
function betterAuth(auth, opts) {
|
|
6
6
|
return createMiddleware().define(async ({ ctx, next }) => {
|
|
7
|
-
const
|
|
7
|
+
const authData = await auth.api.getSession({ headers: await headers() });
|
|
8
8
|
if (opts?.authorize) return opts.authorize({
|
|
9
|
-
|
|
9
|
+
authData,
|
|
10
10
|
ctx,
|
|
11
11
|
next
|
|
12
12
|
});
|
|
13
|
-
if (!
|
|
13
|
+
if (!authData) unauthorized();
|
|
14
14
|
return next({ ctx: { auth: {
|
|
15
|
-
user:
|
|
16
|
-
session:
|
|
15
|
+
user: authData.user,
|
|
16
|
+
session: authData.session
|
|
17
17
|
} } });
|
|
18
18
|
});
|
|
19
19
|
}
|
|
20
20
|
//#endregion
|
|
21
|
-
export {
|
|
21
|
+
export { betterAuth };
|
|
22
22
|
|
|
23
23
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["import type { Auth, BetterAuthOptions } from \"better-auth\";\nimport { createMiddleware } from \"next-safe-action\";\nimport type { MiddlewareFn } from \"next-safe-action\";\nimport { headers } from \"next/headers\";\nimport { unauthorized } from \"next/navigation\";\nimport type { BetterAuthContext,
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["import type { Auth, BetterAuthOptions } from \"better-auth\";\nimport { createMiddleware } from \"next-safe-action\";\nimport type { MiddlewareFn } from \"next-safe-action\";\nimport { headers } from \"next/headers\";\nimport { unauthorized } from \"next/navigation\";\nimport type { BetterAuthContext, BetterAuthOpts } from \"./index.types\";\n\n/**\n * Creates a next-safe-action middleware that integrates with better-auth.\n *\n * Default behavior: fetches the session via `auth.api.getSession()`, calls `unauthorized()` if\n * no session exists, and injects `{ auth: { user, session } }` into the action context.\n *\n * Pass an `authorize` callback to customize the authorization flow. The session is pre-fetched\n * and passed to the callback, so common customizations (e.g. role checks) don't need to re-fetch.\n *\n * Note: `unauthorized()` requires `experimental.authInterrupts: true` in your `next.config.ts` file.\n *\n * @example\n * ```ts\n * // Default: fetch session, unauthorized() if absent\n * actionClient.use(betterAuth(auth));\n *\n * // Custom: check role\n * actionClient.use(betterAuth(auth, {\n * authorize: ({ authData, next }) => {\n * if (!authData || authData.user.role !== \"admin\") {\n * unauthorized();\n * }\n * return next({ ctx: { auth: authData } });\n * },\n * }));\n * ```\n */\nexport function betterAuth<O extends BetterAuthOptions>(\n\tauth: Auth<O>\n): MiddlewareFn<any, any, object, BetterAuthContext<O>>;\nexport function betterAuth<O extends BetterAuthOptions, NC extends object, Ctx extends object>(\n\tauth: Auth<O>,\n\topts: BetterAuthOpts<O, NC, Ctx>\n): MiddlewareFn<any, any, Ctx, NC>;\nexport function betterAuth<O extends BetterAuthOptions>(\n\tauth: Auth<O>,\n\topts?: BetterAuthOpts<O, any, any>\n) {\n\treturn createMiddleware().define(async ({ ctx, next }) => {\n\t\tconst authData = await auth.api.getSession({ headers: await headers() });\n\n\t\tif (opts?.authorize) {\n\t\t\treturn opts.authorize({ authData, ctx, next });\n\t\t}\n\n\t\tif (!authData) {\n\t\t\tunauthorized();\n\t\t}\n\n\t\treturn next({ ctx: { auth: { user: authData.user, session: authData.session } } });\n\t});\n}\n\nexport type { AuthorizeFn, BetterAuthContext, BetterAuthOpts } from \"./index.types\";\n"],"mappings":";;;;AAyCA,SAAgB,WACf,MACA,MACC;AACD,QAAO,kBAAkB,CAAC,OAAO,OAAO,EAAE,KAAK,WAAW;EACzD,MAAM,WAAW,MAAM,KAAK,IAAI,WAAW,EAAE,SAAS,MAAM,SAAS,EAAE,CAAC;AAExE,MAAI,MAAM,UACT,QAAO,KAAK,UAAU;GAAE;GAAU;GAAK;GAAM,CAAC;AAG/C,MAAI,CAAC,SACJ,eAAc;AAGf,SAAO,KAAK,EAAE,KAAK,EAAE,MAAM;GAAE,MAAM,SAAS;GAAM,SAAS,SAAS;GAAS,EAAE,EAAE,CAAC;GACjF"}
|