@neondatabase/auth 0.1.0-beta.2 → 0.1.0-beta.20
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 +112 -18
- package/dist/{adapter-core-C12KoaiU.d.mts → adapter-core-23fYTUxT.d.mts} +175 -627
- package/dist/{adapter-core-BDOw-gBC.mjs → adapter-core-8s6XdCco.mjs} +392 -69
- package/dist/{better-auth-react-adapter-FnBHa2nr.mjs → better-auth-react-adapter-D9tIaEyQ.mjs} +10 -8
- package/dist/better-auth-react-adapter-QFe5RtaM.d.mts +2170 -0
- package/dist/better-auth-types-BUiggBfa.d.mts +9 -0
- package/dist/chunk-VCZJYX65-CLnrj1o7-D6ZQkcc_.mjs +543 -0
- package/dist/constants-2bpp2_-f.mjs +30 -0
- package/dist/index-Bga0CzOO.d.mts +49 -0
- package/dist/index.d.mts +5 -98
- package/dist/index.mjs +2 -1
- package/dist/middleware-C7jHeulu.mjs +303 -0
- package/dist/{neon-auth-C9XTFffv.mjs → neon-auth-2f58U8_-.mjs} +7 -4
- package/dist/neon-auth-CDYpC_O1.d.mts +107 -0
- package/dist/next/index.d.mts +139 -303
- package/dist/next/index.mjs +37 -174
- package/dist/next/server/index.d.mts +389 -0
- package/dist/next/server/index.mjs +432 -0
- package/dist/react/adapters/index.d.mts +5 -4
- package/dist/react/adapters/index.mjs +2 -1
- package/dist/react/index.d.mts +6 -5
- package/dist/react/index.mjs +5 -92
- package/dist/react/ui/index.d.mts +1 -1
- package/dist/react/ui/index.mjs +3 -91
- package/dist/react/ui/server.mjs +1 -1
- package/dist/{supabase-adapter-ggmqWgPe.mjs → supabase-adapter-BYMJSxOT.mjs} +72 -167
- package/dist/supabase-adapter-Dr-pKvPt.d.mts +2258 -0
- package/dist/types/index.d.mts +4 -0
- package/dist/types/index.mjs +3 -0
- package/dist/ui/.safelist.html +3 -0
- package/dist/ui/css.css +2 -2
- package/dist/ui/tailwind.css +3 -2
- package/dist/ui/theme-inline.css +39 -0
- package/dist/ui/theme.css +125 -49
- package/dist/ui-Cg1EZzGG.mjs +12104 -0
- package/dist/vanilla/adapters/index.d.mts +4 -3
- package/dist/vanilla/adapters/index.mjs +2 -1
- package/dist/vanilla/index.d.mts +4 -3
- package/dist/vanilla/index.mjs +2 -1
- package/llms.txt +172 -0
- package/package.json +21 -10
- package/dist/better-auth-react-adapter-BXL48HIU.d.mts +0 -722
- package/dist/supabase-adapter-crabDnl2.d.mts +0 -128
- package/dist/ui-CNFBSekF.mjs +0 -401
- /package/dist/{adapters-Dkx0zoMR.mjs → adapters-B7YKkjaL.mjs} +0 -0
- /package/dist/{index-C-svZlpj.d.mts → index-BHI9uOzY.d.mts} +0 -0
- /package/dist/{index-DuDD6cIY.d.mts → index-CSe4aQIZ.d.mts} +0 -0
- /package/dist/{index-UW23fDSn.d.mts → index-LhFpnU-f.d.mts} +0 -0
package/dist/index.d.mts
CHANGED
|
@@ -1,99 +1,6 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
//#region src/neon-auth.d.ts
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Type representing the Better Auth React client
|
|
11
|
-
*/
|
|
12
|
-
type ReactBetterAuthClient = ReturnType<typeof createAuthClient$1<{
|
|
13
|
-
plugins: SupportedBetterAuthClientPlugins;
|
|
14
|
-
}>>;
|
|
15
|
-
/**`
|
|
16
|
-
* Type representing the Better Auth Vanilla client
|
|
17
|
-
*/
|
|
18
|
-
type VanillaBetterAuthClient = ReturnType<typeof createAuthClient$2<{
|
|
19
|
-
plugins: SupportedBetterAuthClientPlugins;
|
|
20
|
-
}>>;
|
|
21
|
-
/**
|
|
22
|
-
* Union type of all supported auth adapter instances
|
|
23
|
-
*/
|
|
24
|
-
type NeonAuthAdapter = BetterAuthVanillaAdapterInstance | BetterAuthReactAdapterInstance | SupabaseAuthAdapterInstance;
|
|
25
|
-
/**
|
|
26
|
-
* Configuration for createAuthClient
|
|
27
|
-
*/
|
|
28
|
-
interface NeonAuthConfig<T extends NeonAuthAdapter> {
|
|
29
|
-
/** The adapter builder to use. Defaults to BetterAuthVanillaAdapter() if not specified. */
|
|
30
|
-
adapter?: (url: string) => T;
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Resolves the public API type for an adapter.
|
|
34
|
-
* - SupabaseAuthAdapter: exposes its own methods directly (Supabase-compatible API)
|
|
35
|
-
* - BetterAuth adapters: expose the Better Auth client directly
|
|
36
|
-
*/
|
|
37
|
-
type NeonAuthPublicApi<T extends NeonAuthAdapter> = T extends BetterAuthVanillaAdapterInstance ? VanillaBetterAuthClient : T extends BetterAuthReactAdapterInstance ? ReactBetterAuthClient : T;
|
|
38
|
-
/**
|
|
39
|
-
* NeonAuth type - combines base functionality with the appropriate public API
|
|
40
|
-
* This is the return type of createAuthClient()
|
|
41
|
-
*
|
|
42
|
-
* For SupabaseAuthAdapter: exposes Supabase-compatible methods (signInWithPassword, getSession, etc.)
|
|
43
|
-
* For BetterAuth adapters: exposes the Better Auth client directly (signIn.email, signUp.email, etc.)
|
|
44
|
-
*/
|
|
45
|
-
type NeonAuth<T extends NeonAuthAdapter> = {
|
|
46
|
-
adapter: NeonAuthPublicApi<T>;
|
|
47
|
-
getJWTToken: () => Promise<string | null>;
|
|
48
|
-
};
|
|
49
|
-
/**
|
|
50
|
-
* Create a NeonAuth instance that exposes the appropriate API based on the adapter.
|
|
51
|
-
*
|
|
52
|
-
* @param url - The auth service URL (e.g., 'https://auth.example.com')
|
|
53
|
-
* @param config - Configuration with adapter builder
|
|
54
|
-
* @returns NeonAuth instance with the adapter's API exposed directly
|
|
55
|
-
*
|
|
56
|
-
* @example SupabaseAuthAdapter - Supabase-compatible API
|
|
57
|
-
* ```typescript
|
|
58
|
-
* import { createAuthClient, SupabaseAuthAdapter } from '@neondatabase/auth';
|
|
59
|
-
*
|
|
60
|
-
* const auth = createAuthClient('https://auth.example.com', {
|
|
61
|
-
* adapter: SupabaseAuthAdapter(),
|
|
62
|
-
* });
|
|
63
|
-
*
|
|
64
|
-
* // Supabase-compatible methods
|
|
65
|
-
* await auth.signInWithPassword({ email, password });
|
|
66
|
-
* await auth.getSession();
|
|
67
|
-
* ```
|
|
68
|
-
*
|
|
69
|
-
* @example BetterAuthVanillaAdapter - Direct Better Auth API
|
|
70
|
-
* ```typescript
|
|
71
|
-
* import { createAuthClient, BetterAuthVanillaAdapter } from '@neondatabase/auth';
|
|
72
|
-
*
|
|
73
|
-
* const auth = createAuthClient('https://auth.example.com', {
|
|
74
|
-
* adapter: BetterAuthVanillaAdapter(),
|
|
75
|
-
* });
|
|
76
|
-
*
|
|
77
|
-
* // Direct Better Auth API access
|
|
78
|
-
* await auth.signIn.email({ email, password });
|
|
79
|
-
* await auth.signUp.email({ email, password, name: 'John' });
|
|
80
|
-
* await auth.getSession();
|
|
81
|
-
* ```
|
|
82
|
-
*
|
|
83
|
-
* @example BetterAuthReactAdapter - Better Auth with React hooks
|
|
84
|
-
* ```typescript
|
|
85
|
-
* import { createAuthClient, BetterAuthReactAdapter } from '@neondatabase/auth';
|
|
86
|
-
*
|
|
87
|
-
* const auth = createAuthClient('https://auth.example.com', {
|
|
88
|
-
* adapter: BetterAuthReactAdapter(),
|
|
89
|
-
* });
|
|
90
|
-
*
|
|
91
|
-
* // Direct Better Auth API with React hooks
|
|
92
|
-
* await auth.signIn.email({ email, password });
|
|
93
|
-
* const session = auth.useSession(); // React hook
|
|
94
|
-
* ```
|
|
95
|
-
*/
|
|
96
|
-
declare function createInternalNeonAuth<T extends NeonAuthAdapter = BetterAuthVanillaAdapterInstance>(url: string, config?: NeonAuthConfig<T>): NeonAuth<T>;
|
|
97
|
-
declare function createAuthClient<T extends NeonAuthAdapter = BetterAuthVanillaAdapterInstance>(url: string, config?: NeonAuthConfig<T>): NeonAuthPublicApi<T>;
|
|
98
|
-
//#endregion
|
|
1
|
+
import "./better-auth-types-BUiggBfa.mjs";
|
|
2
|
+
import "./adapter-core-23fYTUxT.mjs";
|
|
3
|
+
import "./better-auth-react-adapter-QFe5RtaM.mjs";
|
|
4
|
+
import "./supabase-adapter-Dr-pKvPt.mjs";
|
|
5
|
+
import { a as ReactBetterAuthClient, c as createInternalNeonAuth, i as NeonAuthPublicApi, n as NeonAuthAdapter, o as VanillaBetterAuthClient, r as NeonAuthConfig, s as createAuthClient, t as NeonAuth } from "./neon-auth-CDYpC_O1.mjs";
|
|
99
6
|
export { type NeonAuth, type NeonAuthAdapter, type NeonAuthConfig, type NeonAuthPublicApi, type ReactBetterAuthClient, type VanillaBetterAuthClient, createAuthClient, createInternalNeonAuth };
|
package/dist/index.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import "./adapter-core-8s6XdCco.mjs";
|
|
2
|
+
import { n as createInternalNeonAuth, t as createAuthClient } from "./neon-auth-2f58U8_-.mjs";
|
|
2
3
|
|
|
3
4
|
export { createAuthClient, createInternalNeonAuth };
|
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
import { r as NEON_AUTH_COOKIE_PREFIX, s as NEON_AUTH_SESSION_VERIFIER_PARAM_NAME } from "./constants-2bpp2_-f.mjs";
|
|
2
|
+
import { parseCookies, parseSetCookieHeader } from "better-auth/cookies";
|
|
3
|
+
import { NextRequest, NextResponse } from "next/server";
|
|
4
|
+
import { cookies, headers } from "next/headers";
|
|
5
|
+
|
|
6
|
+
//#region src/next/errors.ts
|
|
7
|
+
const ERRORS = { MISSING_AUTH_BASE_URL: "Missing environment variable: NEON_AUTH_BASE_URL. \n You must provide the auth url of your Neon Auth instance in environment variables" };
|
|
8
|
+
|
|
9
|
+
//#endregion
|
|
10
|
+
//#region src/utils/cookies.ts
|
|
11
|
+
/**
|
|
12
|
+
* Extract the Neon Auth cookies from the request headers.
|
|
13
|
+
* Only returns cookies that start with the NEON_AUTH_COOKIE_PREFIX.
|
|
14
|
+
*
|
|
15
|
+
* @param headers - The request headers or cookie header string.
|
|
16
|
+
* @returns The cookie string with all Neon Auth cookies (e.g., "name=value; name2=value2").
|
|
17
|
+
*/
|
|
18
|
+
const extractNeonAuthCookies = (headers$1) => {
|
|
19
|
+
const cookieHeader = typeof headers$1 === "string" ? headers$1 : headers$1.get("cookie");
|
|
20
|
+
if (!cookieHeader) return "";
|
|
21
|
+
const parsedCookies = parseCookies(cookieHeader);
|
|
22
|
+
const result = [];
|
|
23
|
+
for (const [name, value] of parsedCookies.entries()) if (name.startsWith(NEON_AUTH_COOKIE_PREFIX)) result.push(`${name}=${value}`);
|
|
24
|
+
return result.join("; ");
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Parses the `set-cookie` header from Neon Auth response into a list of cookies.
|
|
28
|
+
*
|
|
29
|
+
* @param setCookieHeader - The `set-cookie` header from Neon Auth response.
|
|
30
|
+
* @returns The list of parsed cookies with their options.
|
|
31
|
+
*/
|
|
32
|
+
const parseSetCookies = (setCookieHeader) => {
|
|
33
|
+
const parsedCookies = parseSetCookieHeader(setCookieHeader);
|
|
34
|
+
const cookies$1 = [];
|
|
35
|
+
for (const entry of parsedCookies.entries()) {
|
|
36
|
+
const [name, parsedCookie] = entry;
|
|
37
|
+
cookies$1.push({
|
|
38
|
+
name,
|
|
39
|
+
value: decodeURIComponent(parsedCookie.value),
|
|
40
|
+
path: parsedCookie.path,
|
|
41
|
+
maxAge: parsedCookie["max-age"] ?? parsedCookie.maxAge,
|
|
42
|
+
httpOnly: parsedCookie.httponly ?? true,
|
|
43
|
+
secure: parsedCookie.secure ?? true,
|
|
44
|
+
sameSite: parsedCookie.samesite ?? "lax",
|
|
45
|
+
partitioned: parsedCookie.partitioned
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
return cookies$1;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
//#endregion
|
|
52
|
+
//#region src/next/constants.ts
|
|
53
|
+
const NEON_AUTH_SESSION_COOKIE_NAME = `${NEON_AUTH_COOKIE_PREFIX}.session_token`;
|
|
54
|
+
const NEON_AUTH_SESSION_CHALLENGE_COOKIE_NAME = `${NEON_AUTH_COOKIE_PREFIX}.session_challange`;
|
|
55
|
+
const NEON_AUTH_HEADER_MIDDLEWARE_NAME = "X-Neon-Auth-Next-Middleware";
|
|
56
|
+
|
|
57
|
+
//#endregion
|
|
58
|
+
//#region src/next/handler/request.ts
|
|
59
|
+
const PROXY_HEADERS = [
|
|
60
|
+
"user-agent",
|
|
61
|
+
"authorization",
|
|
62
|
+
"referer",
|
|
63
|
+
"content-type"
|
|
64
|
+
];
|
|
65
|
+
const handleAuthRequest = async (baseUrl, request, path) => {
|
|
66
|
+
const headers$1 = prepareRequestHeaders(request);
|
|
67
|
+
const body = await parseRequestBody(request);
|
|
68
|
+
try {
|
|
69
|
+
const upstreamURL = getUpstreamURL(baseUrl, path, { originalUrl: new URL(request.url) });
|
|
70
|
+
return await fetch(upstreamURL.toString(), {
|
|
71
|
+
method: request.method,
|
|
72
|
+
headers: headers$1,
|
|
73
|
+
body
|
|
74
|
+
});
|
|
75
|
+
} catch (error) {
|
|
76
|
+
const message = error instanceof Error ? error.message : "Internal Server Error";
|
|
77
|
+
console.error(`[AuthError] ${message}`, error);
|
|
78
|
+
return new Response(`[AuthError] ${message}`, { status: 500 });
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
const getUpstreamURL = (baseUrl, path, { originalUrl }) => {
|
|
82
|
+
const url = new URL(`${baseUrl}/${path}`);
|
|
83
|
+
if (originalUrl) {
|
|
84
|
+
url.search = originalUrl.search;
|
|
85
|
+
return url;
|
|
86
|
+
}
|
|
87
|
+
return url;
|
|
88
|
+
};
|
|
89
|
+
const prepareRequestHeaders = (request) => {
|
|
90
|
+
const headers$1 = new Headers();
|
|
91
|
+
for (const header of PROXY_HEADERS) if (request.headers.get(header)) headers$1.set(header, request.headers.get(header));
|
|
92
|
+
headers$1.set("Origin", getOrigin(request));
|
|
93
|
+
headers$1.set("Cookie", extractNeonAuthCookies(request.headers));
|
|
94
|
+
headers$1.set(NEON_AUTH_HEADER_MIDDLEWARE_NAME, "true");
|
|
95
|
+
return headers$1;
|
|
96
|
+
};
|
|
97
|
+
const getOrigin = (request) => {
|
|
98
|
+
return request.headers.get("origin") || request.headers.get("referer")?.split("/").slice(0, 3).join("/") || new URL(request.url).origin;
|
|
99
|
+
};
|
|
100
|
+
const parseRequestBody = async (request) => {
|
|
101
|
+
if (request.body) return request.text();
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
//#endregion
|
|
105
|
+
//#region src/next/handler/response.ts
|
|
106
|
+
const RESPONSE_HEADERS_ALLOWLIST = [
|
|
107
|
+
"content-type",
|
|
108
|
+
"content-length",
|
|
109
|
+
"content-encoding",
|
|
110
|
+
"transfer-encoding",
|
|
111
|
+
"connection",
|
|
112
|
+
"date",
|
|
113
|
+
"set-cookie",
|
|
114
|
+
"set-auth-jwt",
|
|
115
|
+
"set-auth-token",
|
|
116
|
+
"x-neon-ret-request-id"
|
|
117
|
+
];
|
|
118
|
+
const handleAuthResponse = async (response) => {
|
|
119
|
+
return new Response(response.body, {
|
|
120
|
+
status: response.status,
|
|
121
|
+
statusText: response.statusText,
|
|
122
|
+
headers: prepareResponseHeaders(response)
|
|
123
|
+
});
|
|
124
|
+
};
|
|
125
|
+
const prepareResponseHeaders = (response) => {
|
|
126
|
+
const headers$1 = new Headers();
|
|
127
|
+
for (const header of RESPONSE_HEADERS_ALLOWLIST) {
|
|
128
|
+
const value = response.headers.get(header);
|
|
129
|
+
if (value) headers$1.set(header, value);
|
|
130
|
+
}
|
|
131
|
+
return headers$1;
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
//#endregion
|
|
135
|
+
//#region src/next/handler/index.ts
|
|
136
|
+
/**
|
|
137
|
+
*
|
|
138
|
+
* An API route handler to handle the auth requests from the client and proxy them to the Neon Auth.
|
|
139
|
+
*
|
|
140
|
+
* @returns A Next.js API handler functions those can be used in a Next.js route.
|
|
141
|
+
*
|
|
142
|
+
* @example
|
|
143
|
+
* Mount the `authApiHandler` to an API route. Create a route file inside `/api/auth/[...all]/route.ts` directory.
|
|
144
|
+
* And add the following code:
|
|
145
|
+
*
|
|
146
|
+
* ```ts
|
|
147
|
+
* // app/api/auth/[...all]/route.ts
|
|
148
|
+
* import { authApiHandler } from '@neondatabase/auth/next';
|
|
149
|
+
*
|
|
150
|
+
* export const { GET, POST } = authApiHandler();
|
|
151
|
+
* ```
|
|
152
|
+
*/
|
|
153
|
+
function authApiHandler() {
|
|
154
|
+
const baseURL = process.env.NEON_AUTH_BASE_URL;
|
|
155
|
+
if (!baseURL) throw new Error(ERRORS.MISSING_AUTH_BASE_URL);
|
|
156
|
+
const handler = async (request, { params }) => {
|
|
157
|
+
return await handleAuthResponse(await handleAuthRequest(baseURL, request, (await params).path.join("/")));
|
|
158
|
+
};
|
|
159
|
+
return {
|
|
160
|
+
GET: handler,
|
|
161
|
+
POST: handler,
|
|
162
|
+
PUT: handler,
|
|
163
|
+
DELETE: handler,
|
|
164
|
+
PATCH: handler
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
//#endregion
|
|
169
|
+
//#region src/next/auth/cookies.ts
|
|
170
|
+
/**
|
|
171
|
+
* Extract the Neon Auth cookies from the response headers.
|
|
172
|
+
* @deprecated Use parseSetCookies instead
|
|
173
|
+
*/
|
|
174
|
+
const extractResponseCookies = (headers$1) => {
|
|
175
|
+
const cookieHeader = headers$1.get("set-cookie");
|
|
176
|
+
if (!cookieHeader) return [];
|
|
177
|
+
return cookieHeader.split(", ").map((c) => c.trim());
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
//#endregion
|
|
181
|
+
//#region src/next/middleware/oauth.ts
|
|
182
|
+
const needsSessionVerification = (request) => {
|
|
183
|
+
const hasVerifier = request.nextUrl.searchParams.has(NEON_AUTH_SESSION_VERIFIER_PARAM_NAME);
|
|
184
|
+
const hasChallenge = request.cookies.get(NEON_AUTH_SESSION_CHALLENGE_COOKIE_NAME);
|
|
185
|
+
return hasVerifier && hasChallenge;
|
|
186
|
+
};
|
|
187
|
+
const exchangeOAuthToken = async (request, baseUrl) => {
|
|
188
|
+
const url = request.nextUrl;
|
|
189
|
+
const verifier = url.searchParams.get(NEON_AUTH_SESSION_VERIFIER_PARAM_NAME);
|
|
190
|
+
const challenge = request.cookies.get(NEON_AUTH_SESSION_CHALLENGE_COOKIE_NAME);
|
|
191
|
+
if (!verifier || !challenge) return null;
|
|
192
|
+
const response = await handleAuthResponse(await handleAuthRequest(baseUrl, new Request(request.url, {
|
|
193
|
+
method: "GET",
|
|
194
|
+
headers: request.headers
|
|
195
|
+
}), "get-session"));
|
|
196
|
+
if (response.ok) {
|
|
197
|
+
const headers$1 = new Headers();
|
|
198
|
+
const cookies$1 = extractResponseCookies(response.headers);
|
|
199
|
+
for (const cookie of cookies$1) headers$1.append("Set-Cookie", cookie);
|
|
200
|
+
url.searchParams.delete(NEON_AUTH_SESSION_VERIFIER_PARAM_NAME);
|
|
201
|
+
return NextResponse.redirect(url, { headers: headers$1 });
|
|
202
|
+
}
|
|
203
|
+
return null;
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
//#endregion
|
|
207
|
+
//#region src/next/env-variables.ts
|
|
208
|
+
const NEON_AUTH_BASE_URL = process.env.NEON_AUTH_BASE_URL;
|
|
209
|
+
|
|
210
|
+
//#endregion
|
|
211
|
+
//#region src/next/auth/session.ts
|
|
212
|
+
/**
|
|
213
|
+
* A utility function to be used in react server components fetch the session details from the Neon Auth API, if session token is available in cookie.
|
|
214
|
+
*
|
|
215
|
+
* @returns - `{ session: Session, user: User }` | `{ session: null, user: null}`.
|
|
216
|
+
*
|
|
217
|
+
* @example
|
|
218
|
+
* ```ts
|
|
219
|
+
* import { neonAuth } from "@neondatabase/auth/next"
|
|
220
|
+
*
|
|
221
|
+
* const { session, user } = await neonAuth()
|
|
222
|
+
* ```
|
|
223
|
+
*/
|
|
224
|
+
const neonAuth = async () => {
|
|
225
|
+
return await fetchSession();
|
|
226
|
+
};
|
|
227
|
+
/**
|
|
228
|
+
* A utility function to fetch the session details from the Neon Auth API, if session token is available in cookie.
|
|
229
|
+
*
|
|
230
|
+
* @returns - `{ session: Session, user: User }` | `{ session: null, user: null}`.
|
|
231
|
+
*/
|
|
232
|
+
const fetchSession = async () => {
|
|
233
|
+
const baseUrl = NEON_AUTH_BASE_URL;
|
|
234
|
+
const requestHeaders = await headers();
|
|
235
|
+
const upstreamURL = getUpstreamURL(baseUrl, "get-session", { originalUrl: new URL("get-session", baseUrl) });
|
|
236
|
+
const response = await fetch(upstreamURL.toString(), {
|
|
237
|
+
method: "GET",
|
|
238
|
+
headers: { Cookie: extractNeonAuthCookies(requestHeaders) }
|
|
239
|
+
});
|
|
240
|
+
const body = await response.json();
|
|
241
|
+
const cookieHeader = response.headers.get("set-cookie");
|
|
242
|
+
if (cookieHeader) {
|
|
243
|
+
const cookieStore = await cookies();
|
|
244
|
+
parseSetCookies(cookieHeader).map((cookie) => {
|
|
245
|
+
cookieStore.set(cookie.name, cookie.value, cookie);
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
if (!response.ok || body === null) return {
|
|
249
|
+
session: null,
|
|
250
|
+
user: null
|
|
251
|
+
};
|
|
252
|
+
return {
|
|
253
|
+
session: body.session,
|
|
254
|
+
user: body.user
|
|
255
|
+
};
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
//#endregion
|
|
259
|
+
//#region src/next/middleware/index.ts
|
|
260
|
+
const SKIP_ROUTES = [
|
|
261
|
+
"/api/auth",
|
|
262
|
+
"/auth/callback",
|
|
263
|
+
"/auth/sign-in",
|
|
264
|
+
"/auth/sign-up",
|
|
265
|
+
"/auth/magic-link",
|
|
266
|
+
"/auth/email-otp",
|
|
267
|
+
"/auth/forgot-password"
|
|
268
|
+
];
|
|
269
|
+
/**
|
|
270
|
+
* A Next.js middleware to protect routes from unauthenticated requests and refresh the session if required.
|
|
271
|
+
*
|
|
272
|
+
* @param loginUrl - The URL to redirect to when the user is not authenticated.
|
|
273
|
+
* @returns A middleware function that can be used in the Next.js app.
|
|
274
|
+
*
|
|
275
|
+
* @example
|
|
276
|
+
* ```ts
|
|
277
|
+
* import { neonAuthMiddleware } from "@neondatabase/auth/next"
|
|
278
|
+
*
|
|
279
|
+
* export default neonAuthMiddleware({
|
|
280
|
+
* loginUrl: '/auth/sign-in',
|
|
281
|
+
* });
|
|
282
|
+
* ```
|
|
283
|
+
*/
|
|
284
|
+
function neonAuthMiddleware({ loginUrl = "/auth/sign-in" }) {
|
|
285
|
+
const baseUrl = NEON_AUTH_BASE_URL;
|
|
286
|
+
if (!baseUrl) throw new Error(ERRORS.MISSING_AUTH_BASE_URL);
|
|
287
|
+
return async (request) => {
|
|
288
|
+
const { pathname } = request.nextUrl;
|
|
289
|
+
if (pathname.startsWith(loginUrl)) return NextResponse.next();
|
|
290
|
+
if (needsSessionVerification(request)) {
|
|
291
|
+
const response = await exchangeOAuthToken(request, baseUrl);
|
|
292
|
+
if (response !== null) return response;
|
|
293
|
+
}
|
|
294
|
+
if (SKIP_ROUTES.some((route) => pathname.startsWith(route))) return NextResponse.next();
|
|
295
|
+
if ((await fetchSession()).session === null) return NextResponse.redirect(new URL(loginUrl, request.url));
|
|
296
|
+
const reqHeaders = new Headers(request.headers);
|
|
297
|
+
reqHeaders.set(NEON_AUTH_HEADER_MIDDLEWARE_NAME, "true");
|
|
298
|
+
return NextResponse.next({ request: { headers: reqHeaders } });
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
//#endregion
|
|
303
|
+
export { parseSetCookies as a, extractNeonAuthCookies as i, neonAuth as n, authApiHandler as r, neonAuthMiddleware as t };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { n as BetterAuthVanillaAdapter } from "./supabase-adapter-
|
|
1
|
+
import { n as BetterAuthVanillaAdapter } from "./supabase-adapter-BYMJSxOT.mjs";
|
|
2
2
|
|
|
3
3
|
//#region src/neon-auth.ts
|
|
4
4
|
/**
|
|
@@ -49,13 +49,16 @@ import { n as BetterAuthVanillaAdapter } from "./supabase-adapter-ggmqWgPe.mjs";
|
|
|
49
49
|
* ```
|
|
50
50
|
*/
|
|
51
51
|
function createInternalNeonAuth(url, config) {
|
|
52
|
-
const
|
|
52
|
+
const adapterBuilder = config?.adapter ?? BetterAuthVanillaAdapter();
|
|
53
|
+
const { fetchOptions } = config ?? {};
|
|
54
|
+
const adapter = adapterBuilder(url, fetchOptions);
|
|
55
|
+
const allowAnonymous = config?.allowAnonymous ?? false;
|
|
53
56
|
if (!(typeof adapter.initialize === "function")) return {
|
|
54
|
-
getJWTToken: adapter.getJWTToken
|
|
57
|
+
getJWTToken: () => adapter.getJWTToken(allowAnonymous),
|
|
55
58
|
adapter: adapter.getBetterAuthInstance()
|
|
56
59
|
};
|
|
57
60
|
return {
|
|
58
|
-
getJWTToken: adapter.getJWTToken
|
|
61
|
+
getJWTToken: () => adapter.getJWTToken(allowAnonymous),
|
|
59
62
|
adapter
|
|
60
63
|
};
|
|
61
64
|
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { r as SupportedBetterAuthClientPlugins } from "./adapter-core-23fYTUxT.mjs";
|
|
2
|
+
import { n as BetterAuthReactAdapterInstance } from "./better-auth-react-adapter-QFe5RtaM.mjs";
|
|
3
|
+
import { r as SupabaseAuthAdapterInstance, s as BetterAuthVanillaAdapterInstance } from "./supabase-adapter-Dr-pKvPt.mjs";
|
|
4
|
+
import { createAuthClient } from "better-auth/react";
|
|
5
|
+
import { createAuthClient as createAuthClient$1 } from "better-auth/client";
|
|
6
|
+
|
|
7
|
+
//#region src/neon-auth.d.ts
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Type representing the Better Auth React client
|
|
11
|
+
*/
|
|
12
|
+
type ReactBetterAuthClient = ReturnType<typeof createAuthClient<{
|
|
13
|
+
plugins: SupportedBetterAuthClientPlugins;
|
|
14
|
+
}>>;
|
|
15
|
+
/**
|
|
16
|
+
* Type representing the Better Auth Vanilla client
|
|
17
|
+
*/
|
|
18
|
+
type VanillaBetterAuthClient = ReturnType<typeof createAuthClient$1<{
|
|
19
|
+
plugins: SupportedBetterAuthClientPlugins;
|
|
20
|
+
}>>;
|
|
21
|
+
/**
|
|
22
|
+
* Union type of all supported auth adapter instances
|
|
23
|
+
*/
|
|
24
|
+
type NeonAuthAdapter = BetterAuthVanillaAdapterInstance | BetterAuthReactAdapterInstance | SupabaseAuthAdapterInstance;
|
|
25
|
+
/**
|
|
26
|
+
* Configuration for createAuthClient
|
|
27
|
+
*/
|
|
28
|
+
interface NeonAuthConfig<T extends NeonAuthAdapter> {
|
|
29
|
+
/** The adapter builder to use. Defaults to BetterAuthVanillaAdapter() if not specified. */
|
|
30
|
+
adapter?: (url: string, fetchOptions?: {
|
|
31
|
+
headers?: Record<string, string>;
|
|
32
|
+
}) => T;
|
|
33
|
+
/**
|
|
34
|
+
* When true, automatically uses an anonymous token when no user session exists.
|
|
35
|
+
* This enables RLS-based data access for users with the anonymous role.
|
|
36
|
+
* @default false
|
|
37
|
+
*/
|
|
38
|
+
allowAnonymous?: boolean;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Resolves the public API type for an adapter.
|
|
42
|
+
* - SupabaseAuthAdapter: exposes its own methods directly (Supabase-compatible API)
|
|
43
|
+
* - BetterAuth adapters: expose the Better Auth client directly
|
|
44
|
+
*/
|
|
45
|
+
type NeonAuthPublicApi<T extends NeonAuthAdapter> = T extends BetterAuthVanillaAdapterInstance ? VanillaBetterAuthClient : T extends BetterAuthReactAdapterInstance ? ReactBetterAuthClient : T;
|
|
46
|
+
/**
|
|
47
|
+
* NeonAuth type - combines base functionality with the appropriate public API
|
|
48
|
+
* This is the return type of createAuthClient()
|
|
49
|
+
*
|
|
50
|
+
* For SupabaseAuthAdapter: exposes Supabase-compatible methods (signInWithPassword, getSession, etc.)
|
|
51
|
+
* For BetterAuth adapters: exposes the Better Auth client directly (signIn.email, signUp.email, etc.)
|
|
52
|
+
*/
|
|
53
|
+
type NeonAuth<T extends NeonAuthAdapter> = {
|
|
54
|
+
adapter: NeonAuthPublicApi<T>;
|
|
55
|
+
getJWTToken: () => Promise<string | null>;
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* Create a NeonAuth instance that exposes the appropriate API based on the adapter.
|
|
59
|
+
*
|
|
60
|
+
* @param url - The auth service URL (e.g., 'https://auth.example.com')
|
|
61
|
+
* @param config - Configuration with adapter builder
|
|
62
|
+
* @returns NeonAuth instance with the adapter's API exposed directly
|
|
63
|
+
*
|
|
64
|
+
* @example SupabaseAuthAdapter - Supabase-compatible API
|
|
65
|
+
* ```typescript
|
|
66
|
+
* import { createAuthClient, SupabaseAuthAdapter } from '@neondatabase/auth';
|
|
67
|
+
*
|
|
68
|
+
* const auth = createAuthClient('https://auth.example.com', {
|
|
69
|
+
* adapter: SupabaseAuthAdapter(),
|
|
70
|
+
* });
|
|
71
|
+
*
|
|
72
|
+
* // Supabase-compatible methods
|
|
73
|
+
* await auth.signInWithPassword({ email, password });
|
|
74
|
+
* await auth.getSession();
|
|
75
|
+
* ```
|
|
76
|
+
*
|
|
77
|
+
* @example BetterAuthVanillaAdapter - Direct Better Auth API
|
|
78
|
+
* ```typescript
|
|
79
|
+
* import { createAuthClient, BetterAuthVanillaAdapter } from '@neondatabase/auth';
|
|
80
|
+
*
|
|
81
|
+
* const auth = createAuthClient('https://auth.example.com', {
|
|
82
|
+
* adapter: BetterAuthVanillaAdapter(),
|
|
83
|
+
* });
|
|
84
|
+
*
|
|
85
|
+
* // Direct Better Auth API access
|
|
86
|
+
* await auth.signIn.email({ email, password });
|
|
87
|
+
* await auth.signUp.email({ email, password, name: 'John' });
|
|
88
|
+
* await auth.getSession();
|
|
89
|
+
* ```
|
|
90
|
+
*
|
|
91
|
+
* @example BetterAuthReactAdapter - Better Auth with React hooks
|
|
92
|
+
* ```typescript
|
|
93
|
+
* import { createAuthClient, BetterAuthReactAdapter } from '@neondatabase/auth';
|
|
94
|
+
*
|
|
95
|
+
* const auth = createAuthClient('https://auth.example.com', {
|
|
96
|
+
* adapter: BetterAuthReactAdapter(),
|
|
97
|
+
* });
|
|
98
|
+
*
|
|
99
|
+
* // Direct Better Auth API with React hooks
|
|
100
|
+
* await auth.signIn.email({ email, password });
|
|
101
|
+
* const session = auth.useSession(); // React hook
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
104
|
+
declare function createInternalNeonAuth<T extends NeonAuthAdapter = BetterAuthVanillaAdapterInstance>(url: string, config?: NeonAuthConfigInternal<T>): NeonAuth<T>;
|
|
105
|
+
declare function createAuthClient$2<T extends NeonAuthAdapter = BetterAuthVanillaAdapterInstance>(url: string, config?: NeonAuthConfig<T>): NeonAuthPublicApi<T>;
|
|
106
|
+
//#endregion
|
|
107
|
+
export { ReactBetterAuthClient as a, createInternalNeonAuth as c, NeonAuthPublicApi as i, NeonAuthAdapter as n, VanillaBetterAuthClient as o, NeonAuthConfig as r, createAuthClient$2 as s, NeonAuth as t };
|