@leisuresaas/react-native 0.6.8-dev.2 → 0.6.8
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/package.json +1 -1
- package/src/errors.ts +18 -0
- package/src/index.ts +14 -1
- package/src/public-auth.ts +73 -4
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leisuresaas/react-native",
|
|
3
|
-
"version": "0.6.8
|
|
3
|
+
"version": "0.6.8",
|
|
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": {
|
package/src/errors.ts
CHANGED
|
@@ -18,6 +18,13 @@ export const ErrorReason = {
|
|
|
18
18
|
InvalidCredentials: "INVALID_CREDENTIALS",
|
|
19
19
|
EmailAlreadyExists: "EMAIL_ALREADY_EXISTS",
|
|
20
20
|
EmailNotVerified: "EMAIL_NOT_VERIFIED",
|
|
21
|
+
// Password reset: the token is missing, unknown, used, expired or for another product.
|
|
22
|
+
PasswordResetTokenInvalid: "PASSWORD_RESET_TOKEN_INVALID",
|
|
23
|
+
// Email codes: wrong code, no active code, or voided after 5 wrong tries / older than 10 minutes.
|
|
24
|
+
EmailCodeInvalid: "EMAIL_CODE_INVALID",
|
|
25
|
+
EmailCodeExpired: "EMAIL_CODE_EXPIRED",
|
|
26
|
+
// The endpoint is switched off on the platform (HTTP 404).
|
|
27
|
+
FeatureDisabled: "FEATURE_DISABLED",
|
|
21
28
|
// Workspaces, seats and billing (returned by the product BFF).
|
|
22
29
|
TenantForbidden: "TENANT_FORBIDDEN",
|
|
23
30
|
TenantInviteInvalid: "TENANT_INVITE_INVALID",
|
|
@@ -61,6 +68,7 @@ type PlatformErrorInit = {
|
|
|
61
68
|
message: string;
|
|
62
69
|
domain?: string;
|
|
63
70
|
rawBody?: string;
|
|
71
|
+
retryAfter?: number;
|
|
64
72
|
};
|
|
65
73
|
|
|
66
74
|
/**
|
|
@@ -73,6 +81,8 @@ export class PlatformError extends Error {
|
|
|
73
81
|
readonly statusCode: number;
|
|
74
82
|
readonly domain?: string;
|
|
75
83
|
readonly rawBody: string;
|
|
84
|
+
/** Seconds to wait before retrying, from `retry_after` on RATE_LIMITED responses. */
|
|
85
|
+
readonly retryAfter?: number;
|
|
76
86
|
|
|
77
87
|
constructor(init: PlatformErrorInit) {
|
|
78
88
|
super(init.message || init.reason || `HTTP ${init.statusCode}`);
|
|
@@ -82,6 +92,7 @@ export class PlatformError extends Error {
|
|
|
82
92
|
this.statusCode = init.statusCode;
|
|
83
93
|
this.domain = init.domain;
|
|
84
94
|
this.rawBody = init.rawBody ?? "";
|
|
95
|
+
this.retryAfter = init.retryAfter;
|
|
85
96
|
}
|
|
86
97
|
|
|
87
98
|
/** Raw response body (compat with older HttpError wrappers). */
|
|
@@ -95,6 +106,7 @@ export class PlatformError extends Error {
|
|
|
95
106
|
let message = raw;
|
|
96
107
|
let domain: string | undefined;
|
|
97
108
|
let reason = "";
|
|
109
|
+
let retryAfter: number | undefined;
|
|
98
110
|
|
|
99
111
|
if (raw) {
|
|
100
112
|
try {
|
|
@@ -103,6 +115,7 @@ export class PlatformError extends Error {
|
|
|
103
115
|
message?: string;
|
|
104
116
|
domain?: string;
|
|
105
117
|
reason?: string;
|
|
118
|
+
retry_after?: number;
|
|
106
119
|
error?: { code?: string; message?: string; domain?: string; reason?: string };
|
|
107
120
|
};
|
|
108
121
|
const nested = json.error;
|
|
@@ -110,6 +123,9 @@ export class PlatformError extends Error {
|
|
|
110
123
|
message = String(json.message ?? nested?.message ?? raw).trim() || raw;
|
|
111
124
|
domain = String(json.domain ?? nested?.domain ?? "").trim() || undefined;
|
|
112
125
|
reason = String(json.reason ?? nested?.reason ?? "").trim();
|
|
126
|
+
if (typeof json.retry_after === "number" && json.retry_after > 0) {
|
|
127
|
+
retryAfter = json.retry_after;
|
|
128
|
+
}
|
|
113
129
|
} catch {
|
|
114
130
|
/* keep raw message */
|
|
115
131
|
}
|
|
@@ -132,6 +148,7 @@ export class PlatformError extends Error {
|
|
|
132
148
|
message,
|
|
133
149
|
domain,
|
|
134
150
|
rawBody: raw,
|
|
151
|
+
retryAfter,
|
|
135
152
|
});
|
|
136
153
|
}
|
|
137
154
|
}
|
|
@@ -149,6 +166,7 @@ export class LeisureSaasHttpError extends PlatformError {
|
|
|
149
166
|
message: parsed.message,
|
|
150
167
|
domain: parsed.domain,
|
|
151
168
|
rawBody: parsed.rawBody,
|
|
169
|
+
retryAfter: parsed.retryAfter,
|
|
152
170
|
});
|
|
153
171
|
this.name = "LeisureSaasHttpError";
|
|
154
172
|
}
|
package/src/index.ts
CHANGED
|
@@ -73,6 +73,7 @@ export {
|
|
|
73
73
|
changePassword,
|
|
74
74
|
changeProductPassword,
|
|
75
75
|
confirmEmailVerification,
|
|
76
|
+
confirmEmailCode,
|
|
76
77
|
confirmPasswordReset,
|
|
77
78
|
confirmProductEmailVerification,
|
|
78
79
|
confirmProductPasswordReset,
|
|
@@ -89,6 +90,9 @@ export {
|
|
|
89
90
|
refreshSession,
|
|
90
91
|
register,
|
|
91
92
|
registerProductAccount,
|
|
93
|
+
EmailCodePurpose,
|
|
94
|
+
inspectPasswordReset,
|
|
95
|
+
requestEmailCode,
|
|
92
96
|
requestEmailVerification,
|
|
93
97
|
requestPasswordReset,
|
|
94
98
|
requestProductEmailVerification,
|
|
@@ -100,7 +104,16 @@ export {
|
|
|
100
104
|
revokeSession,
|
|
101
105
|
startHeartbeat,
|
|
102
106
|
} from "./public-auth";
|
|
103
|
-
export type {
|
|
107
|
+
export type {
|
|
108
|
+
AuthTokens,
|
|
109
|
+
EmailCodeRequest,
|
|
110
|
+
EmailProof,
|
|
111
|
+
HeartbeatOptions,
|
|
112
|
+
PasswordResetInfo,
|
|
113
|
+
ProductAuthTokens,
|
|
114
|
+
PublicAuthContext,
|
|
115
|
+
UserSession,
|
|
116
|
+
} from "./public-auth";
|
|
104
117
|
export { appDeviceId } from "./device-id";
|
|
105
118
|
export { getPublicAdsFeed, recordPublicAdEvents } from "./public-ads";
|
|
106
119
|
export type { PublicAdsRequestContext } from "./public-ads";
|
package/src/public-auth.ts
CHANGED
|
@@ -114,16 +114,85 @@ function tokensFromJSON(data: TokenJSON): AuthTokens {
|
|
|
114
114
|
};
|
|
115
115
|
}
|
|
116
116
|
|
|
117
|
+
/**
|
|
118
|
+
* Without `emailProof` the platform emails a verification link and returns `verificationRequired`.
|
|
119
|
+
* With a proof from `confirmEmailCode` (or the product backend) it returns `tokens` instead.
|
|
120
|
+
*/
|
|
117
121
|
export async function register(
|
|
118
122
|
ctx: PublicAuthContext,
|
|
119
|
-
input: { email: string; password: string; name?: string; locale?: string },
|
|
120
|
-
): Promise<{ verificationRequired: boolean }> {
|
|
121
|
-
const
|
|
123
|
+
input: { email: string; password: string; name?: string; locale?: string; emailProof?: string },
|
|
124
|
+
): Promise<{ verificationRequired: boolean; tokens?: AuthTokens }> {
|
|
125
|
+
const body: Record<string, string> = {
|
|
122
126
|
email: input.email, password: input.password, name: input.name ?? "", locale: input.locale ?? "",
|
|
123
|
-
}
|
|
127
|
+
};
|
|
128
|
+
const proof = input.emailProof?.trim() ?? "";
|
|
129
|
+
if (proof) body.email_proof = proof;
|
|
130
|
+
const resp = await publicAuthRequest<TokenJSON & { verification_required?: boolean }>(ctx, "/register", body);
|
|
131
|
+
if (resp.access_token?.trim()) {
|
|
132
|
+
return { verificationRequired: false, tokens: tokensFromJSON(resp) };
|
|
133
|
+
}
|
|
124
134
|
return { verificationRequired: resp.verification_required !== false };
|
|
125
135
|
}
|
|
126
136
|
|
|
137
|
+
/** `purpose` values for `requestEmailCode`. Only `register` today. */
|
|
138
|
+
export const EmailCodePurpose = { Register: "register" } as const;
|
|
139
|
+
|
|
140
|
+
/** Result of `requestEmailCode`; the same for registered emails. Times in seconds. */
|
|
141
|
+
export type EmailCodeRequest = { expiresIn: number; resendAfter: number };
|
|
142
|
+
|
|
143
|
+
/** One-time proof from `confirmEmailCode`; pass `proof` as `register({ emailProof })`. */
|
|
144
|
+
export type EmailProof = { proof: string; expiresAtUnix: number };
|
|
145
|
+
|
|
146
|
+
/** The account behind a valid password reset token. */
|
|
147
|
+
export type PasswordResetInfo = { email: string; expiresAtUnix: number };
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Emails a 6-digit code. A registered email gets an "account exists" notice without a code; the
|
|
151
|
+
* result is the same. Errors: RateLimited (`err.retryAfter`), InvalidArgument, FeatureDisabled.
|
|
152
|
+
*/
|
|
153
|
+
export async function requestEmailCode(
|
|
154
|
+
ctx: PublicAuthContext,
|
|
155
|
+
input: { email: string; purpose?: string; locale?: string },
|
|
156
|
+
): Promise<EmailCodeRequest> {
|
|
157
|
+
const resp = await publicAuthRequest<{ expires_in?: number; resend_after?: number }>(ctx, "/email-code/request", {
|
|
158
|
+
email: input.email.trim(), purpose: input.purpose ?? EmailCodePurpose.Register, locale: input.locale ?? "",
|
|
159
|
+
});
|
|
160
|
+
return { expiresIn: resp.expires_in ?? 0, resendAfter: resp.resend_after ?? 0 };
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Checks the code and returns an email proof for `register`. Errors: EmailCodeInvalid (wrong code,
|
|
165
|
+
* no code, or 5 wrong tries), EmailCodeExpired, RateLimited, FeatureDisabled.
|
|
166
|
+
*/
|
|
167
|
+
export async function confirmEmailCode(
|
|
168
|
+
ctx: PublicAuthContext,
|
|
169
|
+
input: { email: string; purpose?: string; code: string },
|
|
170
|
+
): Promise<EmailProof> {
|
|
171
|
+
const resp = await publicAuthRequest<{ email_proof?: string; expires_at_unix?: number }>(ctx, "/email-code/confirm", {
|
|
172
|
+
email: input.email.trim(), purpose: input.purpose ?? EmailCodePurpose.Register, code: input.code.trim(),
|
|
173
|
+
});
|
|
174
|
+
const proof = resp.email_proof?.trim() ?? "";
|
|
175
|
+
if (!proof) {
|
|
176
|
+
throw PlatformError.fromHttp(502, "missing email_proof");
|
|
177
|
+
}
|
|
178
|
+
return { proof, expiresAtUnix: resp.expires_at_unix ?? 0 };
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Returns the email behind the `token` of a password reset link; the token is not consumed.
|
|
183
|
+
* Errors: PasswordResetTokenInvalid, RateLimited, FeatureDisabled.
|
|
184
|
+
*/
|
|
185
|
+
export async function inspectPasswordReset(ctx: PublicAuthContext, token: string): Promise<PasswordResetInfo> {
|
|
186
|
+
const resp = await publicAuthRequest<{ email?: string; expires_at_unix?: number }>(ctx, "/password-reset/inspect", {
|
|
187
|
+
token: token.trim(),
|
|
188
|
+
});
|
|
189
|
+
const email = resp.email?.trim() ?? "";
|
|
190
|
+
if (!email) {
|
|
191
|
+
throw PlatformError.fromHttp(502, "missing email");
|
|
192
|
+
}
|
|
193
|
+
return { email, expiresAtUnix: resp.expires_at_unix ?? 0 };
|
|
194
|
+
}
|
|
195
|
+
|
|
127
196
|
export async function login(
|
|
128
197
|
ctx: PublicAuthContext,
|
|
129
198
|
input: { email: string; password: string; locale?: string },
|