foxlogin 0.3.1 → 0.4.0
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/CHANGELOG.md +9 -0
- package/README.md +26 -0
- package/dist/client.d.ts +77 -11
- package/dist/client.js +78 -1
- package/dist/index.d.ts +1 -1
- package/dist/types.d.ts +2 -0
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.4.0 — 2026-12-03
|
|
4
|
+
|
|
5
|
+
- Google sign-in: `googleSignInUrl`, `signInWithGoogle` (keeps a random state),
|
|
6
|
+
`handleRedirect` (checks the state, exchanges `fox_code`, reports `country_required` or
|
|
7
|
+
`fox_error`) and `completeGoogleSignUp`. Works the same with Fox Login's Google app and with
|
|
8
|
+
an application's own Google app.
|
|
9
|
+
- `getAppConfig()` returns the application's sign-in methods and user fields;
|
|
10
|
+
`signUp` takes `profile` with the values of those fields. `User.profile` holds them.
|
|
11
|
+
|
|
3
12
|
## 0.3.1 — 2026-12-02
|
|
4
13
|
|
|
5
14
|
- `FoxLoginAdmin` accepts per-application secret keys (`sk_live_…`) as well as organization API
|
package/README.md
CHANGED
|
@@ -36,6 +36,32 @@ await fox.signOut();
|
|
|
36
36
|
Sessions live in memory by default. To keep users signed in across reloads, pass
|
|
37
37
|
`storage: localStorageStorage()` (or your own `SessionStorage`).
|
|
38
38
|
|
|
39
|
+
### Sign in with Google
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
// On your sign-in page (the redirect URI must be listed under Redirect URLs in the dashboard):
|
|
43
|
+
fox.signInWithGoogle({ redirectUri: "https://your.app/auth/callback" });
|
|
44
|
+
|
|
45
|
+
// On https://your.app/auth/callback:
|
|
46
|
+
const r = await fox.handleRedirect(); // null when the URL is not a Fox Login return
|
|
47
|
+
if (r?.status === "country_required") {
|
|
48
|
+
await fox.completeGoogleSignUp({ ticket: r.ticket, country: "TR" }); // ask the user first
|
|
49
|
+
} else if (r?.status === "mfa_required") {
|
|
50
|
+
await fox.verifyMfa({ mfaToken: r.mfaToken, code });
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Whether Google shows Fox Login's app or your own Google app is set in the dashboard; the code
|
|
55
|
+
is the same in both cases.
|
|
56
|
+
|
|
57
|
+
### Sign-up fields
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
const app = await fox.getAppConfig(); // sign-in methods and user fields of the application
|
|
61
|
+
// app.user_fields.fields.username.mode === "required" → show a required username input
|
|
62
|
+
await fox.signUp({ email, password, country: "TR", name, profile: { username: "ayse", plan: "pro" } });
|
|
63
|
+
```
|
|
64
|
+
|
|
39
65
|
## On your server: check tokens and manage users
|
|
40
66
|
|
|
41
67
|
```ts
|
package/dist/client.d.ts
CHANGED
|
@@ -25,16 +25,39 @@ export type AuthState = {
|
|
|
25
25
|
};
|
|
26
26
|
/** `pk_live_eu_…` → `eu`. */
|
|
27
27
|
export declare function regionFromPublishableKey(key: string): string;
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
28
|
+
export interface FieldRule {
|
|
29
|
+
mode: "off" | "optional" | "required";
|
|
30
|
+
editable: boolean;
|
|
31
|
+
}
|
|
32
|
+
export interface AppConfig {
|
|
33
|
+
id: string;
|
|
34
|
+
name: string;
|
|
35
|
+
region: string;
|
|
36
|
+
sign_in: {
|
|
37
|
+
password: boolean;
|
|
38
|
+
email_code: boolean;
|
|
39
|
+
};
|
|
40
|
+
providers: {
|
|
41
|
+
google: {
|
|
42
|
+
enabled: boolean;
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
user_fields: {
|
|
46
|
+
fields: Record<string, FieldRule>;
|
|
47
|
+
custom: {
|
|
48
|
+
key: string;
|
|
49
|
+
label: string;
|
|
50
|
+
type: "text" | "number" | "date" | "boolean" | "select" | "url";
|
|
51
|
+
mode: FieldRule["mode"];
|
|
52
|
+
editable: boolean;
|
|
53
|
+
options?: string[];
|
|
54
|
+
}[];
|
|
55
|
+
};
|
|
56
|
+
country_restriction: {
|
|
57
|
+
mode: "all" | "only_listed";
|
|
58
|
+
countries: string[];
|
|
59
|
+
};
|
|
60
|
+
}
|
|
38
61
|
export declare class FoxLogin {
|
|
39
62
|
readonly baseUrl: string;
|
|
40
63
|
private readonly key;
|
|
@@ -59,12 +82,21 @@ export declare class FoxLogin {
|
|
|
59
82
|
/** Called on sign-in, refresh and sign-out. Returns an unsubscribe function. */
|
|
60
83
|
onChange(listener: (s: AuthState) => void): () => void;
|
|
61
84
|
private completeSignIn;
|
|
62
|
-
/**
|
|
85
|
+
/**
|
|
86
|
+
* The application's public configuration: name, region, which sign-in methods are on, the
|
|
87
|
+
* user fields the sign-up form should show (off / optional / required) and the providers.
|
|
88
|
+
*/
|
|
89
|
+
getAppConfig(): Promise<AppConfig>;
|
|
90
|
+
/**
|
|
91
|
+
* Always resolves to `{status: "verification_sent"}`, also when the address already has an
|
|
92
|
+
* account. `profile` holds the values of the application's user fields (see `getAppConfig`).
|
|
93
|
+
*/
|
|
63
94
|
signUp(input: {
|
|
64
95
|
email: string;
|
|
65
96
|
password: string;
|
|
66
97
|
country: string;
|
|
67
98
|
name?: string;
|
|
99
|
+
profile?: Record<string, unknown>;
|
|
68
100
|
}): Promise<{
|
|
69
101
|
status: "verification_sent";
|
|
70
102
|
}>;
|
|
@@ -113,6 +145,40 @@ export declare class FoxLogin {
|
|
|
113
145
|
/** Forces a refresh. Signs out on an invalid refresh token. */
|
|
114
146
|
refresh(): Promise<string | null>;
|
|
115
147
|
private doRefresh;
|
|
148
|
+
/**
|
|
149
|
+
* The URL that starts Google sign-in. `redirectUri` must be one of the application's
|
|
150
|
+
* redirect URLs. Fox Login picks the Google app itself (its own, or the application's own
|
|
151
|
+
* when set up in the dashboard), so this URL is the same in both modes.
|
|
152
|
+
*/
|
|
153
|
+
googleSignInUrl(input: {
|
|
154
|
+
redirectUri: string;
|
|
155
|
+
state?: string;
|
|
156
|
+
country?: string;
|
|
157
|
+
}): string;
|
|
158
|
+
/**
|
|
159
|
+
* Browser: sends the user to Google. A random `state` is kept in sessionStorage and checked
|
|
160
|
+
* by `handleRedirect` when the browser comes back to `redirectUri`.
|
|
161
|
+
*/
|
|
162
|
+
signInWithGoogle(input: {
|
|
163
|
+
redirectUri: string;
|
|
164
|
+
country?: string;
|
|
165
|
+
}): void;
|
|
166
|
+
/**
|
|
167
|
+
* Call on the page Google sign-in returns to (with its URL, by default `location.href`).
|
|
168
|
+
* Resolves to `null` when the URL is not a Fox Login return; otherwise signs in
|
|
169
|
+
* (`signed_in` / `mfa_required`), or asks for the user's country first
|
|
170
|
+
* (`country_required` + `ticket`, then call `completeGoogleSignUp`). Throws `FoxLoginError`
|
|
171
|
+
* with the code from `fox_error` when the sign-in did not finish.
|
|
172
|
+
*/
|
|
173
|
+
handleRedirect(url?: string): Promise<SignInResult | {
|
|
174
|
+
status: "country_required";
|
|
175
|
+
ticket: string;
|
|
176
|
+
} | null>;
|
|
177
|
+
/** New Google user: creates the account in the chosen country and signs in. */
|
|
178
|
+
completeGoogleSignUp(input: {
|
|
179
|
+
ticket: string;
|
|
180
|
+
country: string;
|
|
181
|
+
}): Promise<SignInResult>;
|
|
116
182
|
/** Ends the session on the server and locally. */
|
|
117
183
|
signOut(): Promise<void>;
|
|
118
184
|
}
|
package/dist/client.js
CHANGED
|
@@ -30,6 +30,12 @@ export function regionFromPublishableKey(key) {
|
|
|
30
30
|
* const token = await fox.getToken(); // send to your backend as Bearer
|
|
31
31
|
* ```
|
|
32
32
|
*/
|
|
33
|
+
const STATE_KEY = "foxlogin:google_state";
|
|
34
|
+
function randomState() {
|
|
35
|
+
const b = new Uint8Array(16);
|
|
36
|
+
globalThis.crypto.getRandomValues(b);
|
|
37
|
+
return Array.from(b, (x) => x.toString(16).padStart(2, "0")).join("");
|
|
38
|
+
}
|
|
33
39
|
export class FoxLogin {
|
|
34
40
|
baseUrl;
|
|
35
41
|
key;
|
|
@@ -108,7 +114,17 @@ export class FoxLogin {
|
|
|
108
114
|
await this.save(s.user, s);
|
|
109
115
|
return { status: "signed_in", user: s.user };
|
|
110
116
|
}
|
|
111
|
-
/**
|
|
117
|
+
/**
|
|
118
|
+
* The application's public configuration: name, region, which sign-in methods are on, the
|
|
119
|
+
* user fields the sign-up form should show (off / optional / required) and the providers.
|
|
120
|
+
*/
|
|
121
|
+
getAppConfig() {
|
|
122
|
+
return request(this.fetchImpl, `${this.baseUrl}/v1/client/app`, { method: "GET", headers: { "x-fox-publishable-key": this.key } });
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Always resolves to `{status: "verification_sent"}`, also when the address already has an
|
|
126
|
+
* account. `profile` holds the values of the application's user fields (see `getAppConfig`).
|
|
127
|
+
*/
|
|
112
128
|
signUp(input) {
|
|
113
129
|
return this.post("/v1/client/sign_up", input);
|
|
114
130
|
}
|
|
@@ -179,6 +195,67 @@ export class FoxLogin {
|
|
|
179
195
|
throw e;
|
|
180
196
|
}
|
|
181
197
|
}
|
|
198
|
+
/**
|
|
199
|
+
* The URL that starts Google sign-in. `redirectUri` must be one of the application's
|
|
200
|
+
* redirect URLs. Fox Login picks the Google app itself (its own, or the application's own
|
|
201
|
+
* when set up in the dashboard), so this URL is the same in both modes.
|
|
202
|
+
*/
|
|
203
|
+
googleSignInUrl(input) {
|
|
204
|
+
const q = new URLSearchParams({ publishable_key: this.key, redirect_uri: input.redirectUri });
|
|
205
|
+
if (input.state)
|
|
206
|
+
q.set("state", input.state);
|
|
207
|
+
if (input.country)
|
|
208
|
+
q.set("country", input.country);
|
|
209
|
+
return `${this.baseUrl}/v1/oauth/google/start?${q}`;
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Browser: sends the user to Google. A random `state` is kept in sessionStorage and checked
|
|
213
|
+
* by `handleRedirect` when the browser comes back to `redirectUri`.
|
|
214
|
+
*/
|
|
215
|
+
signInWithGoogle(input) {
|
|
216
|
+
const state = randomState();
|
|
217
|
+
try {
|
|
218
|
+
globalThis.sessionStorage?.setItem(STATE_KEY, state);
|
|
219
|
+
}
|
|
220
|
+
catch {
|
|
221
|
+
/* storage blocked: the state check is skipped */
|
|
222
|
+
}
|
|
223
|
+
globalThis.location.assign(this.googleSignInUrl({ ...input, state }));
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Call on the page Google sign-in returns to (with its URL, by default `location.href`).
|
|
227
|
+
* Resolves to `null` when the URL is not a Fox Login return; otherwise signs in
|
|
228
|
+
* (`signed_in` / `mfa_required`), or asks for the user's country first
|
|
229
|
+
* (`country_required` + `ticket`, then call `completeGoogleSignUp`). Throws `FoxLoginError`
|
|
230
|
+
* with the code from `fox_error` when the sign-in did not finish.
|
|
231
|
+
*/
|
|
232
|
+
async handleRedirect(url = globalThis.location?.href ?? "") {
|
|
233
|
+
const u = new URL(url);
|
|
234
|
+
const q = u.searchParams;
|
|
235
|
+
if (!q.has("fox_code") && !q.has("fox_status") && !q.has("fox_error"))
|
|
236
|
+
return null;
|
|
237
|
+
let expected = null;
|
|
238
|
+
try {
|
|
239
|
+
expected = globalThis.sessionStorage?.getItem(STATE_KEY) ?? null;
|
|
240
|
+
globalThis.sessionStorage?.removeItem(STATE_KEY);
|
|
241
|
+
}
|
|
242
|
+
catch {
|
|
243
|
+
expected = null;
|
|
244
|
+
}
|
|
245
|
+
if (expected && q.get("state") !== expected) {
|
|
246
|
+
throw new FoxLoginError({ status: 400, code: "invalid_state", message: "The sign-in did not start in this browser. Start again.", retryable: false });
|
|
247
|
+
}
|
|
248
|
+
const err = q.get("fox_error");
|
|
249
|
+
if (err)
|
|
250
|
+
throw new FoxLoginError({ status: 400, code: err, message: `Google sign-in did not finish (${err}).`, retryable: err !== "access_denied" });
|
|
251
|
+
if (q.get("fox_status") === "country_required")
|
|
252
|
+
return { status: "country_required", ticket: q.get("fox_ticket") ?? "" };
|
|
253
|
+
return this.completeSignIn("/v1/client/oauth/token", { code: q.get("fox_code") });
|
|
254
|
+
}
|
|
255
|
+
/** New Google user: creates the account in the chosen country and signs in. */
|
|
256
|
+
completeGoogleSignUp(input) {
|
|
257
|
+
return this.completeSignIn("/v1/client/oauth/complete", input);
|
|
258
|
+
}
|
|
182
259
|
/** Ends the session on the server and locally. */
|
|
183
260
|
async signOut() {
|
|
184
261
|
await this.loaded;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export { FoxLogin, memoryStorage, localStorageStorage, regionFromPublishableKey } from "./client.js";
|
|
2
|
-
export type { FoxLoginOptions, SessionStorage, AuthState } from "./client.js";
|
|
2
|
+
export type { FoxLoginOptions, SessionStorage, AuthState, AppConfig, FieldRule } from "./client.js";
|
|
3
3
|
export { FoxLoginError } from "./errors.js";
|
|
4
4
|
export type { User, Session, TokenPair, SessionClaims, Region, SignInResult, MfaRequired } from "./types.js";
|
package/dist/types.d.ts
CHANGED
|
@@ -8,6 +8,8 @@ export interface User {
|
|
|
8
8
|
country: string;
|
|
9
9
|
status: "active" | "locked";
|
|
10
10
|
labels: Record<string, string>;
|
|
11
|
+
/** Values of the application's user fields (username, phone, custom fields…). */
|
|
12
|
+
profile?: Record<string, unknown>;
|
|
11
13
|
created_at: string;
|
|
12
14
|
last_sign_in_at: string | null;
|
|
13
15
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "foxlogin",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Fox Login SDK for browsers and Node.js: sign-up, sign-in, sessions, token verification and the Management API. User data stays in its own region (EU, TR).",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "METANETSOFT",
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"types": "./dist/index.d.ts",
|
|
21
21
|
"scripts": {
|
|
22
22
|
"build": "tsc -p tsconfig.json",
|
|
23
|
-
"test": "npm run build && node --test test/",
|
|
23
|
+
"test": "npm run build && node --test test/sdk.test.js",
|
|
24
24
|
"prepublishOnly": "npm test"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": { "jose": "^6.1.0" },
|