@leisuresaas/react-native 0.6.9 → 0.6.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -0
- package/package.json +2 -1
- package/src/app-config-json.ts +71 -0
- package/src/consents.ts +87 -0
- package/src/errors.ts +15 -0
- package/src/index.ts +13 -0
- package/src/public-app-config.ts +4 -59
- package/src/public-auth-body.ts +77 -0
- package/src/public-auth.ts +45 -55
- package/src/social-login.ts +7 -0
package/README.md
CHANGED
|
@@ -54,6 +54,10 @@ await signInWithGoogle({ gatewayUrl, publishableKey, /* ios/android client ids
|
|
|
54
54
|
|
|
55
55
|
Admin → Social login 必须配置,否则换产品会话失败。Publishable Key 白名单含本 App 的 bundle / package。
|
|
56
56
|
|
|
57
|
+
## 同意项
|
|
58
|
+
|
|
59
|
+
注册页按 `getPublicAppConfig` 返回的 `consents` 画勾选框,勾选项传给 `register({ consents })`。登录后调 `getConsents`,`allRequiredAccepted` 为假时显示同意页,再 `submitConsents`。详见 [AGENTS.md](AGENTS.md) §2c。
|
|
60
|
+
|
|
57
61
|
## Push(产品 BFF 登记)
|
|
58
62
|
|
|
59
63
|
```ts
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leisuresaas/react-native",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.10",
|
|
4
4
|
"description": "React Native SDK for LeisureSaas: OAuth, Public Ads/App Config, Drive User Plane (publishable keys). Frontend-only — no Integration Key.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -25,6 +25,7 @@
|
|
|
25
25
|
},
|
|
26
26
|
"scripts": {
|
|
27
27
|
"typecheck": "tsc --noEmit",
|
|
28
|
+
"test": "tsc -p tsconfig.test.json && node --test test/",
|
|
28
29
|
"prepublishOnly": "npm run typecheck"
|
|
29
30
|
},
|
|
30
31
|
"peerDependencies": {
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// App Config JSON type and normalization. No React Native imports, so `npm test` can run it.
|
|
2
|
+
import { type ConsentItem, consentItemsFromJSON } from "./consents";
|
|
3
|
+
|
|
4
|
+
export type AppConfigResponse = {
|
|
5
|
+
platform: string;
|
|
6
|
+
policy: {
|
|
7
|
+
min_supported_version: string;
|
|
8
|
+
latest_version: string;
|
|
9
|
+
store_url: string;
|
|
10
|
+
release_notes?: string;
|
|
11
|
+
update_message?: string;
|
|
12
|
+
};
|
|
13
|
+
/** Product support / legal URLs (Admin → Branding → Links). Empty string when unset. */
|
|
14
|
+
links: {
|
|
15
|
+
support_url: string;
|
|
16
|
+
terms_url: string;
|
|
17
|
+
privacy_url: string;
|
|
18
|
+
};
|
|
19
|
+
/** Product authoring locales (BCP 47) from Admin product Settings. */
|
|
20
|
+
supported_locales: string[];
|
|
21
|
+
/** Product default locale; always a member of supported_locales. */
|
|
22
|
+
default_locale: string;
|
|
23
|
+
client?: {
|
|
24
|
+
app_version?: string;
|
|
25
|
+
app_build?: string;
|
|
26
|
+
};
|
|
27
|
+
update: {
|
|
28
|
+
required: boolean;
|
|
29
|
+
recommended: boolean;
|
|
30
|
+
reason: string;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Consent items to show on the sign-up form and the consent screen, in display order. [] when the
|
|
34
|
+
* product has none. Send `fresh: true` before a consent screen to get the newest versions.
|
|
35
|
+
*/
|
|
36
|
+
consents: ConsentItem[];
|
|
37
|
+
social_login?: {
|
|
38
|
+
google?: {
|
|
39
|
+
enabled?: boolean;
|
|
40
|
+
web_client_id?: string;
|
|
41
|
+
ios_client_id?: string;
|
|
42
|
+
android_client_ids?: string[];
|
|
43
|
+
};
|
|
44
|
+
apple?: {
|
|
45
|
+
enabled?: boolean;
|
|
46
|
+
services_id?: string;
|
|
47
|
+
bundle_ids?: string[];
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/** @internal */
|
|
53
|
+
export function appConfigFromJSON(text: string): AppConfigResponse {
|
|
54
|
+
const body = JSON.parse(text) as AppConfigResponse;
|
|
55
|
+
const links = body.links ?? { support_url: "", terms_url: "", privacy_url: "" };
|
|
56
|
+
const supported = Array.isArray(body.supported_locales)
|
|
57
|
+
? body.supported_locales.map((x) => String(x ?? "").trim()).filter(Boolean)
|
|
58
|
+
: [];
|
|
59
|
+
const defaultLocale = String(body.default_locale ?? "").trim() || "en";
|
|
60
|
+
return {
|
|
61
|
+
...body,
|
|
62
|
+
links: {
|
|
63
|
+
support_url: String(links.support_url ?? "").trim(),
|
|
64
|
+
terms_url: String(links.terms_url ?? "").trim(),
|
|
65
|
+
privacy_url: String(links.privacy_url ?? "").trim(),
|
|
66
|
+
},
|
|
67
|
+
supported_locales: supported.length > 0 ? supported : ["en"],
|
|
68
|
+
default_locale: defaultLocale,
|
|
69
|
+
consents: consentItemsFromJSON(body.consents),
|
|
70
|
+
};
|
|
71
|
+
}
|
package/src/consents.ts
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Consent items (terms, privacy, health data …) and the signed-in user's consent state.
|
|
3
|
+
* Pure helpers: no React Native imports, so they run under `npm test`.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/** A consent item the user accepted: key and the version shown to them. */
|
|
7
|
+
export type ConsentGrant = { key: string; version: number };
|
|
8
|
+
|
|
9
|
+
/** A consent item from App Config `consents`, in display order. `url` is empty when unset. */
|
|
10
|
+
export type ConsentItem = { key: string; version: number; url: string; required: boolean };
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* `accepted`: the current version is accepted. `outdated`: an older version is accepted.
|
|
14
|
+
* `withdrawn`: the user withdrew it. `missing`: never accepted.
|
|
15
|
+
*/
|
|
16
|
+
export type ConsentStatusValue = "accepted" | "outdated" | "withdrawn" | "missing";
|
|
17
|
+
|
|
18
|
+
export type ConsentStatus = ConsentItem & {
|
|
19
|
+
status: ConsentStatusValue;
|
|
20
|
+
/** Last accepted version; null when never accepted or withdrawn. */
|
|
21
|
+
acceptedVersion: number | null;
|
|
22
|
+
/** RFC 3339 time of the last accept; null when never accepted or withdrawn. */
|
|
23
|
+
acceptedAt: string | null;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/** Result of `getConsents`, `submitConsents` and `withdrawConsents`. */
|
|
27
|
+
export type ConsentState = { items: ConsentStatus[]; allRequiredAccepted: boolean };
|
|
28
|
+
|
|
29
|
+
type GrantJSON = { key?: unknown; version?: unknown };
|
|
30
|
+
|
|
31
|
+
type StatusJSON = GrantJSON & {
|
|
32
|
+
url?: unknown;
|
|
33
|
+
required?: unknown;
|
|
34
|
+
status?: unknown;
|
|
35
|
+
accepted_version?: unknown;
|
|
36
|
+
accepted_at?: unknown;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const STATUSES: readonly string[] = ["accepted", "outdated", "withdrawn", "missing"];
|
|
40
|
+
|
|
41
|
+
function num(v: unknown): number {
|
|
42
|
+
return typeof v === "number" && Number.isFinite(v) ? v : 0;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** @internal Wire form of `consents`; undefined when there is nothing to send. */
|
|
46
|
+
export function consentGrantsJSON(grants?: ConsentGrant[]): ConsentGrant[] | undefined {
|
|
47
|
+
if (!grants || grants.length === 0) return undefined;
|
|
48
|
+
return grants.map((g) => ({ key: g.key, version: g.version }));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** @internal Parses `[{key, version}]`; anything else is []. */
|
|
52
|
+
export function consentGrantsFromJSON(raw: unknown): ConsentGrant[] {
|
|
53
|
+
if (!Array.isArray(raw)) return [];
|
|
54
|
+
return raw.map((g: GrantJSON) => ({ key: String(g?.key ?? ""), version: num(g?.version) }));
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** @internal Parses App Config `consents`; missing or malformed is []. */
|
|
58
|
+
export function consentItemsFromJSON(raw: unknown): ConsentItem[] {
|
|
59
|
+
if (!Array.isArray(raw)) return [];
|
|
60
|
+
return raw.map((c: StatusJSON) => ({
|
|
61
|
+
key: String(c?.key ?? ""),
|
|
62
|
+
version: num(c?.version),
|
|
63
|
+
url: String(c?.url ?? ""),
|
|
64
|
+
required: c?.required === true,
|
|
65
|
+
}));
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** @internal Parses `{items, all_required_accepted}`. */
|
|
69
|
+
export function consentStateFromJSON(raw: unknown): ConsentState {
|
|
70
|
+
const body = (raw ?? {}) as { items?: unknown; all_required_accepted?: unknown };
|
|
71
|
+
const items = Array.isArray(body.items) ? (body.items as StatusJSON[]) : [];
|
|
72
|
+
return {
|
|
73
|
+
items: items.map((c) => {
|
|
74
|
+
const status = String(c?.status ?? "");
|
|
75
|
+
return {
|
|
76
|
+
key: String(c?.key ?? ""),
|
|
77
|
+
version: num(c?.version),
|
|
78
|
+
url: String(c?.url ?? ""),
|
|
79
|
+
required: c?.required === true,
|
|
80
|
+
status: (STATUSES.includes(status) ? status : "missing") as ConsentStatusValue,
|
|
81
|
+
acceptedVersion: typeof c?.accepted_version === "number" ? c.accepted_version : null,
|
|
82
|
+
acceptedAt: typeof c?.accepted_at === "string" && c.accepted_at ? c.accepted_at : null,
|
|
83
|
+
};
|
|
84
|
+
}),
|
|
85
|
+
allRequiredAccepted: body.all_required_accepted === true,
|
|
86
|
+
};
|
|
87
|
+
}
|
package/src/errors.ts
CHANGED
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
* `{ code, message, domain?, reason? }` into a typed Error.
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
+
import { type ConsentGrant, consentGrantsFromJSON } from "./consents";
|
|
7
|
+
|
|
6
8
|
export const ErrorReason = {
|
|
7
9
|
PermissionDenied: "PERMISSION_DENIED",
|
|
8
10
|
InvalidArgument: "INVALID_ARGUMENT",
|
|
@@ -55,6 +57,10 @@ export const ErrorReason = {
|
|
|
55
57
|
CareDelegationForbidden: "CARE_DELEGATION_FORBIDDEN",
|
|
56
58
|
CareDelegationDriveNotAllowed: "CARE_DELEGATION_DRIVE_NOT_ALLOWED",
|
|
57
59
|
CareDelegationInvalid: "CARE_DELEGATION_INVALID",
|
|
60
|
+
// Consent: register needs required items the user has not accepted (HTTP 412, `missingConsents`) /
|
|
61
|
+
// unknown key, version above the current one, a key twice, or an empty list.
|
|
62
|
+
ConsentRequired: "CONSENT_REQUIRED",
|
|
63
|
+
ConsentInvalid: "CONSENT_INVALID",
|
|
58
64
|
Network: "NETWORK",
|
|
59
65
|
Unknown: "UNKNOWN",
|
|
60
66
|
} as const;
|
|
@@ -69,6 +75,7 @@ type PlatformErrorInit = {
|
|
|
69
75
|
domain?: string;
|
|
70
76
|
rawBody?: string;
|
|
71
77
|
retryAfter?: number;
|
|
78
|
+
missingConsents?: ConsentGrant[];
|
|
72
79
|
};
|
|
73
80
|
|
|
74
81
|
/**
|
|
@@ -83,6 +90,8 @@ export class PlatformError extends Error {
|
|
|
83
90
|
readonly rawBody: string;
|
|
84
91
|
/** Seconds to wait before retrying, from `retry_after` on RATE_LIMITED responses. */
|
|
85
92
|
readonly retryAfter?: number;
|
|
93
|
+
/** Items to accept, with their current version, from `missing_consents` on CONSENT_REQUIRED; else []. */
|
|
94
|
+
readonly missingConsents: ConsentGrant[];
|
|
86
95
|
|
|
87
96
|
constructor(init: PlatformErrorInit) {
|
|
88
97
|
super(init.message || init.reason || `HTTP ${init.statusCode}`);
|
|
@@ -93,6 +102,7 @@ export class PlatformError extends Error {
|
|
|
93
102
|
this.domain = init.domain;
|
|
94
103
|
this.rawBody = init.rawBody ?? "";
|
|
95
104
|
this.retryAfter = init.retryAfter;
|
|
105
|
+
this.missingConsents = init.missingConsents ?? [];
|
|
96
106
|
}
|
|
97
107
|
|
|
98
108
|
/** Raw response body (compat with older HttpError wrappers). */
|
|
@@ -107,6 +117,7 @@ export class PlatformError extends Error {
|
|
|
107
117
|
let domain: string | undefined;
|
|
108
118
|
let reason = "";
|
|
109
119
|
let retryAfter: number | undefined;
|
|
120
|
+
let missingConsents: ConsentGrant[] = [];
|
|
110
121
|
|
|
111
122
|
if (raw) {
|
|
112
123
|
try {
|
|
@@ -116,6 +127,7 @@ export class PlatformError extends Error {
|
|
|
116
127
|
domain?: string;
|
|
117
128
|
reason?: string;
|
|
118
129
|
retry_after?: number;
|
|
130
|
+
missing_consents?: unknown;
|
|
119
131
|
error?: { code?: string; message?: string; domain?: string; reason?: string };
|
|
120
132
|
};
|
|
121
133
|
const nested = json.error;
|
|
@@ -126,6 +138,7 @@ export class PlatformError extends Error {
|
|
|
126
138
|
if (typeof json.retry_after === "number" && json.retry_after > 0) {
|
|
127
139
|
retryAfter = json.retry_after;
|
|
128
140
|
}
|
|
141
|
+
missingConsents = consentGrantsFromJSON(json.missing_consents);
|
|
129
142
|
} catch {
|
|
130
143
|
/* keep raw message */
|
|
131
144
|
}
|
|
@@ -149,6 +162,7 @@ export class PlatformError extends Error {
|
|
|
149
162
|
domain,
|
|
150
163
|
rawBody: raw,
|
|
151
164
|
retryAfter,
|
|
165
|
+
missingConsents,
|
|
152
166
|
});
|
|
153
167
|
}
|
|
154
168
|
}
|
|
@@ -167,6 +181,7 @@ export class LeisureSaasHttpError extends PlatformError {
|
|
|
167
181
|
domain: parsed.domain,
|
|
168
182
|
rawBody: parsed.rawBody,
|
|
169
183
|
retryAfter: parsed.retryAfter,
|
|
184
|
+
missingConsents: parsed.missingConsents,
|
|
170
185
|
});
|
|
171
186
|
this.name = "LeisureSaasHttpError";
|
|
172
187
|
}
|
package/src/index.ts
CHANGED
|
@@ -102,10 +102,16 @@ export {
|
|
|
102
102
|
listSessions,
|
|
103
103
|
revokeSession,
|
|
104
104
|
startHeartbeat,
|
|
105
|
+
getConsents,
|
|
106
|
+
submitConsents,
|
|
107
|
+
withdrawConsents,
|
|
105
108
|
} from "./public-auth";
|
|
106
109
|
export type {
|
|
107
110
|
AuthTokens,
|
|
108
111
|
HeartbeatOptions,
|
|
112
|
+
LoginWithAppleInput,
|
|
113
|
+
LoginWithGoogleInput,
|
|
114
|
+
RegisterInput,
|
|
109
115
|
PasswordResetInfo,
|
|
110
116
|
ProductAuthTokens,
|
|
111
117
|
PublicAuthContext,
|
|
@@ -119,6 +125,13 @@ export { getPublicAdsFeed, recordPublicAdEvents } from "./public-ads";
|
|
|
119
125
|
export type { PublicAdsRequestContext } from "./public-ads";
|
|
120
126
|
export { getPublicAppConfig } from "./public-app-config";
|
|
121
127
|
export type { AppConfigResponse, PublicAppConfigRequestContext } from "./public-app-config";
|
|
128
|
+
export type {
|
|
129
|
+
ConsentGrant,
|
|
130
|
+
ConsentItem,
|
|
131
|
+
ConsentState,
|
|
132
|
+
ConsentStatus,
|
|
133
|
+
ConsentStatusValue,
|
|
134
|
+
} from "./consents";
|
|
122
135
|
export {
|
|
123
136
|
getSocialLoginStatus,
|
|
124
137
|
prepareSocialLogin,
|
package/src/public-app-config.ts
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
|
+
import { type AppConfigResponse, appConfigFromJSON } from "./app-config-json";
|
|
1
2
|
import { PlatformError } from "./errors";
|
|
2
3
|
import { mobilePlatform } from "./platform";
|
|
3
4
|
import { applyPublishableKeyHeaders } from "./publishable-key";
|
|
4
5
|
import type { MobilePlatform } from "./types";
|
|
5
6
|
|
|
7
|
+
export type { AppConfigResponse };
|
|
8
|
+
|
|
6
9
|
export type PublicAppConfigRequestContext = {
|
|
7
10
|
gatewayUrl: string;
|
|
8
11
|
publishableKey: string;
|
|
@@ -12,49 +15,6 @@ export type PublicAppConfigRequestContext = {
|
|
|
12
15
|
locale?: string;
|
|
13
16
|
};
|
|
14
17
|
|
|
15
|
-
export type AppConfigResponse = {
|
|
16
|
-
platform: string;
|
|
17
|
-
policy: {
|
|
18
|
-
min_supported_version: string;
|
|
19
|
-
latest_version: string;
|
|
20
|
-
store_url: string;
|
|
21
|
-
release_notes?: string;
|
|
22
|
-
update_message?: string;
|
|
23
|
-
};
|
|
24
|
-
/** Product support / legal URLs (Admin → Branding → Links). Empty string when unset. */
|
|
25
|
-
links: {
|
|
26
|
-
support_url: string;
|
|
27
|
-
terms_url: string;
|
|
28
|
-
privacy_url: string;
|
|
29
|
-
};
|
|
30
|
-
/** Product authoring locales (BCP 47) from Admin product Settings. */
|
|
31
|
-
supported_locales: string[];
|
|
32
|
-
/** Product default locale; always a member of supported_locales. */
|
|
33
|
-
default_locale: string;
|
|
34
|
-
client?: {
|
|
35
|
-
app_version?: string;
|
|
36
|
-
app_build?: string;
|
|
37
|
-
};
|
|
38
|
-
update: {
|
|
39
|
-
required: boolean;
|
|
40
|
-
recommended: boolean;
|
|
41
|
-
reason: string;
|
|
42
|
-
};
|
|
43
|
-
social_login?: {
|
|
44
|
-
google?: {
|
|
45
|
-
enabled?: boolean;
|
|
46
|
-
web_client_id?: string;
|
|
47
|
-
ios_client_id?: string;
|
|
48
|
-
android_client_ids?: string[];
|
|
49
|
-
};
|
|
50
|
-
apple?: {
|
|
51
|
-
enabled?: boolean;
|
|
52
|
-
services_id?: string;
|
|
53
|
-
bundle_ids?: string[];
|
|
54
|
-
};
|
|
55
|
-
};
|
|
56
|
-
};
|
|
57
|
-
|
|
58
18
|
function trimSlash(url: string): string {
|
|
59
19
|
return url.replace(/\/$/, "");
|
|
60
20
|
}
|
|
@@ -100,20 +60,5 @@ export async function getPublicAppConfig(
|
|
|
100
60
|
if (!res.ok) {
|
|
101
61
|
throw PlatformError.fromHttp(res.status, text.trim());
|
|
102
62
|
}
|
|
103
|
-
|
|
104
|
-
const links = body.links ?? { support_url: "", terms_url: "", privacy_url: "" };
|
|
105
|
-
const supported = Array.isArray(body.supported_locales)
|
|
106
|
-
? body.supported_locales.map((x) => String(x ?? "").trim()).filter(Boolean)
|
|
107
|
-
: [];
|
|
108
|
-
const defaultLocale = String(body.default_locale ?? "").trim() || "en";
|
|
109
|
-
return {
|
|
110
|
-
...body,
|
|
111
|
-
links: {
|
|
112
|
-
support_url: String(links.support_url ?? "").trim(),
|
|
113
|
-
terms_url: String(links.terms_url ?? "").trim(),
|
|
114
|
-
privacy_url: String(links.privacy_url ?? "").trim(),
|
|
115
|
-
},
|
|
116
|
-
supported_locales: supported.length > 0 ? supported : ["en"],
|
|
117
|
-
default_locale: defaultLocale,
|
|
118
|
-
};
|
|
63
|
+
return appConfigFromJSON(text);
|
|
119
64
|
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @internal Request bodies of Public Auth calls that take `consents`.
|
|
3
|
+
* Without `consents` (or with an empty list) each body is the same as before consents existed.
|
|
4
|
+
*/
|
|
5
|
+
import { type ConsentGrant, consentGrantsJSON } from "./consents";
|
|
6
|
+
|
|
7
|
+
/** How `register` confirms the email: `"link"` (default) or a 6-digit `"code"`. */
|
|
8
|
+
export type VerificationMethod = "link" | "code";
|
|
9
|
+
|
|
10
|
+
export type RegisterInput = {
|
|
11
|
+
email: string;
|
|
12
|
+
password: string;
|
|
13
|
+
name?: string;
|
|
14
|
+
locale?: string;
|
|
15
|
+
emailProof?: string;
|
|
16
|
+
verification?: VerificationMethod;
|
|
17
|
+
/** Items the user ticked on the sign-up form, from App Config `consents`. */
|
|
18
|
+
consents?: ConsentGrant[];
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export type LoginWithGoogleInput = {
|
|
22
|
+
idToken?: string;
|
|
23
|
+
authorizationCode?: string;
|
|
24
|
+
redirectUri?: string;
|
|
25
|
+
codeVerifier?: string;
|
|
26
|
+
password?: string;
|
|
27
|
+
locale?: string;
|
|
28
|
+
/** Recorded after sign-in for new and existing users; never makes sign-in fail. */
|
|
29
|
+
consents?: ConsentGrant[];
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export type LoginWithAppleInput = {
|
|
33
|
+
identityToken: string;
|
|
34
|
+
authorizationCode?: string;
|
|
35
|
+
fullName?: string;
|
|
36
|
+
password?: string;
|
|
37
|
+
locale?: string;
|
|
38
|
+
/** Recorded after sign-in for new and existing users; never makes sign-in fail. */
|
|
39
|
+
consents?: ConsentGrant[];
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
function withConsents(body: Record<string, unknown>, consents?: ConsentGrant[]): Record<string, unknown> {
|
|
43
|
+
const grants = consentGrantsJSON(consents);
|
|
44
|
+
if (grants) body.consents = grants;
|
|
45
|
+
return body;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function registerBody(input: RegisterInput): Record<string, unknown> {
|
|
49
|
+
const body: Record<string, unknown> = {
|
|
50
|
+
email: input.email, password: input.password, name: input.name ?? "", locale: input.locale ?? "",
|
|
51
|
+
};
|
|
52
|
+
const proof = input.emailProof?.trim() ?? "";
|
|
53
|
+
if (proof) body.email_proof = proof;
|
|
54
|
+
if (input.verification && input.verification !== "link") body.verification = input.verification;
|
|
55
|
+
return withConsents(body, input.consents);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function googleLoginBody(input: LoginWithGoogleInput): Record<string, unknown> {
|
|
59
|
+
return withConsents({
|
|
60
|
+
id_token: input.idToken ?? "",
|
|
61
|
+
authorization_code: input.authorizationCode ?? "",
|
|
62
|
+
redirect_uri: input.redirectUri ?? "",
|
|
63
|
+
code_verifier: input.codeVerifier ?? "",
|
|
64
|
+
password: input.password ?? "",
|
|
65
|
+
locale: input.locale ?? "",
|
|
66
|
+
}, input.consents);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function appleLoginBody(input: LoginWithAppleInput): Record<string, unknown> {
|
|
70
|
+
return withConsents({
|
|
71
|
+
identity_token: input.identityToken,
|
|
72
|
+
authorization_code: input.authorizationCode ?? "",
|
|
73
|
+
full_name: input.fullName ?? "",
|
|
74
|
+
password: input.password ?? "",
|
|
75
|
+
locale: input.locale ?? "",
|
|
76
|
+
}, input.consents);
|
|
77
|
+
}
|
package/src/public-auth.ts
CHANGED
|
@@ -3,9 +3,19 @@ import * as WebBrowser from "expo-web-browser";
|
|
|
3
3
|
import { AppState, type AppStateStatus } from "react-native";
|
|
4
4
|
|
|
5
5
|
import { exchangeAuthorizationCode } from "./auth-session";
|
|
6
|
+
import { type ConsentGrant, type ConsentState, consentStateFromJSON } from "./consents";
|
|
6
7
|
import { appDeviceId } from "./device-id";
|
|
7
8
|
import { PlatformError } from "./errors";
|
|
8
9
|
import { mobilePlatform } from "./platform";
|
|
10
|
+
import {
|
|
11
|
+
appleLoginBody,
|
|
12
|
+
googleLoginBody,
|
|
13
|
+
type LoginWithAppleInput,
|
|
14
|
+
type LoginWithGoogleInput,
|
|
15
|
+
type RegisterInput,
|
|
16
|
+
registerBody,
|
|
17
|
+
type VerificationMethod,
|
|
18
|
+
} from "./public-auth-body";
|
|
9
19
|
import { applyPublishableKeyHeaders } from "./publishable-key";
|
|
10
20
|
import type { MobilePlatform } from "./types";
|
|
11
21
|
|
|
@@ -114,8 +124,7 @@ function tokensFromJSON(data: TokenJSON): AuthTokens {
|
|
|
114
124
|
};
|
|
115
125
|
}
|
|
116
126
|
|
|
117
|
-
|
|
118
|
-
export type VerificationMethod = "link" | "code";
|
|
127
|
+
export type { LoginWithAppleInput, LoginWithGoogleInput, RegisterInput, VerificationMethod };
|
|
119
128
|
|
|
120
129
|
/**
|
|
121
130
|
* Outcome of `register`. `tokens` is set when `emailProof` skipped verification; otherwise
|
|
@@ -140,24 +149,12 @@ type VerificationJSON = { verification?: string; expires_in?: number; resend_aft
|
|
|
140
149
|
* With a proof from the product backend (Integration `POST /v1/auth/email-proofs`) it returns
|
|
141
150
|
* `tokens` instead and ignores `verification`. Errors: EmailAlreadyExists (verified account), FeatureDisabled (`"code"`
|
|
142
151
|
* switched off), RateLimited (`"code"`, `err.retryAfter`).
|
|
152
|
+
* `consents` lists the App Config items the user ticked; without it the request body is unchanged.
|
|
153
|
+
* Errors: ConsentRequired (412, the product enforces consent at register; `err.missingConsents`),
|
|
154
|
+
* ConsentInvalid (unknown key, version above current, a key twice).
|
|
143
155
|
*/
|
|
144
|
-
export async function register(
|
|
145
|
-
|
|
146
|
-
input: {
|
|
147
|
-
email: string;
|
|
148
|
-
password: string;
|
|
149
|
-
name?: string;
|
|
150
|
-
locale?: string;
|
|
151
|
-
emailProof?: string;
|
|
152
|
-
verification?: VerificationMethod;
|
|
153
|
-
},
|
|
154
|
-
): Promise<RegisterResult> {
|
|
155
|
-
const body: Record<string, string> = {
|
|
156
|
-
email: input.email, password: input.password, name: input.name ?? "", locale: input.locale ?? "",
|
|
157
|
-
};
|
|
158
|
-
const proof = input.emailProof?.trim() ?? "";
|
|
159
|
-
if (proof) body.email_proof = proof;
|
|
160
|
-
if (input.verification && input.verification !== "link") body.verification = input.verification;
|
|
156
|
+
export async function register(ctx: PublicAuthContext, input: RegisterInput): Promise<RegisterResult> {
|
|
157
|
+
const body = registerBody(input);
|
|
161
158
|
const resp = await publicAuthRequest<TokenJSON & VerificationJSON & { verification_required?: boolean }>(
|
|
162
159
|
ctx, "/register", body,
|
|
163
160
|
);
|
|
@@ -234,45 +231,15 @@ export async function login(
|
|
|
234
231
|
return tokensFromJSON(resp);
|
|
235
232
|
}
|
|
236
233
|
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
input
|
|
240
|
-
idToken?: string;
|
|
241
|
-
authorizationCode?: string;
|
|
242
|
-
redirectUri?: string;
|
|
243
|
-
codeVerifier?: string;
|
|
244
|
-
password?: string;
|
|
245
|
-
locale?: string;
|
|
246
|
-
},
|
|
247
|
-
): Promise<AuthTokens> {
|
|
248
|
-
const resp = await publicAuthRequest<TokenJSON>(ctx, "/login/google", {
|
|
249
|
-
id_token: input.idToken ?? "",
|
|
250
|
-
authorization_code: input.authorizationCode ?? "",
|
|
251
|
-
redirect_uri: input.redirectUri ?? "",
|
|
252
|
-
code_verifier: input.codeVerifier ?? "",
|
|
253
|
-
password: input.password ?? "",
|
|
254
|
-
locale: input.locale ?? "",
|
|
255
|
-
});
|
|
234
|
+
/** Optional `consents` are recorded after sign-in; without them the request body is unchanged. */
|
|
235
|
+
export async function loginWithGoogle(ctx: PublicAuthContext, input: LoginWithGoogleInput): Promise<AuthTokens> {
|
|
236
|
+
const resp = await publicAuthRequest<TokenJSON>(ctx, "/login/google", googleLoginBody(input));
|
|
256
237
|
return tokensFromJSON(resp);
|
|
257
238
|
}
|
|
258
239
|
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
input
|
|
262
|
-
identityToken: string;
|
|
263
|
-
authorizationCode?: string;
|
|
264
|
-
fullName?: string;
|
|
265
|
-
password?: string;
|
|
266
|
-
locale?: string;
|
|
267
|
-
},
|
|
268
|
-
): Promise<AuthTokens> {
|
|
269
|
-
const resp = await publicAuthRequest<TokenJSON>(ctx, "/login/apple", {
|
|
270
|
-
identity_token: input.identityToken,
|
|
271
|
-
authorization_code: input.authorizationCode ?? "",
|
|
272
|
-
full_name: input.fullName ?? "",
|
|
273
|
-
password: input.password ?? "",
|
|
274
|
-
locale: input.locale ?? "",
|
|
275
|
-
});
|
|
240
|
+
/** Optional `consents` are recorded after sign-in; without them the request body is unchanged. */
|
|
241
|
+
export async function loginWithApple(ctx: PublicAuthContext, input: LoginWithAppleInput): Promise<AuthTokens> {
|
|
242
|
+
const resp = await publicAuthRequest<TokenJSON>(ctx, "/login/apple", appleLoginBody(input));
|
|
276
243
|
return tokensFromJSON(resp);
|
|
277
244
|
}
|
|
278
245
|
|
|
@@ -379,6 +346,29 @@ export async function revokeSession(ctx: PublicAuthContext, accessToken: string,
|
|
|
379
346
|
await publicAuthRequest(ctx, `/sessions/${encodeURIComponent(sessionId)}/revoke`, {}, accessToken);
|
|
380
347
|
}
|
|
381
348
|
|
|
349
|
+
/** The signed-in user's state for each current consent item of the product. */
|
|
350
|
+
export async function getConsents(ctx: PublicAuthContext, accessToken: string): Promise<ConsentState> {
|
|
351
|
+
const out = await publicAuthRequest<unknown>(ctx, "/consents", undefined, accessToken, "GET");
|
|
352
|
+
return consentStateFromJSON(out);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/** Accepts consent items (`{key, version}` from App Config). Errors: ConsentInvalid. */
|
|
356
|
+
export async function submitConsents(
|
|
357
|
+
ctx: PublicAuthContext,
|
|
358
|
+
accessToken: string,
|
|
359
|
+
consents: ConsentGrant[],
|
|
360
|
+
): Promise<ConsentState> {
|
|
361
|
+
const body = { consents: consents.map((c) => ({ key: c.key, version: c.version })) };
|
|
362
|
+
const out = await publicAuthRequest<unknown>(ctx, "/consents", body, accessToken);
|
|
363
|
+
return consentStateFromJSON(out);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/** Withdraws consent items by key; items not accepted are skipped. Errors: ConsentInvalid. */
|
|
367
|
+
export async function withdrawConsents(ctx: PublicAuthContext, accessToken: string, keys: string[]): Promise<ConsentState> {
|
|
368
|
+
const out = await publicAuthRequest<unknown>(ctx, "/consents/withdraw", { keys }, accessToken);
|
|
369
|
+
return consentStateFromJSON(out);
|
|
370
|
+
}
|
|
371
|
+
|
|
382
372
|
export type HeartbeatOptions = {
|
|
383
373
|
/** Default 60000 ms. */
|
|
384
374
|
intervalMs?: number;
|
package/src/social-login.ts
CHANGED
|
@@ -16,6 +16,7 @@ import {
|
|
|
16
16
|
type AuthTokens,
|
|
17
17
|
type PublicAuthContext,
|
|
18
18
|
} from "./public-auth";
|
|
19
|
+
import type { ConsentGrant } from "./consents";
|
|
19
20
|
import { ErrorReason, isPlatformError, isReason } from "./errors";
|
|
20
21
|
import { appBundleId } from "./ads/bundle-id";
|
|
21
22
|
|
|
@@ -173,6 +174,8 @@ async function loadAppleAuthModule(): Promise<{
|
|
|
173
174
|
}
|
|
174
175
|
|
|
175
176
|
export type SignInWithGoogleOptions = {
|
|
177
|
+
/** Consent items the user ticked (App Config `consents`); recorded after sign-in. */
|
|
178
|
+
consents?: ConsentGrant[];
|
|
176
179
|
password?: string;
|
|
177
180
|
locale?: string;
|
|
178
181
|
appVersion?: string;
|
|
@@ -229,6 +232,7 @@ export async function signInWithGoogle(
|
|
|
229
232
|
idToken,
|
|
230
233
|
password: opts.password,
|
|
231
234
|
locale: opts.locale,
|
|
235
|
+
consents: opts.consents,
|
|
232
236
|
});
|
|
233
237
|
} catch (err) {
|
|
234
238
|
if (isOAuthEmailAlreadyExistsError(err)) {
|
|
@@ -239,6 +243,8 @@ export async function signInWithGoogle(
|
|
|
239
243
|
}
|
|
240
244
|
|
|
241
245
|
export type SignInWithAppleOptions = {
|
|
246
|
+
/** Consent items the user ticked (App Config `consents`); recorded after sign-in. */
|
|
247
|
+
consents?: ConsentGrant[];
|
|
242
248
|
password?: string;
|
|
243
249
|
locale?: string;
|
|
244
250
|
appVersion?: string;
|
|
@@ -307,6 +313,7 @@ export async function signInWithApple(
|
|
|
307
313
|
fullName: fullName || undefined,
|
|
308
314
|
password: opts.password,
|
|
309
315
|
locale: opts.locale,
|
|
316
|
+
consents: opts.consents,
|
|
310
317
|
});
|
|
311
318
|
} catch (err) {
|
|
312
319
|
if (isOAuthEmailAlreadyExistsError(err)) {
|