@farm.js/auth 0.1.60 → 0.1.61
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/config.d.ts +0 -2
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +1 -2
- package/dist/internal.d.ts +0 -6
- package/dist/internal.d.ts.map +1 -1
- package/dist/internal.js +2 -15
- package/dist/runtime.d.ts +1 -12
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +8 -129
- package/package.json +3 -3
- package/dist/client.native.d.ts +0 -275
- package/dist/client.native.d.ts.map +0 -1
- package/dist/client.native.js +0 -191
package/dist/config.d.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
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;
|
|
5
3
|
//# 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"}
|
package/dist/config.js
CHANGED
|
@@ -47,8 +47,7 @@ export function resolveFarmAuthConfig(input) {
|
|
|
47
47
|
validateFarmAuthConfig(resolved);
|
|
48
48
|
return resolved;
|
|
49
49
|
}
|
|
50
|
-
|
|
51
|
-
export function normalizeBasePath(value) {
|
|
50
|
+
function normalizeBasePath(value) {
|
|
52
51
|
const route = (value || "/api/auth").trim();
|
|
53
52
|
if (!route)
|
|
54
53
|
return "/api/auth";
|
package/dist/internal.d.ts
CHANGED
|
@@ -15,12 +15,6 @@ 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;
|
|
24
18
|
}
|
|
25
19
|
export declare function createFarmAuthIntegration(config: ResolvedFarmAuthConfig, options: FarmAuthIntegrationOptions): FarmAuthIntegration;
|
|
26
20
|
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;AASzD,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;CACJ;AAED,wBAAgB,yBAAyB,CACvC,MAAM,EAAE,sBAAsB,EAC9B,OAAO,EAAE,0BAA0B,GAClC,mBAAmB,CAmBrB;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,
|
|
1
|
+
import { configureFarmAuth, disposeFarmAuth as runDispose, getFarmAuthRuntime, migrateFarmAuth as runMigration, compileFarmAuthMigration as runCompileMigration, } from "./runtime.js";
|
|
2
2
|
export function createFarmAuthIntegration(config, options) {
|
|
3
3
|
configureFarmAuth(config, options);
|
|
4
4
|
return {
|
|
@@ -12,23 +12,10 @@ 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(
|
|
15
|
+
return runtime.handler(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
|
-
},
|
|
32
19
|
};
|
|
33
20
|
}
|
|
34
21
|
export async function migrateFarmAuth() {
|
package/dist/runtime.d.ts
CHANGED
|
@@ -13,13 +13,6 @@ 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;
|
|
23
16
|
export declare function getFarmAuthRequest(request?: Request): Request;
|
|
24
17
|
/**
|
|
25
18
|
* Close the connection opened for the runtime and drop the cached instance.
|
|
@@ -54,11 +47,7 @@ export declare function resolveFarmAuthBaseURL(mode: "development" | "production
|
|
|
54
47
|
* A production deployment is still reachable at its own deployment hostname,
|
|
55
48
|
* and previews get a fresh one per build, so both are trusted explicitly
|
|
56
49
|
* 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`.
|
|
61
50
|
*/
|
|
62
|
-
export declare function resolveFarmAuthTrustedOrigins(
|
|
51
|
+
export declare function resolveFarmAuthTrustedOrigins(env?: NodeJS.ProcessEnv): string[];
|
|
63
52
|
export {};
|
|
64
53
|
//# 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":"AAGA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAC;AAMzD,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,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;AA4ED;;;;;;;;;;GAUG;AACH,wBAAgB,sBAAsB,CACpC,IAAI,EAAE,aAAa,GAAG,YAAY,EAClC,GAAG,GAAE,MAAM,CAAC,UAAwB,GACnC,MAAM,GAAG,SAAS,CAWpB;AAED;;;;;;GAMG;AACH,wBAAgB,6BAA6B,CAAC,GAAG,GAAE,MAAM,CAAC,UAAwB,GAAG,MAAM,EAAE,CAM5F"}
|
package/dist/runtime.js
CHANGED
|
@@ -1,17 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
1
|
+
import { mkdir } from "node:fs/promises";
|
|
3
2
|
import { createRequire } from "node:module";
|
|
4
|
-
import { isIP } from "node:net";
|
|
5
3
|
import path from "node:path";
|
|
6
4
|
const FARM_AUTH_STATE_KEY = Symbol.for("@farm.js/auth/runtime");
|
|
7
5
|
const CURRENT_REQUEST_RESOLVER_KEY = Symbol.for("farm.currentRequestResolver");
|
|
8
6
|
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";
|
|
15
7
|
function getState() {
|
|
16
8
|
const runtime = globalThis;
|
|
17
9
|
runtime[FARM_AUTH_STATE_KEY] ||= {};
|
|
@@ -61,23 +53,6 @@ export async function getFarmAuthRuntime() {
|
|
|
61
53
|
}
|
|
62
54
|
return state.runtime;
|
|
63
55
|
}
|
|
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
|
-
}
|
|
81
56
|
export function getFarmAuthRequest(request) {
|
|
82
57
|
if (request)
|
|
83
58
|
return request;
|
|
@@ -153,9 +128,9 @@ async function createBetterAuthOptions(config, options, runtimeOptions = {}) {
|
|
|
153
128
|
return {
|
|
154
129
|
appName: config.appName || "Farm app",
|
|
155
130
|
baseURL: resolveFarmAuthBaseURL(options.mode),
|
|
156
|
-
trustedOrigins: resolveFarmAuthTrustedOrigins(
|
|
131
|
+
trustedOrigins: resolveFarmAuthTrustedOrigins(),
|
|
157
132
|
basePath: config.basePath,
|
|
158
|
-
secret: secret ||
|
|
133
|
+
secret: secret || "farm-auth-development-or-migration-secret",
|
|
159
134
|
database: await createDatabase(config, options),
|
|
160
135
|
emailAndPassword: {
|
|
161
136
|
enabled: config.emailAndPassword.enabled,
|
|
@@ -167,62 +142,8 @@ async function createBetterAuthOptions(config, options, runtimeOptions = {}) {
|
|
|
167
142
|
expiresIn: config.session.expiresIn,
|
|
168
143
|
updateAge: config.session.updateAge,
|
|
169
144
|
},
|
|
170
|
-
advanced: {
|
|
171
|
-
ipAddress: { ipAddressHeaders: [CLIENT_ADDRESS_HEADER] },
|
|
172
|
-
},
|
|
173
145
|
};
|
|
174
146
|
}
|
|
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
|
-
}
|
|
226
147
|
/**
|
|
227
148
|
* The origin Better Auth signs cookies and validates requests against.
|
|
228
149
|
*
|
|
@@ -235,19 +156,15 @@ function getEphemeralSecret(key) {
|
|
|
235
156
|
* so production prefers the canonical one and previews keep their own URL.
|
|
236
157
|
*/
|
|
237
158
|
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) {
|
|
244
159
|
const configured = env.FARM_AUTH_URL || env.BETTER_AUTH_URL;
|
|
245
160
|
if (configured)
|
|
246
161
|
return configured;
|
|
247
162
|
const vercelHost = env.VERCEL_ENV === "production"
|
|
248
163
|
? env.VERCEL_PROJECT_PRODUCTION_URL || env.VERCEL_URL
|
|
249
164
|
: env.VERCEL_URL;
|
|
250
|
-
|
|
165
|
+
if (vercelHost)
|
|
166
|
+
return toAbsoluteOrigin(vercelHost);
|
|
167
|
+
return mode === "development" ? "http://localhost:3000" : undefined;
|
|
251
168
|
}
|
|
252
169
|
/**
|
|
253
170
|
* Hosts that stay valid alongside the base URL.
|
|
@@ -255,52 +172,14 @@ function resolveConfiguredBaseURL(env) {
|
|
|
255
172
|
* A production deployment is still reachable at its own deployment hostname,
|
|
256
173
|
* and previews get a fresh one per build, so both are trusted explicitly
|
|
257
174
|
* 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`.
|
|
262
175
|
*/
|
|
263
|
-
export function resolveFarmAuthTrustedOrigins(
|
|
176
|
+
export function resolveFarmAuthTrustedOrigins(env = process.env) {
|
|
264
177
|
const origins = new Set();
|
|
265
178
|
for (const host of [env.VERCEL_URL, env.VERCEL_PROJECT_PRODUCTION_URL, env.VERCEL_BRANCH_URL]) {
|
|
266
179
|
if (host)
|
|
267
180
|
origins.add(toAbsoluteOrigin(host));
|
|
268
181
|
}
|
|
269
|
-
|
|
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;
|
|
182
|
+
return [...origins];
|
|
304
183
|
}
|
|
305
184
|
function toAbsoluteOrigin(host) {
|
|
306
185
|
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.61",
|
|
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.3"
|
|
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.3"
|
|
61
61
|
},
|
|
62
62
|
"peerDependenciesMeta": {
|
|
63
63
|
"@farm.js/native": {
|
package/dist/client.native.d.ts
DELETED
|
@@ -1,275 +0,0 @@
|
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
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"}
|
package/dist/client.native.js
DELETED
|
@@ -1,191 +0,0 @@
|
|
|
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;
|