@neondatabase/auth 0.1.0-beta.20 → 0.1.0-beta.21

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.
Files changed (38) hide show
  1. package/dist/{adapter-core-8s6XdCco.mjs → adapter-core-PD5NQpLE.mjs} +1 -1
  2. package/dist/{adapter-core-23fYTUxT.d.mts → adapter-core-y53SWo8w.d.mts} +8 -2
  3. package/dist/{better-auth-react-adapter-D9tIaEyQ.mjs → better-auth-react-adapter-B0XIXPUH.mjs} +1 -1
  4. package/dist/{supabase-adapter-Dr-pKvPt.d.mts → better-auth-react-adapter-B7zoQmoL.d.mts} +56 -144
  5. package/dist/index.d.mts +4 -5
  6. package/dist/index.mjs +2 -2
  7. package/dist/{neon-auth-2f58U8_-.mjs → neon-auth-DUbqaO2v.mjs} +1 -1
  8. package/dist/{neon-auth-CDYpC_O1.d.mts → neon-auth-oDgy6lQm.d.mts} +3 -3
  9. package/dist/next/index.d.mts +1 -64
  10. package/dist/next/index.mjs +4 -35
  11. package/dist/next/server/index.d.mts +50 -6
  12. package/dist/next/server/index.mjs +300 -1
  13. package/dist/react/adapters/index.d.mts +3 -4
  14. package/dist/react/adapters/index.mjs +2 -2
  15. package/dist/react/index.d.mts +4 -5
  16. package/dist/react/index.mjs +3 -3
  17. package/dist/react/ui/index.d.mts +1 -1
  18. package/dist/react/ui/index.mjs +1 -1
  19. package/dist/{supabase-adapter-BYMJSxOT.mjs → supabase-adapter-Bdw6aPGx.mjs} +1 -1
  20. package/dist/{better-auth-react-adapter-QFe5RtaM.d.mts → supabase-adapter-Dm56RKRF.d.mts} +287 -199
  21. package/dist/types/index.d.mts +1 -2
  22. package/dist/ui/.safelist.html +1 -1
  23. package/dist/ui/css.css +1 -1
  24. package/dist/ui/tailwind.css +1 -1
  25. package/dist/ui/theme-inline.css +41 -36
  26. package/dist/ui/theme.css +102 -75
  27. package/dist/{ui-Cg1EZzGG.mjs → ui-CrxGg6vQ.mjs} +27 -18
  28. package/dist/vanilla/adapters/index.d.mts +3 -4
  29. package/dist/vanilla/adapters/index.mjs +2 -2
  30. package/dist/vanilla/index.d.mts +3 -4
  31. package/dist/vanilla/index.mjs +2 -2
  32. package/package.json +3 -3
  33. package/dist/better-auth-types-BUiggBfa.d.mts +0 -9
  34. package/dist/index-Bga0CzOO.d.mts +0 -49
  35. package/dist/middleware-C7jHeulu.mjs +0 -303
  36. /package/dist/{index-BHI9uOzY.d.mts → index-CPnFzULh.d.mts} +0 -0
  37. /package/dist/{index-CSe4aQIZ.d.mts → index-CzsGMS7C.d.mts} +0 -0
  38. /package/dist/{index-LhFpnU-f.d.mts → index-OEBbnNdr.d.mts} +0 -0
@@ -1,303 +0,0 @@
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 };