@next-safe-action/adapter-better-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 +4 -4
- package/dist/index.d.mts +4 -4
- package/dist/index.mjs +5 -5
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -106,11 +106,11 @@ import { auth } from "./auth";
|
|
|
106
106
|
// Role-based access
|
|
107
107
|
export const adminClient = actionClient.use(
|
|
108
108
|
betterAuth(auth, {
|
|
109
|
-
authorize: ({
|
|
110
|
-
if (!
|
|
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
|
|
package/dist/index.d.mts
CHANGED
|
@@ -14,7 +14,7 @@ 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;
|
|
@@ -46,11 +46,11 @@ type BetterAuthOpts<O extends BetterAuthOptions, NC extends object, Ctx extends
|
|
|
46
46
|
*
|
|
47
47
|
* // Custom: check role
|
|
48
48
|
* actionClient.use(betterAuth(auth, {
|
|
49
|
-
* authorize: ({
|
|
50
|
-
* if (!
|
|
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
|
* ```
|
package/dist/index.mjs
CHANGED
|
@@ -4,16 +4,16 @@ import { unauthorized } from "next/navigation.js";
|
|
|
4
4
|
//#region src/index.ts
|
|
5
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
|
}
|
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, 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: ({
|
|
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"}
|