@farm.js/auth 0.1.61 → 0.1.63
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/client.native.d.ts +275 -0
- package/dist/client.native.d.ts.map +1 -0
- package/dist/client.native.js +191 -0
- package/dist/config.d.ts +2 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +2 -1
- package/dist/internal.d.ts +6 -0
- package/dist/internal.d.ts.map +1 -1
- package/dist/internal.js +15 -2
- package/dist/runtime.d.ts +12 -1
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +129 -8
- package/package.json +3 -3
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
import type * as web from "./client.js";
|
|
2
|
+
import type { FarmAuthCredentials, FarmAuthSignUpInput } from "./types.js";
|
|
3
|
+
/**
|
|
4
|
+
* Same options as the web client. `basePath` must match `auth.basePath` and, like every
|
|
5
|
+
* `serverFetch` path, sits under the app's base path on `mobile.server`. `baseURL` does not apply:
|
|
6
|
+
* native screens always call `mobile.server`, the only origin the device token is sent to.
|
|
7
|
+
*/
|
|
8
|
+
export declare const createFarmAuthClient: typeof web.createFarmAuthClient;
|
|
9
|
+
export declare const getSession: () => Promise<{
|
|
10
|
+
data: {
|
|
11
|
+
user: import("better-auth").StripEmptyObjects<{
|
|
12
|
+
id: string;
|
|
13
|
+
createdAt: Date;
|
|
14
|
+
updatedAt: Date;
|
|
15
|
+
email: string;
|
|
16
|
+
emailVerified: boolean;
|
|
17
|
+
name: string;
|
|
18
|
+
image?: string | null | undefined;
|
|
19
|
+
}>;
|
|
20
|
+
session: import("better-auth").StripEmptyObjects<{
|
|
21
|
+
id: string;
|
|
22
|
+
createdAt: Date;
|
|
23
|
+
updatedAt: Date;
|
|
24
|
+
userId: string;
|
|
25
|
+
expiresAt: Date;
|
|
26
|
+
token: string;
|
|
27
|
+
ipAddress?: string | null | undefined;
|
|
28
|
+
userAgent?: string | null | undefined;
|
|
29
|
+
}>;
|
|
30
|
+
} | null;
|
|
31
|
+
error: null;
|
|
32
|
+
} | {
|
|
33
|
+
data: null;
|
|
34
|
+
error: {
|
|
35
|
+
code?: string | undefined | undefined;
|
|
36
|
+
message?: string | undefined | undefined;
|
|
37
|
+
status: number;
|
|
38
|
+
statusText: string;
|
|
39
|
+
};
|
|
40
|
+
}>, signIn: (input: FarmAuthCredentials) => Promise<{
|
|
41
|
+
data: Omit<{
|
|
42
|
+
redirect: boolean;
|
|
43
|
+
token: string;
|
|
44
|
+
url?: string | undefined;
|
|
45
|
+
user: {
|
|
46
|
+
id: string;
|
|
47
|
+
createdAt: Date;
|
|
48
|
+
updatedAt: Date;
|
|
49
|
+
email: string;
|
|
50
|
+
emailVerified: boolean;
|
|
51
|
+
name: string;
|
|
52
|
+
image?: string | null | undefined | undefined;
|
|
53
|
+
};
|
|
54
|
+
}, "user"> & {
|
|
55
|
+
user: import("better-auth").StripEmptyObjects<{
|
|
56
|
+
id: string;
|
|
57
|
+
createdAt: Date;
|
|
58
|
+
updatedAt: Date;
|
|
59
|
+
email: string;
|
|
60
|
+
emailVerified: boolean;
|
|
61
|
+
name: string;
|
|
62
|
+
image?: string | null | undefined;
|
|
63
|
+
}>;
|
|
64
|
+
};
|
|
65
|
+
error: null;
|
|
66
|
+
} | {
|
|
67
|
+
data: null;
|
|
68
|
+
error: {
|
|
69
|
+
code?: string | undefined | undefined;
|
|
70
|
+
message?: string | undefined | undefined;
|
|
71
|
+
status: number;
|
|
72
|
+
statusText: string;
|
|
73
|
+
};
|
|
74
|
+
}>, signOut: () => Promise<{
|
|
75
|
+
data: {
|
|
76
|
+
success: boolean;
|
|
77
|
+
url: string | undefined;
|
|
78
|
+
redirect: boolean | undefined;
|
|
79
|
+
};
|
|
80
|
+
error: null;
|
|
81
|
+
} | {
|
|
82
|
+
data: null;
|
|
83
|
+
error: {
|
|
84
|
+
code?: string | undefined | undefined;
|
|
85
|
+
message?: string | undefined | undefined;
|
|
86
|
+
status: number;
|
|
87
|
+
statusText: string;
|
|
88
|
+
};
|
|
89
|
+
}>, signUp: (input: FarmAuthSignUpInput) => Promise<{
|
|
90
|
+
data: (Omit<{
|
|
91
|
+
token: null;
|
|
92
|
+
user: {
|
|
93
|
+
id: string;
|
|
94
|
+
createdAt: Date;
|
|
95
|
+
updatedAt: Date;
|
|
96
|
+
email: string;
|
|
97
|
+
emailVerified: boolean;
|
|
98
|
+
name: string;
|
|
99
|
+
image?: string | null | undefined | undefined;
|
|
100
|
+
};
|
|
101
|
+
}, "user"> & {
|
|
102
|
+
user: import("better-auth").StripEmptyObjects<{
|
|
103
|
+
id: string;
|
|
104
|
+
createdAt: Date;
|
|
105
|
+
updatedAt: Date;
|
|
106
|
+
email: string;
|
|
107
|
+
emailVerified: boolean;
|
|
108
|
+
name: string;
|
|
109
|
+
image?: string | null | undefined;
|
|
110
|
+
}>;
|
|
111
|
+
}) | (Omit<{
|
|
112
|
+
token: string;
|
|
113
|
+
user: {
|
|
114
|
+
id: string;
|
|
115
|
+
createdAt: Date;
|
|
116
|
+
updatedAt: Date;
|
|
117
|
+
email: string;
|
|
118
|
+
emailVerified: boolean;
|
|
119
|
+
name: string;
|
|
120
|
+
image?: string | null | undefined | undefined;
|
|
121
|
+
};
|
|
122
|
+
}, "user"> & {
|
|
123
|
+
user: import("better-auth").StripEmptyObjects<{
|
|
124
|
+
id: string;
|
|
125
|
+
createdAt: Date;
|
|
126
|
+
updatedAt: Date;
|
|
127
|
+
email: string;
|
|
128
|
+
emailVerified: boolean;
|
|
129
|
+
name: string;
|
|
130
|
+
image?: string | null | undefined;
|
|
131
|
+
}>;
|
|
132
|
+
});
|
|
133
|
+
error: null;
|
|
134
|
+
} | {
|
|
135
|
+
data: null;
|
|
136
|
+
error: {
|
|
137
|
+
code?: string | undefined | undefined;
|
|
138
|
+
message?: string | undefined | undefined;
|
|
139
|
+
status: number;
|
|
140
|
+
statusText: string;
|
|
141
|
+
};
|
|
142
|
+
}>, useAuth: () => {
|
|
143
|
+
user: import("better-auth").StripEmptyObjects<{
|
|
144
|
+
id: string;
|
|
145
|
+
createdAt: Date;
|
|
146
|
+
updatedAt: Date;
|
|
147
|
+
email: string;
|
|
148
|
+
emailVerified: boolean;
|
|
149
|
+
name: string;
|
|
150
|
+
image?: string | null | undefined;
|
|
151
|
+
}> | null;
|
|
152
|
+
session: import("better-auth").StripEmptyObjects<{
|
|
153
|
+
id: string;
|
|
154
|
+
createdAt: Date;
|
|
155
|
+
updatedAt: Date;
|
|
156
|
+
userId: string;
|
|
157
|
+
expiresAt: Date;
|
|
158
|
+
token: string;
|
|
159
|
+
ipAddress?: string | null | undefined;
|
|
160
|
+
userAgent?: string | null | undefined;
|
|
161
|
+
}> | null;
|
|
162
|
+
isPending: boolean;
|
|
163
|
+
isRefetching: boolean;
|
|
164
|
+
error: import("better-auth/react").BetterFetchError | null;
|
|
165
|
+
refetch: (queryParams?: {
|
|
166
|
+
query?: import("better-auth").SessionQueryParams;
|
|
167
|
+
} | undefined) => Promise<void>;
|
|
168
|
+
signIn: (input: FarmAuthCredentials) => Promise<{
|
|
169
|
+
data: Omit<{
|
|
170
|
+
redirect: boolean;
|
|
171
|
+
token: string;
|
|
172
|
+
url?: string | undefined;
|
|
173
|
+
user: {
|
|
174
|
+
id: string;
|
|
175
|
+
createdAt: Date;
|
|
176
|
+
updatedAt: Date;
|
|
177
|
+
email: string;
|
|
178
|
+
emailVerified: boolean;
|
|
179
|
+
name: string;
|
|
180
|
+
image?: string | null | undefined | undefined;
|
|
181
|
+
};
|
|
182
|
+
}, "user"> & {
|
|
183
|
+
user: import("better-auth").StripEmptyObjects<{
|
|
184
|
+
id: string;
|
|
185
|
+
createdAt: Date;
|
|
186
|
+
updatedAt: Date;
|
|
187
|
+
email: string;
|
|
188
|
+
emailVerified: boolean;
|
|
189
|
+
name: string;
|
|
190
|
+
image?: string | null | undefined;
|
|
191
|
+
}>;
|
|
192
|
+
};
|
|
193
|
+
error: null;
|
|
194
|
+
} | {
|
|
195
|
+
data: null;
|
|
196
|
+
error: {
|
|
197
|
+
code?: string | undefined | undefined;
|
|
198
|
+
message?: string | undefined | undefined;
|
|
199
|
+
status: number;
|
|
200
|
+
statusText: string;
|
|
201
|
+
};
|
|
202
|
+
}>;
|
|
203
|
+
signUp: (input: FarmAuthSignUpInput) => Promise<{
|
|
204
|
+
data: (Omit<{
|
|
205
|
+
token: null;
|
|
206
|
+
user: {
|
|
207
|
+
id: string;
|
|
208
|
+
createdAt: Date;
|
|
209
|
+
updatedAt: Date;
|
|
210
|
+
email: string;
|
|
211
|
+
emailVerified: boolean;
|
|
212
|
+
name: string;
|
|
213
|
+
image?: string | null | undefined | undefined;
|
|
214
|
+
};
|
|
215
|
+
}, "user"> & {
|
|
216
|
+
user: import("better-auth").StripEmptyObjects<{
|
|
217
|
+
id: string;
|
|
218
|
+
createdAt: Date;
|
|
219
|
+
updatedAt: Date;
|
|
220
|
+
email: string;
|
|
221
|
+
emailVerified: boolean;
|
|
222
|
+
name: string;
|
|
223
|
+
image?: string | null | undefined;
|
|
224
|
+
}>;
|
|
225
|
+
}) | (Omit<{
|
|
226
|
+
token: string;
|
|
227
|
+
user: {
|
|
228
|
+
id: string;
|
|
229
|
+
createdAt: Date;
|
|
230
|
+
updatedAt: Date;
|
|
231
|
+
email: string;
|
|
232
|
+
emailVerified: boolean;
|
|
233
|
+
name: string;
|
|
234
|
+
image?: string | null | undefined | undefined;
|
|
235
|
+
};
|
|
236
|
+
}, "user"> & {
|
|
237
|
+
user: import("better-auth").StripEmptyObjects<{
|
|
238
|
+
id: string;
|
|
239
|
+
createdAt: Date;
|
|
240
|
+
updatedAt: Date;
|
|
241
|
+
email: string;
|
|
242
|
+
emailVerified: boolean;
|
|
243
|
+
name: string;
|
|
244
|
+
image?: string | null | undefined;
|
|
245
|
+
}>;
|
|
246
|
+
});
|
|
247
|
+
error: null;
|
|
248
|
+
} | {
|
|
249
|
+
data: null;
|
|
250
|
+
error: {
|
|
251
|
+
code?: string | undefined | undefined;
|
|
252
|
+
message?: string | undefined | undefined;
|
|
253
|
+
status: number;
|
|
254
|
+
statusText: string;
|
|
255
|
+
};
|
|
256
|
+
}>;
|
|
257
|
+
signOut: () => Promise<{
|
|
258
|
+
data: {
|
|
259
|
+
success: boolean;
|
|
260
|
+
url: string | undefined;
|
|
261
|
+
redirect: boolean | undefined;
|
|
262
|
+
};
|
|
263
|
+
error: null;
|
|
264
|
+
} | {
|
|
265
|
+
data: null;
|
|
266
|
+
error: {
|
|
267
|
+
code?: string | undefined | undefined;
|
|
268
|
+
message?: string | undefined | undefined;
|
|
269
|
+
status: number;
|
|
270
|
+
statusText: string;
|
|
271
|
+
};
|
|
272
|
+
}>;
|
|
273
|
+
};
|
|
274
|
+
export type { FarmAuthClientOptions, FarmAuthCredentials, FarmAuthSignUpInput } from "./types.js";
|
|
275
|
+
//# sourceMappingURL=client.native.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.native.d.ts","sourceRoot":"","sources":["../src/client.native.ts"],"names":[],"mappings":"AAYA,OAAO,KAAK,KAAK,GAAG,MAAM,aAAa,CAAC;AACxC,OAAO,KAAK,EAAyB,mBAAmB,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAC;AAgDlG;;;;GAIG;AACH,eAAO,MAAM,oBAAoB,EAAE,OAAO,GAAG,CAAC,oBA4J7C,CAAC;AAIF,eAAO,MAAQ,UAAU;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IAAE,MAAM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IAAE,OAAO;;;;;;;;;;;;;;;IAAE,MAAM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IAAE,OAAO;;;;;;;;;;;;;;;;;;;;;;;;aA7Ld,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA6L+B,CAAC;AAE9E,YAAY,EAAE,qBAAqB,EAAE,mBAAmB,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAC"}
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@farm.js/auth/client` for React Native screens (`page.native.tsx`). Metro resolves this file
|
|
3
|
+
* through the `react-native` export condition; web builds never see it, so web bundles do not
|
|
4
|
+
* change. It has the web client's exports and types, and calls the same auth routes through
|
|
5
|
+
* `serverFetch` from `@farm.js/native`, which sends the device's session token. Sign-in and
|
|
6
|
+
* sign-up go through the shell's native code, which keeps the session cookie in the WebView and
|
|
7
|
+
* issues the device token from it.
|
|
8
|
+
*/
|
|
9
|
+
import { useSyncExternalStore } from "react";
|
|
10
|
+
import { serverFetch, signOut as forgetDevice } from "@farm.js/native";
|
|
11
|
+
import { shellSignIn } from "@farm.js/native/internal";
|
|
12
|
+
import { normalizeBasePath } from "./config.js";
|
|
13
|
+
const SOCIAL_SIGN_IN_UNSUPPORTED = "@farm.js/auth/client: social and OAuth sign-in need a browser session and are not supported " +
|
|
14
|
+
"on native screens yet. Sign in with email and password, or on a web screen.";
|
|
15
|
+
// The ISO timestamps Better Auth's client turns into Dates, so `createdAt` and `expiresAt` are
|
|
16
|
+
// Dates here as on the web.
|
|
17
|
+
const ISO_DATE = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{1,7})?(?:Z|[+-]\d{2}:\d{2})$/;
|
|
18
|
+
function parseBody(text) {
|
|
19
|
+
if (!text)
|
|
20
|
+
return null;
|
|
21
|
+
try {
|
|
22
|
+
return JSON.parse(text, (key, value) => {
|
|
23
|
+
if (key === "__proto__")
|
|
24
|
+
return undefined;
|
|
25
|
+
if (typeof value === "string" && ISO_DATE.test(value)) {
|
|
26
|
+
const date = new Date(value);
|
|
27
|
+
return Number.isNaN(date.getTime()) ? value : date;
|
|
28
|
+
}
|
|
29
|
+
return value;
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
return text;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
function isRecord(value) {
|
|
37
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Same options as the web client. `basePath` must match `auth.basePath` and, like every
|
|
41
|
+
* `serverFetch` path, sits under the app's base path on `mobile.server`. `baseURL` does not apply:
|
|
42
|
+
* native screens always call `mobile.server`, the only origin the device token is sent to.
|
|
43
|
+
*/
|
|
44
|
+
export const createFarmAuthClient = (options = {}) => {
|
|
45
|
+
const basePath = normalizeBasePath(options.basePath);
|
|
46
|
+
// The status-and-body result Better Auth's client returns.
|
|
47
|
+
function toResult(status, statusText, text) {
|
|
48
|
+
const body = parseBody(text);
|
|
49
|
+
if (status >= 200 && status < 300)
|
|
50
|
+
return { data: (body ?? null), error: null };
|
|
51
|
+
return {
|
|
52
|
+
data: null,
|
|
53
|
+
error: { ...(isRecord(body) ? body : {}), status, statusText },
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
// From `serverFetch`. Async, so a screen that is not rendered yet (no `mobile.server`) rejects
|
|
57
|
+
// instead of throwing.
|
|
58
|
+
async function request(route, init) {
|
|
59
|
+
const response = await serverFetch(`${basePath}${route}`, init);
|
|
60
|
+
return toResult(response.status, response.statusText, await response.text());
|
|
61
|
+
}
|
|
62
|
+
async function loadSession(query) {
|
|
63
|
+
const search = Object.entries(query ?? {})
|
|
64
|
+
.filter(([, value]) => value !== undefined)
|
|
65
|
+
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`)
|
|
66
|
+
.join("&");
|
|
67
|
+
const result = await request(`/get-session${search ? `?${search}` : ""}`, {
|
|
68
|
+
method: "GET",
|
|
69
|
+
});
|
|
70
|
+
// A 401 here means the device has no valid token: signed out, as the web client reports it.
|
|
71
|
+
if (result.error?.status === 401)
|
|
72
|
+
return { data: null, error: null };
|
|
73
|
+
return result;
|
|
74
|
+
}
|
|
75
|
+
const signedOut = {
|
|
76
|
+
data: null,
|
|
77
|
+
error: null,
|
|
78
|
+
isPending: false,
|
|
79
|
+
isRefetching: false,
|
|
80
|
+
};
|
|
81
|
+
let state = { ...signedOut, isPending: true };
|
|
82
|
+
let latest = 0;
|
|
83
|
+
const listeners = new Set();
|
|
84
|
+
function setState(next) {
|
|
85
|
+
state = next;
|
|
86
|
+
for (const listener of listeners)
|
|
87
|
+
listener();
|
|
88
|
+
}
|
|
89
|
+
// Better Auth's useSession states, in the same order: pending until the first answer, a 401
|
|
90
|
+
// clears the user, a thrown error keeps the last one.
|
|
91
|
+
async function refetch(queryParams) {
|
|
92
|
+
const id = ++latest;
|
|
93
|
+
setState({ ...state, isPending: state.data === null, error: null, isRefetching: true });
|
|
94
|
+
try {
|
|
95
|
+
const result = await loadSession(queryParams?.query);
|
|
96
|
+
if (id !== latest)
|
|
97
|
+
return;
|
|
98
|
+
setState(result.error
|
|
99
|
+
? { ...signedOut, data: state.data, error: result.error }
|
|
100
|
+
: { ...signedOut, data: result.data });
|
|
101
|
+
}
|
|
102
|
+
catch (error) {
|
|
103
|
+
if (id !== latest)
|
|
104
|
+
return;
|
|
105
|
+
setState({ ...signedOut, data: state.data, error: error });
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
function subscribe(listener) {
|
|
109
|
+
listeners.add(listener);
|
|
110
|
+
// The first mounted hook loads the session, after React has subscribed.
|
|
111
|
+
if (listeners.size === 1)
|
|
112
|
+
void Promise.resolve().then(() => refetch());
|
|
113
|
+
return () => {
|
|
114
|
+
listeners.delete(listener);
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
const getState = () => state;
|
|
118
|
+
/**
|
|
119
|
+
* The web client's route and body, sent by the shell's native code. On success the WebView has
|
|
120
|
+
* the session cookie (web screens are signed in too), the device has its token, and `useAuth`
|
|
121
|
+
* has the user before this resolves. The shell never returns cookies or tokens to JS; the body
|
|
122
|
+
* is Better Auth's, as the web client gets it.
|
|
123
|
+
*/
|
|
124
|
+
async function credentials(route, input) {
|
|
125
|
+
if ("provider" in input)
|
|
126
|
+
throw new Error(SOCIAL_SIGN_IN_UNSUPPORTED);
|
|
127
|
+
const reply = await shellSignIn(`${basePath}${route}`, input);
|
|
128
|
+
// The shell has no reason phrase to pass on; HTTP/2 responses on the web have none either.
|
|
129
|
+
const result = toResult(reply.status, "", reply.body);
|
|
130
|
+
if (result.error === null)
|
|
131
|
+
await refetch();
|
|
132
|
+
return result;
|
|
133
|
+
}
|
|
134
|
+
function signIn(input) {
|
|
135
|
+
return credentials("/sign-in/email", input);
|
|
136
|
+
}
|
|
137
|
+
function signUp(input) {
|
|
138
|
+
return credentials("/sign-up/email", input);
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* End the server session, then forget this device: revoke its token, delete it from the
|
|
142
|
+
* Keystore or Keychain, and clear the WebView's cookies. The device step runs even when the
|
|
143
|
+
* server call fails or the phone is offline; the server's error is still returned or thrown.
|
|
144
|
+
*/
|
|
145
|
+
async function signOut() {
|
|
146
|
+
let result;
|
|
147
|
+
let failure;
|
|
148
|
+
let failed = false;
|
|
149
|
+
try {
|
|
150
|
+
result = await request("/sign-out", { method: "POST" });
|
|
151
|
+
}
|
|
152
|
+
catch (error) {
|
|
153
|
+
failed = true;
|
|
154
|
+
failure = error;
|
|
155
|
+
}
|
|
156
|
+
try {
|
|
157
|
+
await forgetDevice();
|
|
158
|
+
}
|
|
159
|
+
catch (error) {
|
|
160
|
+
if (!failed)
|
|
161
|
+
throw error;
|
|
162
|
+
}
|
|
163
|
+
finally {
|
|
164
|
+
latest += 1;
|
|
165
|
+
setState(signedOut);
|
|
166
|
+
}
|
|
167
|
+
if (failed)
|
|
168
|
+
throw failure;
|
|
169
|
+
return result;
|
|
170
|
+
}
|
|
171
|
+
function getSession() {
|
|
172
|
+
return loadSession();
|
|
173
|
+
}
|
|
174
|
+
function useAuth() {
|
|
175
|
+
const current = useSyncExternalStore(subscribe, getState, getState);
|
|
176
|
+
return {
|
|
177
|
+
user: current.data?.user ?? null,
|
|
178
|
+
session: current.data?.session ?? null,
|
|
179
|
+
isPending: current.isPending,
|
|
180
|
+
isRefetching: current.isRefetching,
|
|
181
|
+
error: current.error,
|
|
182
|
+
refetch,
|
|
183
|
+
signIn,
|
|
184
|
+
signUp,
|
|
185
|
+
signOut,
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
return { signIn, signUp, signOut, getSession, useAuth };
|
|
189
|
+
};
|
|
190
|
+
const defaultClient = createFarmAuthClient();
|
|
191
|
+
export const { getSession, signIn, signOut, signUp, useAuth } = defaultClient;
|
package/dist/config.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
1
|
import type { FarmAuthUserConfig, ResolvedFarmAuthConfig } from "./types.js";
|
|
2
2
|
export declare function resolveFarmAuthConfig(input: FarmAuthUserConfig | undefined): ResolvedFarmAuthConfig;
|
|
3
|
+
/** `auth.basePath` as the server mounts it. Shared with the native client. */
|
|
4
|
+
export declare function normalizeBasePath(value: string | undefined): string;
|
|
3
5
|
//# sourceMappingURL=config.d.ts.map
|
package/dist/config.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAC;AAK7E,wBAAgB,qBAAqB,CACnC,KAAK,EAAE,kBAAkB,GAAG,SAAS,GACpC,sBAAsB,CAiDxB"}
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAC;AAK7E,wBAAgB,qBAAqB,CACnC,KAAK,EAAE,kBAAkB,GAAG,SAAS,GACpC,sBAAsB,CAiDxB;AAED,8EAA8E;AAC9E,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,GAAG,MAAM,CAOnE"}
|
package/dist/config.js
CHANGED
|
@@ -47,7 +47,8 @@ export function resolveFarmAuthConfig(input) {
|
|
|
47
47
|
validateFarmAuthConfig(resolved);
|
|
48
48
|
return resolved;
|
|
49
49
|
}
|
|
50
|
-
|
|
50
|
+
/** `auth.basePath` as the server mounts it. Shared with the native client. */
|
|
51
|
+
export function normalizeBasePath(value) {
|
|
51
52
|
const route = (value || "/api/auth").trim();
|
|
52
53
|
if (!route)
|
|
53
54
|
return "/api/auth";
|
package/dist/internal.d.ts
CHANGED
|
@@ -15,6 +15,12 @@ interface FarmAuthIntegration {
|
|
|
15
15
|
methods: readonly ["GET", "POST"];
|
|
16
16
|
handler(request: Request): Promise<Response>;
|
|
17
17
|
}>;
|
|
18
|
+
session(request: Request): Promise<FarmAuthIntegrationSession | null>;
|
|
19
|
+
}
|
|
20
|
+
interface FarmAuthIntegrationSession {
|
|
21
|
+
id: string;
|
|
22
|
+
userId: string;
|
|
23
|
+
expiresAt: Date;
|
|
18
24
|
}
|
|
19
25
|
export declare function createFarmAuthIntegration(config: ResolvedFarmAuthConfig, options: FarmAuthIntegrationOptions): FarmAuthIntegration;
|
|
20
26
|
export declare function migrateFarmAuth(): Promise<void>;
|
package/dist/internal.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"internal.d.ts","sourceRoot":"","sources":["../src/internal.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"internal.d.ts","sourceRoot":"","sources":["../src/internal.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAC;AAUzD,UAAU,0BAA0B;IAClC,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,aAAa,GAAG,YAAY,CAAC;CACpC;AAED,UAAU,mBAAmB;IAC3B,QAAQ,CAAC,IAAI,EAAE,kBAAkB,CAAC;IAClC,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,WAAW,CAAC;IAClB,QAAQ,EAAE;QAAE,QAAQ,CAAC,IAAI,EAAE,mBAAmB,CAAA;KAAE,CAAC;IACjD,MAAM,EAAE,KAAK,CAAC;QACZ,IAAI,EAAE,MAAM,CAAC;QACb,OAAO,EAAE,SAAS,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;QAClC,OAAO,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC;KAC9C,CAAC,CAAC;IACH,OAAO,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,0BAA0B,GAAG,IAAI,CAAC,CAAC;CACvE;AAED,UAAU,0BAA0B;IAClC,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,IAAI,CAAC;CACjB;AAED,wBAAgB,yBAAyB,CACvC,MAAM,EAAE,sBAAsB,EAC9B,OAAO,EAAE,0BAA0B,GAClC,mBAAmB,CAiCrB;AAED,wBAAsB,eAAe,IAAI,OAAO,CAAC,IAAI,CAAC,CAErD;AAED,wBAAsB,wBAAwB,IAAI,OAAO,CAAC,MAAM,CAAC,CAEhE;AAED,wBAAsB,eAAe,IAAI,OAAO,CAAC,IAAI,CAAC,CAErD"}
|
package/dist/internal.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { configureFarmAuth, disposeFarmAuth as runDispose, getFarmAuthRuntime, migrateFarmAuth as runMigration, compileFarmAuthMigration as runCompileMigration, } from "./runtime.js";
|
|
1
|
+
import { configureFarmAuth, disposeFarmAuth as runDispose, getFarmAuthRuntime, migrateFarmAuth as runMigration, withFarmAuthClientAddress, compileFarmAuthMigration as runCompileMigration, } from "./runtime.js";
|
|
2
2
|
export function createFarmAuthIntegration(config, options) {
|
|
3
3
|
configureFarmAuth(config, options);
|
|
4
4
|
return {
|
|
@@ -12,10 +12,23 @@ export function createFarmAuthIntegration(config, options) {
|
|
|
12
12
|
methods: ["GET", "POST"],
|
|
13
13
|
async handler(request) {
|
|
14
14
|
const runtime = await getFarmAuthRuntime();
|
|
15
|
-
return runtime.handler(request);
|
|
15
|
+
return runtime.handler(withFarmAuthClientAddress(request));
|
|
16
16
|
},
|
|
17
17
|
},
|
|
18
18
|
],
|
|
19
|
+
// Farm's session contract: who the request's cookies sign in. Mobile session tokens use it.
|
|
20
|
+
async session(request) {
|
|
21
|
+
const runtime = await getFarmAuthRuntime();
|
|
22
|
+
const result = (await runtime.api.getSession({ headers: request.headers }));
|
|
23
|
+
const session = result?.session;
|
|
24
|
+
if (!session || typeof session.id !== "string")
|
|
25
|
+
return null;
|
|
26
|
+
return {
|
|
27
|
+
id: session.id,
|
|
28
|
+
userId: String(session.userId),
|
|
29
|
+
expiresAt: new Date(session.expiresAt),
|
|
30
|
+
};
|
|
31
|
+
},
|
|
19
32
|
};
|
|
20
33
|
}
|
|
21
34
|
export async function migrateFarmAuth() {
|
package/dist/runtime.d.ts
CHANGED
|
@@ -13,6 +13,13 @@ interface BetterAuthRuntime {
|
|
|
13
13
|
}
|
|
14
14
|
export declare function configureFarmAuth(config: ResolvedFarmAuthConfig, options: FarmAuthRuntimeOptions): void;
|
|
15
15
|
export declare function getFarmAuthRuntime(): Promise<BetterAuthRuntime>;
|
|
16
|
+
/**
|
|
17
|
+
* The request Better Auth's handler sees, carrying the client address Farm
|
|
18
|
+
* resolved. Better Auth keys its sign-in rate limit on that address; left to
|
|
19
|
+
* its default it reads a client-supplied single-value x-forwarded-for, so a
|
|
20
|
+
* rotating header bypassed the limit and requests without one shared a bucket.
|
|
21
|
+
*/
|
|
22
|
+
export declare function withFarmAuthClientAddress(request: Request): Request;
|
|
16
23
|
export declare function getFarmAuthRequest(request?: Request): Request;
|
|
17
24
|
/**
|
|
18
25
|
* Close the connection opened for the runtime and drop the cached instance.
|
|
@@ -47,7 +54,11 @@ export declare function resolveFarmAuthBaseURL(mode: "development" | "production
|
|
|
47
54
|
* A production deployment is still reachable at its own deployment hostname,
|
|
48
55
|
* and previews get a fresh one per build, so both are trusted explicitly
|
|
49
56
|
* rather than leaving direct deployment-URL access broken.
|
|
57
|
+
*
|
|
58
|
+
* `farm dev` without a configured URL also trusts the origin the dev server is
|
|
59
|
+
* serving the request on, so another port, an emulator's `10.0.2.2`, or a LAN
|
|
60
|
+
* address under `--host` can sign in. See `resolveDevServerOrigin`.
|
|
50
61
|
*/
|
|
51
|
-
export declare function resolveFarmAuthTrustedOrigins(env?: NodeJS.ProcessEnv): string[];
|
|
62
|
+
export declare function resolveFarmAuthTrustedOrigins(mode: "development" | "production", env?: NodeJS.ProcessEnv): string[] | ((request?: Request) => string[]);
|
|
52
63
|
export {};
|
|
53
64
|
//# sourceMappingURL=runtime.d.ts.map
|
package/dist/runtime.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../src/runtime.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../src/runtime.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAC;AAazD,UAAU,sBAAsB;IAC9B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,aAAa,GAAG,YAAY,CAAC;CACpC;AAED,UAAU,iBAAiB;IACzB,OAAO,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC;IAC7C,GAAG,EAAE;QACH,UAAU,CAAC,KAAK,EAAE;YAAE,OAAO,EAAE,OAAO,CAAA;SAAE,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;KAC3D,CAAC;CACH;AAqBD,wBAAgB,iBAAiB,CAC/B,MAAM,EAAE,sBAAsB,EAC9B,OAAO,EAAE,sBAAsB,GAC9B,IAAI,CAeN;AAED,wBAAsB,kBAAkB,IAAI,OAAO,CAAC,iBAAiB,CAAC,CA6BrE;AAED;;;;;GAKG;AACH,wBAAgB,yBAAyB,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CASnE;AAED,wBAAgB,kBAAkB,CAAC,OAAO,CAAC,EAAE,OAAO,GAAG,OAAO,CAU7D;AAED;;;;;;GAMG;AACH,wBAAsB,eAAe,IAAI,OAAO,CAAC,IAAI,CAAC,CASrD;AAED,wBAAsB,eAAe,IAAI,OAAO,CAAC,IAAI,CAAC,CAIrD;AAED;;;;GAIG;AACH,wBAAsB,wBAAwB,IAAI,OAAO,CAAC,MAAM,CAAC,CAEhE;AAsID;;;;;;;;;;GAUG;AACH,wBAAgB,sBAAsB,CACpC,IAAI,EAAE,aAAa,GAAG,YAAY,EAClC,GAAG,GAAE,MAAM,CAAC,UAAwB,GACnC,MAAM,GAAG,SAAS,CAIpB;AAaD;;;;;;;;;;GAUG;AACH,wBAAgB,6BAA6B,CAC3C,IAAI,EAAE,aAAa,GAAG,YAAY,EAClC,GAAG,GAAE,MAAM,CAAC,UAAwB,GACnC,MAAM,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,EAAE,OAAO,KAAK,MAAM,EAAE,CAAC,CAa9C"}
|
package/dist/runtime.js
CHANGED
|
@@ -1,9 +1,17 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
|
+
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
2
3
|
import { createRequire } from "node:module";
|
|
4
|
+
import { isIP } from "node:net";
|
|
3
5
|
import path from "node:path";
|
|
4
6
|
const FARM_AUTH_STATE_KEY = Symbol.for("@farm.js/auth/runtime");
|
|
5
7
|
const CURRENT_REQUEST_RESOLVER_KEY = Symbol.for("farm.currentRequestResolver");
|
|
6
8
|
const loadNodeModule = createRequire(import.meta.url);
|
|
9
|
+
/**
|
|
10
|
+
* Where Better Auth reads the caller's address from. Farm Auth sets it from the
|
|
11
|
+
* address Farm resolved for the request (the server's peer address, or the
|
|
12
|
+
* forwarded one under `server.trustProxy`) and drops any copy the client sent.
|
|
13
|
+
*/
|
|
14
|
+
const CLIENT_ADDRESS_HEADER = "x-farm-auth-client-address";
|
|
7
15
|
function getState() {
|
|
8
16
|
const runtime = globalThis;
|
|
9
17
|
runtime[FARM_AUTH_STATE_KEY] ||= {};
|
|
@@ -53,6 +61,23 @@ export async function getFarmAuthRuntime() {
|
|
|
53
61
|
}
|
|
54
62
|
return state.runtime;
|
|
55
63
|
}
|
|
64
|
+
/**
|
|
65
|
+
* The request Better Auth's handler sees, carrying the client address Farm
|
|
66
|
+
* resolved. Better Auth keys its sign-in rate limit on that address; left to
|
|
67
|
+
* its default it reads a client-supplied single-value x-forwarded-for, so a
|
|
68
|
+
* rotating header bypassed the limit and requests without one shared a bucket.
|
|
69
|
+
*/
|
|
70
|
+
export function withFarmAuthClientAddress(request) {
|
|
71
|
+
const address = request.socket
|
|
72
|
+
?.remoteAddress;
|
|
73
|
+
if (!address && !request.headers.has(CLIENT_ADDRESS_HEADER))
|
|
74
|
+
return request;
|
|
75
|
+
const headers = new Headers(request.headers);
|
|
76
|
+
headers.delete(CLIENT_ADDRESS_HEADER);
|
|
77
|
+
if (address)
|
|
78
|
+
headers.set(CLIENT_ADDRESS_HEADER, address);
|
|
79
|
+
return new Request(request, { headers });
|
|
80
|
+
}
|
|
56
81
|
export function getFarmAuthRequest(request) {
|
|
57
82
|
if (request)
|
|
58
83
|
return request;
|
|
@@ -128,9 +153,9 @@ async function createBetterAuthOptions(config, options, runtimeOptions = {}) {
|
|
|
128
153
|
return {
|
|
129
154
|
appName: config.appName || "Farm app",
|
|
130
155
|
baseURL: resolveFarmAuthBaseURL(options.mode),
|
|
131
|
-
trustedOrigins: resolveFarmAuthTrustedOrigins(),
|
|
156
|
+
trustedOrigins: resolveFarmAuthTrustedOrigins(options.mode),
|
|
132
157
|
basePath: config.basePath,
|
|
133
|
-
secret: secret ||
|
|
158
|
+
secret: secret || (await resolveFallbackSecret(options)),
|
|
134
159
|
database: await createDatabase(config, options),
|
|
135
160
|
emailAndPassword: {
|
|
136
161
|
enabled: config.emailAndPassword.enabled,
|
|
@@ -142,8 +167,62 @@ async function createBetterAuthOptions(config, options, runtimeOptions = {}) {
|
|
|
142
167
|
expiresIn: config.session.expiresIn,
|
|
143
168
|
updateAge: config.session.updateAge,
|
|
144
169
|
},
|
|
170
|
+
advanced: {
|
|
171
|
+
ipAddress: { ipAddressHeaders: [CLIENT_ADDRESS_HEADER] },
|
|
172
|
+
},
|
|
145
173
|
};
|
|
146
174
|
}
|
|
175
|
+
/** Random secrets used when a project directory cannot hold one, kept for the process. */
|
|
176
|
+
const ephemeralSecrets = new Map();
|
|
177
|
+
/**
|
|
178
|
+
* The secret used when no secret env is set: development or a migration, which never signs.
|
|
179
|
+
*
|
|
180
|
+
* A fixed fallback would let anyone who reaches a dev server (a `farm preview` URL, `--host`) forge
|
|
181
|
+
* Better Auth's signed tokens, such as email verification links. Development instead generates a
|
|
182
|
+
* random secret once per project in `.farm/auth-secret`, next to the local database, so sessions
|
|
183
|
+
* survive restarts. A production migration gets a throwaway one and writes nothing.
|
|
184
|
+
*/
|
|
185
|
+
async function resolveFallbackSecret(options) {
|
|
186
|
+
const file = path.resolve(options.root, ".farm", "auth-secret");
|
|
187
|
+
if (options.mode !== "development")
|
|
188
|
+
return getEphemeralSecret(file);
|
|
189
|
+
const existing = await readSecretFile(file);
|
|
190
|
+
if (existing)
|
|
191
|
+
return existing;
|
|
192
|
+
const generated = randomBytes(32).toString("hex");
|
|
193
|
+
try {
|
|
194
|
+
await mkdir(path.dirname(file), { recursive: true });
|
|
195
|
+
// `wx` keeps a secret another process wrote first; that one is read back below.
|
|
196
|
+
await writeFile(file, `${generated}\n`, { flag: "wx", mode: 0o600 });
|
|
197
|
+
return generated;
|
|
198
|
+
}
|
|
199
|
+
catch (error) {
|
|
200
|
+
if (error.code !== "EEXIST")
|
|
201
|
+
return getEphemeralSecret(file);
|
|
202
|
+
}
|
|
203
|
+
// Another process won the race, or the file holds no usable secret.
|
|
204
|
+
const raced = await readSecretFile(file);
|
|
205
|
+
if (raced)
|
|
206
|
+
return raced;
|
|
207
|
+
return writeFile(file, `${generated}\n`, { mode: 0o600 }).then(() => generated, () => getEphemeralSecret(file));
|
|
208
|
+
}
|
|
209
|
+
async function readSecretFile(file) {
|
|
210
|
+
try {
|
|
211
|
+
const value = (await readFile(file, "utf8")).trim();
|
|
212
|
+
return value.length >= 32 ? value : undefined;
|
|
213
|
+
}
|
|
214
|
+
catch {
|
|
215
|
+
return undefined;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
function getEphemeralSecret(key) {
|
|
219
|
+
let secret = ephemeralSecrets.get(key);
|
|
220
|
+
if (!secret) {
|
|
221
|
+
secret = randomBytes(32).toString("hex");
|
|
222
|
+
ephemeralSecrets.set(key, secret);
|
|
223
|
+
}
|
|
224
|
+
return secret;
|
|
225
|
+
}
|
|
147
226
|
/**
|
|
148
227
|
* The origin Better Auth signs cookies and validates requests against.
|
|
149
228
|
*
|
|
@@ -156,15 +235,19 @@ async function createBetterAuthOptions(config, options, runtimeOptions = {}) {
|
|
|
156
235
|
* so production prefers the canonical one and previews keep their own URL.
|
|
157
236
|
*/
|
|
158
237
|
export function resolveFarmAuthBaseURL(mode, env = process.env) {
|
|
238
|
+
const configured = resolveConfiguredBaseURL(env);
|
|
239
|
+
if (configured)
|
|
240
|
+
return configured;
|
|
241
|
+
return mode === "development" ? "http://localhost:3000" : undefined;
|
|
242
|
+
}
|
|
243
|
+
function resolveConfiguredBaseURL(env) {
|
|
159
244
|
const configured = env.FARM_AUTH_URL || env.BETTER_AUTH_URL;
|
|
160
245
|
if (configured)
|
|
161
246
|
return configured;
|
|
162
247
|
const vercelHost = env.VERCEL_ENV === "production"
|
|
163
248
|
? env.VERCEL_PROJECT_PRODUCTION_URL || env.VERCEL_URL
|
|
164
249
|
: env.VERCEL_URL;
|
|
165
|
-
|
|
166
|
-
return toAbsoluteOrigin(vercelHost);
|
|
167
|
-
return mode === "development" ? "http://localhost:3000" : undefined;
|
|
250
|
+
return vercelHost ? toAbsoluteOrigin(vercelHost) : undefined;
|
|
168
251
|
}
|
|
169
252
|
/**
|
|
170
253
|
* Hosts that stay valid alongside the base URL.
|
|
@@ -172,14 +255,52 @@ export function resolveFarmAuthBaseURL(mode, env = process.env) {
|
|
|
172
255
|
* A production deployment is still reachable at its own deployment hostname,
|
|
173
256
|
* and previews get a fresh one per build, so both are trusted explicitly
|
|
174
257
|
* rather than leaving direct deployment-URL access broken.
|
|
258
|
+
*
|
|
259
|
+
* `farm dev` without a configured URL also trusts the origin the dev server is
|
|
260
|
+
* serving the request on, so another port, an emulator's `10.0.2.2`, or a LAN
|
|
261
|
+
* address under `--host` can sign in. See `resolveDevServerOrigin`.
|
|
175
262
|
*/
|
|
176
|
-
export function resolveFarmAuthTrustedOrigins(env = process.env) {
|
|
263
|
+
export function resolveFarmAuthTrustedOrigins(mode, env = process.env) {
|
|
177
264
|
const origins = new Set();
|
|
178
265
|
for (const host of [env.VERCEL_URL, env.VERCEL_PROJECT_PRODUCTION_URL, env.VERCEL_BRANCH_URL]) {
|
|
179
266
|
if (host)
|
|
180
267
|
origins.add(toAbsoluteOrigin(host));
|
|
181
268
|
}
|
|
182
|
-
|
|
269
|
+
const trusted = [...origins];
|
|
270
|
+
// A configured URL stays authoritative, in development too.
|
|
271
|
+
if (mode !== "development" || resolveConfiguredBaseURL(env))
|
|
272
|
+
return trusted;
|
|
273
|
+
return (request) => {
|
|
274
|
+
const own = request ? resolveDevServerOrigin(request) : undefined;
|
|
275
|
+
return own ? [...trusted, own] : trusted;
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* The dev server's own origin for this request, when it is safe to trust.
|
|
280
|
+
*
|
|
281
|
+
* In development the request URL comes from the Host the dev server accepted,
|
|
282
|
+
* so a page served by the dev server sends that same origin. Only hosts that
|
|
283
|
+
* cannot be DNS-rebound are trusted: `localhost`, `*.localhost`, and IP
|
|
284
|
+
* literals, the hosts Vite allows without `server.allowedHosts`. A rebinding
|
|
285
|
+
* page on `evil.example` that resolves to the dev server sends its own Host and
|
|
286
|
+
* Origin, which match each other but are not trusted here. A cross-site page
|
|
287
|
+
* sends an Origin that is not this request's origin, so it stays rejected.
|
|
288
|
+
*/
|
|
289
|
+
function resolveDevServerOrigin(request) {
|
|
290
|
+
let url;
|
|
291
|
+
try {
|
|
292
|
+
url = new URL(request.url);
|
|
293
|
+
}
|
|
294
|
+
catch {
|
|
295
|
+
return undefined;
|
|
296
|
+
}
|
|
297
|
+
if (url.protocol !== "http:" && url.protocol !== "https:")
|
|
298
|
+
return undefined;
|
|
299
|
+
const hostname = url.hostname.toLowerCase();
|
|
300
|
+
const trusted = hostname === "localhost" ||
|
|
301
|
+
hostname.endsWith(".localhost") ||
|
|
302
|
+
isIP(hostname.replace(/^\[|\]$/g, "")) !== 0;
|
|
303
|
+
return trusted ? url.origin : undefined;
|
|
183
304
|
}
|
|
184
305
|
function toAbsoluteOrigin(host) {
|
|
185
306
|
return host.startsWith("http://") || host.startsWith("https://") ? host : `https://${host}`;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@farm.js/auth",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.63",
|
|
4
4
|
"description": "Farm-native authentication with email/password, server helpers, and React hooks",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -53,11 +53,11 @@
|
|
|
53
53
|
"typescript": "^5.3.3",
|
|
54
54
|
"vite": "8.3.4",
|
|
55
55
|
"vitest": "5.0.5",
|
|
56
|
-
"@farm.js/native": "0.1.0-beta.
|
|
56
|
+
"@farm.js/native": "0.1.0-beta.4"
|
|
57
57
|
},
|
|
58
58
|
"peerDependencies": {
|
|
59
59
|
"react": "^18.2.0 || ^19.0.0",
|
|
60
|
-
"@farm.js/native": "^0.1.0-beta.
|
|
60
|
+
"@farm.js/native": "^0.1.0-beta.4"
|
|
61
61
|
},
|
|
62
62
|
"peerDependenciesMeta": {
|
|
63
63
|
"@farm.js/native": {
|