@leisuresaas/react-native 0.6.3-dev.4 → 0.6.3-dev.6
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 +9 -1
- package/src/index.ts +15 -0
- package/src/social-login.ts +318 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leisuresaas/react-native",
|
|
3
|
-
"version": "0.6.3-dev.
|
|
3
|
+
"version": "0.6.3-dev.6",
|
|
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": {
|
|
@@ -28,7 +28,9 @@
|
|
|
28
28
|
"prepublishOnly": "npm run typecheck"
|
|
29
29
|
},
|
|
30
30
|
"peerDependencies": {
|
|
31
|
+
"@react-native-google-signin/google-signin": ">=13.0.0",
|
|
31
32
|
"expo": ">=52.0.0",
|
|
33
|
+
"expo-apple-authentication": ">=7.0.0",
|
|
32
34
|
"expo-auth-session": ">=6.0.0",
|
|
33
35
|
"expo-linking": ">=7.0.0",
|
|
34
36
|
"expo-notifications": ">=0.29.0",
|
|
@@ -38,6 +40,12 @@
|
|
|
38
40
|
"react-native": ">=0.76.0"
|
|
39
41
|
},
|
|
40
42
|
"peerDependenciesMeta": {
|
|
43
|
+
"@react-native-google-signin/google-signin": {
|
|
44
|
+
"optional": true
|
|
45
|
+
},
|
|
46
|
+
"expo-apple-authentication": {
|
|
47
|
+
"optional": true
|
|
48
|
+
},
|
|
41
49
|
"expo-notifications": {
|
|
42
50
|
"optional": true
|
|
43
51
|
}
|
package/src/index.ts
CHANGED
|
@@ -101,6 +101,21 @@ export { getPublicAdsFeed, recordPublicAdEvents } from "./public-ads";
|
|
|
101
101
|
export type { PublicAdsRequestContext } from "./public-ads";
|
|
102
102
|
export { getPublicAppConfig } from "./public-app-config";
|
|
103
103
|
export type { AppConfigResponse, PublicAppConfigRequestContext } from "./public-app-config";
|
|
104
|
+
export {
|
|
105
|
+
getSocialLoginStatus,
|
|
106
|
+
prepareSocialLogin,
|
|
107
|
+
signInWithApple,
|
|
108
|
+
signInWithGoogle,
|
|
109
|
+
SocialLoginCancelled,
|
|
110
|
+
SocialLoginNeedsLink,
|
|
111
|
+
SocialLoginNotEnabled,
|
|
112
|
+
SocialLoginUnsupported,
|
|
113
|
+
} from "./social-login";
|
|
114
|
+
export type {
|
|
115
|
+
SignInWithAppleOptions,
|
|
116
|
+
SignInWithGoogleOptions,
|
|
117
|
+
SocialLoginStatus,
|
|
118
|
+
} from "./social-login";
|
|
104
119
|
export {
|
|
105
120
|
DRIVE_UPLOAD_MAX_AUTO_PUT_BYTES,
|
|
106
121
|
listFsNodes,
|
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Headless Google / Apple sign-in for React Native.
|
|
3
|
+
* No LeisureSaas UI — call from the product's own Pressable handlers.
|
|
4
|
+
*
|
|
5
|
+
* Optional peers:
|
|
6
|
+
* - `@react-native-google-signin/google-signin` (Google)
|
|
7
|
+
* - `expo-apple-authentication` (Apple, iOS)
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { Platform } from "react-native";
|
|
11
|
+
|
|
12
|
+
import { getPublicAppConfig } from "./public-app-config";
|
|
13
|
+
import {
|
|
14
|
+
loginWithApple,
|
|
15
|
+
loginWithGoogle,
|
|
16
|
+
type AuthTokens,
|
|
17
|
+
type PublicAuthContext,
|
|
18
|
+
} from "./public-auth";
|
|
19
|
+
import { LeisureSaasHttpError } from "./errors";
|
|
20
|
+
import { appBundleId } from "./ads/bundle-id";
|
|
21
|
+
|
|
22
|
+
export class SocialLoginNotEnabled extends Error {
|
|
23
|
+
readonly provider: "google" | "apple";
|
|
24
|
+
constructor(provider: "google" | "apple", message?: string) {
|
|
25
|
+
super(message ?? `${provider} sign-in is not enabled`);
|
|
26
|
+
this.name = "SocialLoginNotEnabled";
|
|
27
|
+
this.provider = provider;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export class SocialLoginCancelled extends Error {
|
|
32
|
+
readonly provider: "google" | "apple";
|
|
33
|
+
constructor(provider: "google" | "apple", message?: string) {
|
|
34
|
+
super(message ?? `${provider} sign-in cancelled`);
|
|
35
|
+
this.name = "SocialLoginCancelled";
|
|
36
|
+
this.provider = provider;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export class SocialLoginUnsupported extends Error {
|
|
41
|
+
constructor(message: string) {
|
|
42
|
+
super(message);
|
|
43
|
+
this.name = "SocialLoginUnsupported";
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export class SocialLoginNeedsLink extends Error {
|
|
48
|
+
readonly provider: "google" | "apple";
|
|
49
|
+
readonly pending: {
|
|
50
|
+
identityToken?: string;
|
|
51
|
+
authorizationCode?: string;
|
|
52
|
+
fullName?: string;
|
|
53
|
+
};
|
|
54
|
+
constructor(
|
|
55
|
+
provider: "google" | "apple",
|
|
56
|
+
pending: SocialLoginNeedsLink["pending"] = {},
|
|
57
|
+
message?: string,
|
|
58
|
+
) {
|
|
59
|
+
super(message ?? "Email already registered; provide password to link");
|
|
60
|
+
this.name = "SocialLoginNeedsLink";
|
|
61
|
+
this.provider = provider;
|
|
62
|
+
this.pending = pending;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function isOAuthEmailAlreadyExistsError(err: unknown): boolean {
|
|
67
|
+
if (!(err instanceof LeisureSaasHttpError)) {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
try {
|
|
71
|
+
const body = JSON.parse(err.body) as { reason?: string; error?: { reason?: string } };
|
|
72
|
+
return (body.reason ?? body.error?.reason) === "OAUTH_EMAIL_ALREADY_EXISTS";
|
|
73
|
+
} catch {
|
|
74
|
+
return err.body.includes("OAUTH_EMAIL_ALREADY_EXISTS");
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export type SocialLoginStatus = {
|
|
79
|
+
google: { enabled: boolean; webClientId: string; iosClientId: string };
|
|
80
|
+
apple: { enabled: boolean; servicesId: string; bundleIds: string[] };
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
let statusCache: { key: string; at: number; value: SocialLoginStatus } | null = null;
|
|
84
|
+
const STATUS_TTL_MS = 60_000;
|
|
85
|
+
|
|
86
|
+
function cacheKey(ctx: PublicAuthContext): string {
|
|
87
|
+
return `${ctx.gatewayUrl}|${ctx.publishableKey}|${ctx.platform ?? ""}|${ctx.bundleId ?? ""}`;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export async function getSocialLoginStatus(
|
|
91
|
+
ctx: PublicAuthContext,
|
|
92
|
+
opts: { fresh?: boolean; locale?: string; appVersion?: string } = {},
|
|
93
|
+
): Promise<SocialLoginStatus> {
|
|
94
|
+
const key = cacheKey(ctx);
|
|
95
|
+
if (!opts.fresh && statusCache && statusCache.key === key && Date.now() - statusCache.at < STATUS_TTL_MS) {
|
|
96
|
+
return statusCache.value;
|
|
97
|
+
}
|
|
98
|
+
const cfg = await getPublicAppConfig(
|
|
99
|
+
{
|
|
100
|
+
gatewayUrl: ctx.gatewayUrl,
|
|
101
|
+
publishableKey: ctx.publishableKey,
|
|
102
|
+
platform: ctx.platform,
|
|
103
|
+
bundleId: ctx.bundleId,
|
|
104
|
+
origin: ctx.origin,
|
|
105
|
+
locale: opts.locale,
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
fresh: opts.fresh,
|
|
109
|
+
appVersion: opts.appVersion ?? "0.0.0",
|
|
110
|
+
},
|
|
111
|
+
);
|
|
112
|
+
const g = cfg.social_login?.google;
|
|
113
|
+
const a = cfg.social_login?.apple;
|
|
114
|
+
const value: SocialLoginStatus = {
|
|
115
|
+
google: {
|
|
116
|
+
enabled: Boolean(g?.enabled && g.web_client_id?.trim()),
|
|
117
|
+
webClientId: g?.web_client_id?.trim() ?? "",
|
|
118
|
+
iosClientId: g?.ios_client_id?.trim() ?? "",
|
|
119
|
+
},
|
|
120
|
+
apple: {
|
|
121
|
+
enabled: Boolean(a?.enabled && (a.services_id?.trim() || (a.bundle_ids?.length ?? 0) > 0)),
|
|
122
|
+
servicesId: a?.services_id?.trim() ?? "",
|
|
123
|
+
bundleIds: Array.isArray(a?.bundle_ids) ? a!.bundle_ids!.map((x) => String(x).trim()).filter(Boolean) : [],
|
|
124
|
+
},
|
|
125
|
+
};
|
|
126
|
+
statusCache = { key, at: Date.now(), value };
|
|
127
|
+
return value;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** Prefetch App Config (no native UI). */
|
|
131
|
+
export async function prepareSocialLogin(
|
|
132
|
+
ctx: PublicAuthContext,
|
|
133
|
+
opts: { locale?: string; appVersion?: string } = {},
|
|
134
|
+
): Promise<SocialLoginStatus> {
|
|
135
|
+
return getSocialLoginStatus(ctx, opts);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
async function loadGoogleSigninModule(): Promise<{
|
|
139
|
+
GoogleSignin: {
|
|
140
|
+
configure: (opts: { webClientId: string; iosClientId?: string; offlineAccess?: boolean }) => void;
|
|
141
|
+
hasPlayServices: (opts?: { showPlayServicesUpdateDialog?: boolean }) => Promise<boolean>;
|
|
142
|
+
signIn: () => Promise<{ data?: { idToken?: string | null } | null; type?: string } | { idToken?: string | null }>;
|
|
143
|
+
};
|
|
144
|
+
statusCodes?: { SIGN_IN_CANCELLED?: string | number; IN_PROGRESS?: string | number };
|
|
145
|
+
}> {
|
|
146
|
+
try {
|
|
147
|
+
// Optional peer — resolved at runtime by the app.
|
|
148
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
149
|
+
return require("@react-native-google-signin/google-signin") as never;
|
|
150
|
+
} catch {
|
|
151
|
+
throw new SocialLoginUnsupported(
|
|
152
|
+
"Install peer @react-native-google-signin/google-signin to use signInWithGoogle",
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
async function loadAppleAuthModule(): Promise<{
|
|
158
|
+
isAvailableAsync: () => Promise<boolean>;
|
|
159
|
+
signInAsync: (opts: { requestedScopes: number[] }) => Promise<{
|
|
160
|
+
identityToken: string | null;
|
|
161
|
+
authorizationCode: string | null;
|
|
162
|
+
fullName?: { givenName?: string | null; familyName?: string | null } | null;
|
|
163
|
+
}>;
|
|
164
|
+
AppleAuthenticationScope: { FULL_NAME: number; EMAIL: number };
|
|
165
|
+
}> {
|
|
166
|
+
try {
|
|
167
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
168
|
+
return require("expo-apple-authentication") as never;
|
|
169
|
+
} catch {
|
|
170
|
+
throw new SocialLoginUnsupported(
|
|
171
|
+
"Install peer expo-apple-authentication to use signInWithApple",
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export type SignInWithGoogleOptions = {
|
|
177
|
+
password?: string;
|
|
178
|
+
locale?: string;
|
|
179
|
+
appVersion?: string;
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
export async function signInWithGoogle(
|
|
183
|
+
ctx: PublicAuthContext,
|
|
184
|
+
opts: SignInWithGoogleOptions = {},
|
|
185
|
+
): Promise<AuthTokens> {
|
|
186
|
+
const status = await getSocialLoginStatus(ctx, { locale: opts.locale, appVersion: opts.appVersion });
|
|
187
|
+
if (!status.google.enabled || !status.google.webClientId) {
|
|
188
|
+
throw new SocialLoginNotEnabled("google");
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const mod = await loadGoogleSigninModule();
|
|
192
|
+
const { GoogleSignin, statusCodes } = mod;
|
|
193
|
+
GoogleSignin.configure({
|
|
194
|
+
webClientId: status.google.webClientId,
|
|
195
|
+
iosClientId: status.google.iosClientId || undefined,
|
|
196
|
+
offlineAccess: false,
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
if (Platform.OS === "android") {
|
|
200
|
+
await GoogleSignin.hasPlayServices({ showPlayServicesUpdateDialog: true });
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
let idToken = "";
|
|
204
|
+
try {
|
|
205
|
+
const result = (await GoogleSignin.signIn()) as {
|
|
206
|
+
type?: string;
|
|
207
|
+
data?: { idToken?: string | null } | null;
|
|
208
|
+
idToken?: string | null;
|
|
209
|
+
};
|
|
210
|
+
if (result.type === "cancelled" || result.type === "noSavedCredentialFound") {
|
|
211
|
+
throw new SocialLoginCancelled("google");
|
|
212
|
+
}
|
|
213
|
+
idToken = (result.data?.idToken ?? result.idToken)?.trim() ?? "";
|
|
214
|
+
} catch (err) {
|
|
215
|
+
if (err instanceof SocialLoginCancelled) {
|
|
216
|
+
throw err;
|
|
217
|
+
}
|
|
218
|
+
const code = (err as { code?: string | number })?.code;
|
|
219
|
+
if (statusCodes?.SIGN_IN_CANCELLED != null && code === statusCodes.SIGN_IN_CANCELLED) {
|
|
220
|
+
throw new SocialLoginCancelled("google");
|
|
221
|
+
}
|
|
222
|
+
throw err;
|
|
223
|
+
}
|
|
224
|
+
if (!idToken) {
|
|
225
|
+
throw new Error("Google id_token missing");
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
try {
|
|
229
|
+
return await loginWithGoogle(ctx, {
|
|
230
|
+
idToken,
|
|
231
|
+
password: opts.password,
|
|
232
|
+
locale: opts.locale,
|
|
233
|
+
});
|
|
234
|
+
} catch (err) {
|
|
235
|
+
if (isOAuthEmailAlreadyExistsError(err)) {
|
|
236
|
+
throw new SocialLoginNeedsLink("google");
|
|
237
|
+
}
|
|
238
|
+
throw err;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
export type SignInWithAppleOptions = {
|
|
243
|
+
password?: string;
|
|
244
|
+
locale?: string;
|
|
245
|
+
appVersion?: string;
|
|
246
|
+
identityToken?: string;
|
|
247
|
+
authorizationCode?: string;
|
|
248
|
+
fullName?: string;
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
export async function signInWithApple(
|
|
252
|
+
ctx: PublicAuthContext,
|
|
253
|
+
opts: SignInWithAppleOptions = {},
|
|
254
|
+
): Promise<AuthTokens> {
|
|
255
|
+
if (Platform.OS !== "ios" && !opts.identityToken) {
|
|
256
|
+
throw new SocialLoginUnsupported("signInWithApple native UI is only available on iOS");
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
const status = await getSocialLoginStatus(ctx, { locale: opts.locale, appVersion: opts.appVersion });
|
|
260
|
+
if (!status.apple.enabled) {
|
|
261
|
+
throw new SocialLoginNotEnabled("apple");
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
const bundle = (ctx.bundleId?.trim() || appBundleId() || "").trim();
|
|
265
|
+
if (bundle && status.apple.bundleIds.length > 0 && !status.apple.bundleIds.includes(bundle)) {
|
|
266
|
+
// Soft warning path: still attempt; server validates aud.
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
let identityToken = opts.identityToken?.trim() ?? "";
|
|
270
|
+
let authorizationCode = opts.authorizationCode?.trim() ?? "";
|
|
271
|
+
let fullName = opts.fullName?.trim() ?? "";
|
|
272
|
+
|
|
273
|
+
if (!identityToken) {
|
|
274
|
+
const AppleAuthentication = await loadAppleAuthModule();
|
|
275
|
+
const available = await AppleAuthentication.isAvailableAsync();
|
|
276
|
+
if (!available) {
|
|
277
|
+
throw new SocialLoginUnsupported("Sign in with Apple is not available on this device");
|
|
278
|
+
}
|
|
279
|
+
try {
|
|
280
|
+
const cred = await AppleAuthentication.signInAsync({
|
|
281
|
+
requestedScopes: [
|
|
282
|
+
AppleAuthentication.AppleAuthenticationScope.FULL_NAME,
|
|
283
|
+
AppleAuthentication.AppleAuthenticationScope.EMAIL,
|
|
284
|
+
],
|
|
285
|
+
});
|
|
286
|
+
identityToken = cred.identityToken?.trim() ?? "";
|
|
287
|
+
authorizationCode = cred.authorizationCode?.trim() ?? "";
|
|
288
|
+
const first = cred.fullName?.givenName?.trim() ?? "";
|
|
289
|
+
const last = cred.fullName?.familyName?.trim() ?? "";
|
|
290
|
+
fullName = [first, last].filter(Boolean).join(" ");
|
|
291
|
+
} catch (err) {
|
|
292
|
+
const code = (err as { code?: string })?.code;
|
|
293
|
+
if (code === "ERR_REQUEST_CANCELED" || code === "ERR_CANCELED") {
|
|
294
|
+
throw new SocialLoginCancelled("apple");
|
|
295
|
+
}
|
|
296
|
+
throw err;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
if (!identityToken) {
|
|
301
|
+
throw new Error("Apple identity token missing");
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
try {
|
|
305
|
+
return await loginWithApple(ctx, {
|
|
306
|
+
identityToken,
|
|
307
|
+
authorizationCode: authorizationCode || undefined,
|
|
308
|
+
fullName: fullName || undefined,
|
|
309
|
+
password: opts.password,
|
|
310
|
+
locale: opts.locale,
|
|
311
|
+
});
|
|
312
|
+
} catch (err) {
|
|
313
|
+
if (isOAuthEmailAlreadyExistsError(err)) {
|
|
314
|
+
throw new SocialLoginNeedsLink("apple", { identityToken, authorizationCode, fullName });
|
|
315
|
+
}
|
|
316
|
+
throw err;
|
|
317
|
+
}
|
|
318
|
+
}
|