@next-safe-action/adapter-better-auth 0.1.0 → 0.1.1
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/dist/index.d.mts +7 -8
- package/dist/index.mjs +3 -3
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Auth, BetterAuthOptions } from "better-auth";
|
|
2
1
|
import { MiddlewareFn, MiddlewareResult } from "next-safe-action";
|
|
2
|
+
import { Auth, BetterAuthOptions } from "better-auth";
|
|
3
3
|
|
|
4
4
|
//#region src/index.types.d.ts
|
|
5
5
|
/**
|
|
@@ -11,12 +11,11 @@ type BetterAuthContext<O extends BetterAuthOptions> = {
|
|
|
11
11
|
};
|
|
12
12
|
/**
|
|
13
13
|
* Authorize callback signature for custom authorization logic.
|
|
14
|
-
* Receives the
|
|
14
|
+
* Receives the pre-fetched session data, current context, and the `next` function.
|
|
15
15
|
*/
|
|
16
|
-
type AuthorizeFn<O extends BetterAuthOptions, NC extends object> = (args: {
|
|
17
|
-
auth: Auth<O>;
|
|
16
|
+
type AuthorizeFn<O extends BetterAuthOptions, NC extends object, Ctx extends object = object> = (args: {
|
|
18
17
|
sessionData: Auth<O>["$Infer"]["Session"] | null;
|
|
19
|
-
ctx:
|
|
18
|
+
ctx: Ctx;
|
|
20
19
|
next: <C extends object>(opts?: {
|
|
21
20
|
ctx?: C;
|
|
22
21
|
}) => Promise<MiddlewareResult<any, C>>;
|
|
@@ -24,8 +23,8 @@ type AuthorizeFn<O extends BetterAuthOptions, NC extends object> = (args: {
|
|
|
24
23
|
/**
|
|
25
24
|
* Options for `betterAuthMiddleware`.
|
|
26
25
|
*/
|
|
27
|
-
type BetterAuthMiddlewareOpts<O extends BetterAuthOptions, NC extends object> = {
|
|
28
|
-
authorize: AuthorizeFn<O, NC>;
|
|
26
|
+
type BetterAuthMiddlewareOpts<O extends BetterAuthOptions, NC extends object, Ctx extends object = object> = {
|
|
27
|
+
authorize: AuthorizeFn<O, NC, Ctx>;
|
|
29
28
|
};
|
|
30
29
|
//#endregion
|
|
31
30
|
//#region src/index.d.ts
|
|
@@ -57,7 +56,7 @@ type BetterAuthMiddlewareOpts<O extends BetterAuthOptions, NC extends object> =
|
|
|
57
56
|
* ```
|
|
58
57
|
*/
|
|
59
58
|
declare function betterAuthMiddleware<O extends BetterAuthOptions>(auth: Auth<O>): MiddlewareFn<any, any, object, BetterAuthContext<O>>;
|
|
60
|
-
declare function betterAuthMiddleware<O extends BetterAuthOptions, NC extends object>(auth: Auth<O>, opts: BetterAuthMiddlewareOpts<O, NC>): MiddlewareFn<any, any,
|
|
59
|
+
declare function betterAuthMiddleware<O extends BetterAuthOptions, NC extends object, Ctx extends object>(auth: Auth<O>, opts: BetterAuthMiddlewareOpts<O, NC, Ctx>): MiddlewareFn<any, any, Ctx, NC>;
|
|
61
60
|
//#endregion
|
|
62
61
|
export { type AuthorizeFn, type BetterAuthContext, type BetterAuthMiddlewareOpts, betterAuthMiddleware };
|
|
63
62
|
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.mjs
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
+
import { createMiddleware } from "next-safe-action";
|
|
1
2
|
import { headers } from "next/headers.js";
|
|
2
3
|
import { unauthorized } from "next/navigation.js";
|
|
3
4
|
//#region src/index.ts
|
|
4
5
|
function betterAuthMiddleware(auth, opts) {
|
|
5
|
-
return async ({ ctx, next }) => {
|
|
6
|
+
return createMiddleware().define(async ({ ctx, next }) => {
|
|
6
7
|
const sessionData = await auth.api.getSession({ headers: await headers() });
|
|
7
8
|
if (opts?.authorize) return opts.authorize({
|
|
8
|
-
auth,
|
|
9
9
|
sessionData,
|
|
10
10
|
ctx,
|
|
11
11
|
next
|
|
@@ -15,7 +15,7 @@ function betterAuthMiddleware(auth, opts) {
|
|
|
15
15
|
user: sessionData.user,
|
|
16
16
|
session: sessionData.session
|
|
17
17
|
} } });
|
|
18
|
-
};
|
|
18
|
+
});
|
|
19
19
|
}
|
|
20
20
|
//#endregion
|
|
21
21
|
export { betterAuthMiddleware };
|
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 type { MiddlewareFn } from \"next-safe-action\";\nimport { headers } from \"next/headers\";\nimport { unauthorized } from \"next/navigation\";\nimport type { BetterAuthContext, BetterAuthMiddlewareOpts } 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(betterAuthMiddleware(auth));\n *\n * // Custom: check role\n * actionClient.use(betterAuthMiddleware(auth, {\n * authorize: ({ sessionData, next }) => {\n * if (!sessionData || sessionData.user.role !== \"admin\") {\n * unauthorized();\n * }\n * return next({ ctx: { auth: sessionData } });\n * },\n * }));\n * ```\n */\nexport function betterAuthMiddleware<O extends BetterAuthOptions>(\n\tauth: Auth<O>\n): MiddlewareFn<any, any, object, BetterAuthContext<O>>;\nexport function betterAuthMiddleware<O extends BetterAuthOptions, NC extends object>(\n\tauth: Auth<O>,\n\topts: BetterAuthMiddlewareOpts<O, NC>\n): MiddlewareFn<any, any,
|
|
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, BetterAuthMiddlewareOpts } 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(betterAuthMiddleware(auth));\n *\n * // Custom: check role\n * actionClient.use(betterAuthMiddleware(auth, {\n * authorize: ({ sessionData, next }) => {\n * if (!sessionData || sessionData.user.role !== \"admin\") {\n * unauthorized();\n * }\n * return next({ ctx: { auth: sessionData } });\n * },\n * }));\n * ```\n */\nexport function betterAuthMiddleware<O extends BetterAuthOptions>(\n\tauth: Auth<O>\n): MiddlewareFn<any, any, object, BetterAuthContext<O>>;\nexport function betterAuthMiddleware<O extends BetterAuthOptions, NC extends object, Ctx extends object>(\n\tauth: Auth<O>,\n\topts: BetterAuthMiddlewareOpts<O, NC, Ctx>\n): MiddlewareFn<any, any, Ctx, NC>;\nexport function betterAuthMiddleware<O extends BetterAuthOptions>(\n\tauth: Auth<O>,\n\topts?: BetterAuthMiddlewareOpts<O, any, any>\n) {\n\treturn createMiddleware().define(async ({ ctx, next }) => {\n\t\tconst sessionData = await auth.api.getSession({ headers: await headers() });\n\n\t\tif (opts?.authorize) {\n\t\t\treturn opts.authorize({ sessionData, ctx, next });\n\t\t}\n\n\t\tif (!sessionData) {\n\t\t\tunauthorized();\n\t\t}\n\n\t\treturn next({ ctx: { auth: { user: sessionData.user, session: sessionData.session } } });\n\t});\n}\n\nexport type { AuthorizeFn, BetterAuthContext, BetterAuthMiddlewareOpts } from \"./index.types\";\n"],"mappings":";;;;AAyCA,SAAgB,qBACf,MACA,MACC;AACD,QAAO,kBAAkB,CAAC,OAAO,OAAO,EAAE,KAAK,WAAW;EACzD,MAAM,cAAc,MAAM,KAAK,IAAI,WAAW,EAAE,SAAS,MAAM,SAAS,EAAE,CAAC;AAE3E,MAAI,MAAM,UACT,QAAO,KAAK,UAAU;GAAE;GAAa;GAAK;GAAM,CAAC;AAGlD,MAAI,CAAC,YACJ,eAAc;AAGf,SAAO,KAAK,EAAE,KAAK,EAAE,MAAM;GAAE,MAAM,YAAY;GAAM,SAAS,YAAY;GAAS,EAAE,EAAE,CAAC;GACvF"}
|