@oxyhq/core 1.0.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/README.md +50 -0
- package/dist/cjs/AuthManager.js +361 -0
- package/dist/cjs/CrossDomainAuth.js +258 -0
- package/dist/cjs/HttpService.js +618 -0
- package/dist/cjs/OxyServices.base.js +263 -0
- package/dist/cjs/OxyServices.errors.js +22 -0
- package/dist/cjs/OxyServices.js +63 -0
- package/dist/cjs/constants/version.js +16 -0
- package/dist/cjs/crypto/index.js +20 -0
- package/dist/cjs/crypto/keyManager.js +887 -0
- package/dist/cjs/crypto/polyfill.js +64 -0
- package/dist/cjs/crypto/recoveryPhrase.js +169 -0
- package/dist/cjs/crypto/signatureService.js +296 -0
- package/dist/cjs/i18n/index.js +73 -0
- package/dist/cjs/i18n/locales/ar-SA.json +120 -0
- package/dist/cjs/i18n/locales/ca-ES.json +120 -0
- package/dist/cjs/i18n/locales/de-DE.json +120 -0
- package/dist/cjs/i18n/locales/en-US.json +956 -0
- package/dist/cjs/i18n/locales/es-ES.json +944 -0
- package/dist/cjs/i18n/locales/fr-FR.json +120 -0
- package/dist/cjs/i18n/locales/it-IT.json +120 -0
- package/dist/cjs/i18n/locales/ja-JP.json +119 -0
- package/dist/cjs/i18n/locales/ko-KR.json +120 -0
- package/dist/cjs/i18n/locales/locales/ar-SA.json +120 -0
- package/dist/cjs/i18n/locales/locales/ca-ES.json +120 -0
- package/dist/cjs/i18n/locales/locales/de-DE.json +120 -0
- package/dist/cjs/i18n/locales/locales/en-US.json +956 -0
- package/dist/cjs/i18n/locales/locales/es-ES.json +944 -0
- package/dist/cjs/i18n/locales/locales/fr-FR.json +120 -0
- package/dist/cjs/i18n/locales/locales/it-IT.json +120 -0
- package/dist/cjs/i18n/locales/locales/ja-JP.json +119 -0
- package/dist/cjs/i18n/locales/locales/ko-KR.json +120 -0
- package/dist/cjs/i18n/locales/locales/pt-PT.json +120 -0
- package/dist/cjs/i18n/locales/locales/zh-CN.json +120 -0
- package/dist/cjs/i18n/locales/pt-PT.json +120 -0
- package/dist/cjs/i18n/locales/zh-CN.json +120 -0
- package/dist/cjs/index.js +153 -0
- package/dist/cjs/mixins/OxyServices.analytics.js +49 -0
- package/dist/cjs/mixins/OxyServices.assets.js +380 -0
- package/dist/cjs/mixins/OxyServices.auth.js +259 -0
- package/dist/cjs/mixins/OxyServices.developer.js +97 -0
- package/dist/cjs/mixins/OxyServices.devices.js +116 -0
- package/dist/cjs/mixins/OxyServices.features.js +309 -0
- package/dist/cjs/mixins/OxyServices.fedcm.js +435 -0
- package/dist/cjs/mixins/OxyServices.karma.js +108 -0
- package/dist/cjs/mixins/OxyServices.language.js +154 -0
- package/dist/cjs/mixins/OxyServices.location.js +43 -0
- package/dist/cjs/mixins/OxyServices.payment.js +158 -0
- package/dist/cjs/mixins/OxyServices.popup.js +371 -0
- package/dist/cjs/mixins/OxyServices.privacy.js +162 -0
- package/dist/cjs/mixins/OxyServices.redirect.js +345 -0
- package/dist/cjs/mixins/OxyServices.security.js +81 -0
- package/dist/cjs/mixins/OxyServices.user.js +355 -0
- package/dist/cjs/mixins/OxyServices.utility.js +156 -0
- package/dist/cjs/mixins/index.js +79 -0
- package/dist/cjs/mixins/mixinHelpers.js +53 -0
- package/dist/cjs/models/interfaces.js +20 -0
- package/dist/cjs/models/session.js +2 -0
- package/dist/cjs/shared/index.js +70 -0
- package/dist/cjs/shared/utils/colorUtils.js +153 -0
- package/dist/cjs/shared/utils/debugUtils.js +73 -0
- package/dist/cjs/shared/utils/errorUtils.js +183 -0
- package/dist/cjs/shared/utils/index.js +49 -0
- package/dist/cjs/shared/utils/networkUtils.js +183 -0
- package/dist/cjs/shared/utils/themeUtils.js +106 -0
- package/dist/cjs/utils/apiUtils.js +61 -0
- package/dist/cjs/utils/asyncUtils.js +194 -0
- package/dist/cjs/utils/cache.js +226 -0
- package/dist/cjs/utils/deviceManager.js +205 -0
- package/dist/cjs/utils/errorUtils.js +154 -0
- package/dist/cjs/utils/index.js +26 -0
- package/dist/cjs/utils/languageUtils.js +165 -0
- package/dist/cjs/utils/loggerUtils.js +126 -0
- package/dist/cjs/utils/platform.js +144 -0
- package/dist/cjs/utils/requestUtils.js +209 -0
- package/dist/cjs/utils/sessionUtils.js +181 -0
- package/dist/cjs/utils/validationUtils.js +173 -0
- package/dist/esm/AuthManager.js +356 -0
- package/dist/esm/CrossDomainAuth.js +253 -0
- package/dist/esm/HttpService.js +614 -0
- package/dist/esm/OxyServices.base.js +259 -0
- package/dist/esm/OxyServices.errors.js +17 -0
- package/dist/esm/OxyServices.js +59 -0
- package/dist/esm/constants/version.js +13 -0
- package/dist/esm/crypto/index.js +13 -0
- package/dist/esm/crypto/keyManager.js +850 -0
- package/dist/esm/crypto/polyfill.js +61 -0
- package/dist/esm/crypto/recoveryPhrase.js +132 -0
- package/dist/esm/crypto/signatureService.js +259 -0
- package/dist/esm/i18n/index.js +69 -0
- package/dist/esm/i18n/locales/ar-SA.json +120 -0
- package/dist/esm/i18n/locales/ca-ES.json +120 -0
- package/dist/esm/i18n/locales/de-DE.json +120 -0
- package/dist/esm/i18n/locales/en-US.json +956 -0
- package/dist/esm/i18n/locales/es-ES.json +944 -0
- package/dist/esm/i18n/locales/fr-FR.json +120 -0
- package/dist/esm/i18n/locales/it-IT.json +120 -0
- package/dist/esm/i18n/locales/ja-JP.json +119 -0
- package/dist/esm/i18n/locales/ko-KR.json +120 -0
- package/dist/esm/i18n/locales/locales/ar-SA.json +120 -0
- package/dist/esm/i18n/locales/locales/ca-ES.json +120 -0
- package/dist/esm/i18n/locales/locales/de-DE.json +120 -0
- package/dist/esm/i18n/locales/locales/en-US.json +956 -0
- package/dist/esm/i18n/locales/locales/es-ES.json +944 -0
- package/dist/esm/i18n/locales/locales/fr-FR.json +120 -0
- package/dist/esm/i18n/locales/locales/it-IT.json +120 -0
- package/dist/esm/i18n/locales/locales/ja-JP.json +119 -0
- package/dist/esm/i18n/locales/locales/ko-KR.json +120 -0
- package/dist/esm/i18n/locales/locales/pt-PT.json +120 -0
- package/dist/esm/i18n/locales/locales/zh-CN.json +120 -0
- package/dist/esm/i18n/locales/pt-PT.json +120 -0
- package/dist/esm/i18n/locales/zh-CN.json +120 -0
- package/dist/esm/index.js +55 -0
- package/dist/esm/mixins/OxyServices.analytics.js +46 -0
- package/dist/esm/mixins/OxyServices.assets.js +377 -0
- package/dist/esm/mixins/OxyServices.auth.js +256 -0
- package/dist/esm/mixins/OxyServices.developer.js +94 -0
- package/dist/esm/mixins/OxyServices.devices.js +113 -0
- package/dist/esm/mixins/OxyServices.features.js +306 -0
- package/dist/esm/mixins/OxyServices.fedcm.js +433 -0
- package/dist/esm/mixins/OxyServices.karma.js +105 -0
- package/dist/esm/mixins/OxyServices.language.js +118 -0
- package/dist/esm/mixins/OxyServices.location.js +40 -0
- package/dist/esm/mixins/OxyServices.payment.js +155 -0
- package/dist/esm/mixins/OxyServices.popup.js +369 -0
- package/dist/esm/mixins/OxyServices.privacy.js +159 -0
- package/dist/esm/mixins/OxyServices.redirect.js +343 -0
- package/dist/esm/mixins/OxyServices.security.js +78 -0
- package/dist/esm/mixins/OxyServices.user.js +352 -0
- package/dist/esm/mixins/OxyServices.utility.js +153 -0
- package/dist/esm/mixins/index.js +76 -0
- package/dist/esm/mixins/mixinHelpers.js +48 -0
- package/dist/esm/models/interfaces.js +17 -0
- package/dist/esm/models/session.js +1 -0
- package/dist/esm/shared/index.js +31 -0
- package/dist/esm/shared/utils/colorUtils.js +143 -0
- package/dist/esm/shared/utils/debugUtils.js +65 -0
- package/dist/esm/shared/utils/errorUtils.js +170 -0
- package/dist/esm/shared/utils/index.js +15 -0
- package/dist/esm/shared/utils/networkUtils.js +173 -0
- package/dist/esm/shared/utils/themeUtils.js +98 -0
- package/dist/esm/utils/apiUtils.js +55 -0
- package/dist/esm/utils/asyncUtils.js +179 -0
- package/dist/esm/utils/cache.js +218 -0
- package/dist/esm/utils/deviceManager.js +168 -0
- package/dist/esm/utils/errorUtils.js +146 -0
- package/dist/esm/utils/index.js +7 -0
- package/dist/esm/utils/languageUtils.js +158 -0
- package/dist/esm/utils/loggerUtils.js +115 -0
- package/dist/esm/utils/platform.js +102 -0
- package/dist/esm/utils/requestUtils.js +203 -0
- package/dist/esm/utils/sessionUtils.js +171 -0
- package/dist/esm/utils/validationUtils.js +153 -0
- package/dist/types/AuthManager.d.ts +143 -0
- package/dist/types/CrossDomainAuth.d.ts +160 -0
- package/dist/types/HttpService.d.ts +163 -0
- package/dist/types/OxyServices.base.d.ts +126 -0
- package/dist/types/OxyServices.d.ts +81 -0
- package/dist/types/OxyServices.errors.d.ts +11 -0
- package/dist/types/constants/version.d.ts +13 -0
- package/dist/types/crypto/index.d.ts +11 -0
- package/dist/types/crypto/keyManager.d.ts +189 -0
- package/dist/types/crypto/polyfill.d.ts +11 -0
- package/dist/types/crypto/recoveryPhrase.d.ts +58 -0
- package/dist/types/crypto/signatureService.d.ts +86 -0
- package/dist/types/i18n/index.d.ts +3 -0
- package/dist/types/index.d.ts +50 -0
- package/dist/types/mixins/OxyServices.analytics.d.ts +66 -0
- package/dist/types/mixins/OxyServices.assets.d.ts +135 -0
- package/dist/types/mixins/OxyServices.auth.d.ts +186 -0
- package/dist/types/mixins/OxyServices.developer.d.ts +99 -0
- package/dist/types/mixins/OxyServices.devices.d.ts +96 -0
- package/dist/types/mixins/OxyServices.features.d.ts +228 -0
- package/dist/types/mixins/OxyServices.fedcm.d.ts +200 -0
- package/dist/types/mixins/OxyServices.karma.d.ts +85 -0
- package/dist/types/mixins/OxyServices.language.d.ts +81 -0
- package/dist/types/mixins/OxyServices.location.d.ts +64 -0
- package/dist/types/mixins/OxyServices.payment.d.ts +111 -0
- package/dist/types/mixins/OxyServices.popup.d.ts +205 -0
- package/dist/types/mixins/OxyServices.privacy.d.ts +122 -0
- package/dist/types/mixins/OxyServices.redirect.d.ts +245 -0
- package/dist/types/mixins/OxyServices.security.d.ts +78 -0
- package/dist/types/mixins/OxyServices.user.d.ts +182 -0
- package/dist/types/mixins/OxyServices.utility.d.ts +93 -0
- package/dist/types/mixins/index.d.ts +30 -0
- package/dist/types/mixins/mixinHelpers.d.ts +31 -0
- package/dist/types/models/interfaces.d.ts +415 -0
- package/dist/types/models/session.d.ts +27 -0
- package/dist/types/shared/index.d.ts +28 -0
- package/dist/types/shared/utils/colorUtils.d.ts +104 -0
- package/dist/types/shared/utils/debugUtils.d.ts +48 -0
- package/dist/types/shared/utils/errorUtils.d.ts +97 -0
- package/dist/types/shared/utils/index.d.ts +13 -0
- package/dist/types/shared/utils/networkUtils.d.ts +139 -0
- package/dist/types/shared/utils/themeUtils.d.ts +90 -0
- package/dist/types/utils/apiUtils.d.ts +53 -0
- package/dist/types/utils/asyncUtils.d.ts +58 -0
- package/dist/types/utils/cache.d.ts +127 -0
- package/dist/types/utils/deviceManager.d.ts +65 -0
- package/dist/types/utils/errorUtils.d.ts +46 -0
- package/dist/types/utils/index.d.ts +6 -0
- package/dist/types/utils/languageUtils.d.ts +37 -0
- package/dist/types/utils/loggerUtils.d.ts +48 -0
- package/dist/types/utils/platform.d.ts +40 -0
- package/dist/types/utils/requestUtils.d.ts +123 -0
- package/dist/types/utils/sessionUtils.d.ts +54 -0
- package/dist/types/utils/validationUtils.d.ts +85 -0
- package/package.json +84 -0
- package/src/AuthManager.ts +436 -0
- package/src/CrossDomainAuth.ts +307 -0
- package/src/HttpService.ts +752 -0
- package/src/OxyServices.base.ts +334 -0
- package/src/OxyServices.errors.ts +26 -0
- package/src/OxyServices.ts +129 -0
- package/src/constants/version.ts +15 -0
- package/src/crypto/index.ts +25 -0
- package/src/crypto/keyManager.ts +962 -0
- package/src/crypto/polyfill.ts +70 -0
- package/src/crypto/recoveryPhrase.ts +166 -0
- package/src/crypto/signatureService.ts +323 -0
- package/src/i18n/index.ts +75 -0
- package/src/i18n/locales/ar-SA.json +120 -0
- package/src/i18n/locales/ca-ES.json +120 -0
- package/src/i18n/locales/de-DE.json +120 -0
- package/src/i18n/locales/en-US.json +956 -0
- package/src/i18n/locales/es-ES.json +944 -0
- package/src/i18n/locales/fr-FR.json +120 -0
- package/src/i18n/locales/it-IT.json +120 -0
- package/src/i18n/locales/ja-JP.json +119 -0
- package/src/i18n/locales/ko-KR.json +120 -0
- package/src/i18n/locales/pt-PT.json +120 -0
- package/src/i18n/locales/zh-CN.json +120 -0
- package/src/index.ts +153 -0
- package/src/mixins/OxyServices.analytics.ts +53 -0
- package/src/mixins/OxyServices.assets.ts +412 -0
- package/src/mixins/OxyServices.auth.ts +358 -0
- package/src/mixins/OxyServices.developer.ts +114 -0
- package/src/mixins/OxyServices.devices.ts +119 -0
- package/src/mixins/OxyServices.features.ts +428 -0
- package/src/mixins/OxyServices.fedcm.ts +494 -0
- package/src/mixins/OxyServices.karma.ts +111 -0
- package/src/mixins/OxyServices.language.ts +127 -0
- package/src/mixins/OxyServices.location.ts +46 -0
- package/src/mixins/OxyServices.payment.ts +163 -0
- package/src/mixins/OxyServices.popup.ts +443 -0
- package/src/mixins/OxyServices.privacy.ts +182 -0
- package/src/mixins/OxyServices.redirect.ts +397 -0
- package/src/mixins/OxyServices.security.ts +103 -0
- package/src/mixins/OxyServices.user.ts +392 -0
- package/src/mixins/OxyServices.utility.ts +191 -0
- package/src/mixins/index.ts +91 -0
- package/src/mixins/mixinHelpers.ts +69 -0
- package/src/models/interfaces.ts +511 -0
- package/src/models/session.ts +30 -0
- package/src/shared/index.ts +82 -0
- package/src/shared/utils/colorUtils.ts +155 -0
- package/src/shared/utils/debugUtils.ts +73 -0
- package/src/shared/utils/errorUtils.ts +181 -0
- package/src/shared/utils/index.ts +59 -0
- package/src/shared/utils/networkUtils.ts +248 -0
- package/src/shared/utils/themeUtils.ts +115 -0
- package/src/types/bip39.d.ts +32 -0
- package/src/types/buffer.d.ts +97 -0
- package/src/types/color.d.ts +20 -0
- package/src/types/elliptic.d.ts +62 -0
- package/src/utils/apiUtils.ts +88 -0
- package/src/utils/asyncUtils.ts +252 -0
- package/src/utils/cache.ts +264 -0
- package/src/utils/deviceManager.ts +198 -0
- package/src/utils/errorUtils.ts +216 -0
- package/src/utils/index.ts +21 -0
- package/src/utils/languageUtils.ts +174 -0
- package/src/utils/loggerUtils.ts +153 -0
- package/src/utils/platform.ts +117 -0
- package/src/utils/requestUtils.ts +237 -0
- package/src/utils/sessionUtils.ts +206 -0
- package/src/utils/validationUtils.ts +174 -0
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cross-Domain Authentication Helper
|
|
3
|
+
*
|
|
4
|
+
* Provides a simplified API for cross-domain SSO authentication that automatically
|
|
5
|
+
* selects the best authentication method based on browser capabilities:
|
|
6
|
+
*
|
|
7
|
+
* 1. FedCM (if supported) - Modern, Google-style browser-native auth
|
|
8
|
+
* 2. Popup (fallback) - OAuth2-style popup window
|
|
9
|
+
* 3. Redirect (final fallback) - Traditional full-page redirect
|
|
10
|
+
*
|
|
11
|
+
* Usage:
|
|
12
|
+
* ```typescript
|
|
13
|
+
* import { CrossDomainAuth } from '@oxyhq/services';
|
|
14
|
+
*
|
|
15
|
+
* const auth = new CrossDomainAuth(oxyServices);
|
|
16
|
+
*
|
|
17
|
+
* // Automatic method selection
|
|
18
|
+
* const session = await auth.signIn();
|
|
19
|
+
*
|
|
20
|
+
* // Or use specific method
|
|
21
|
+
* const session = await auth.signInWithPopup();
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import type { OxyServices } from './OxyServices';
|
|
26
|
+
import type { SessionLoginResponse } from './models/session';
|
|
27
|
+
|
|
28
|
+
export interface CrossDomainAuthOptions {
|
|
29
|
+
/**
|
|
30
|
+
* Preferred authentication method
|
|
31
|
+
* - 'auto': Automatically select best method (default)
|
|
32
|
+
* - 'fedcm': Use FedCM (browser-native)
|
|
33
|
+
* - 'popup': Use popup window
|
|
34
|
+
* - 'redirect': Use full-page redirect
|
|
35
|
+
*/
|
|
36
|
+
method?: 'auto' | 'fedcm' | 'popup' | 'redirect';
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Custom redirect URI (for redirect method)
|
|
40
|
+
*/
|
|
41
|
+
redirectUri?: string;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Whether to open signup page instead of login
|
|
45
|
+
*/
|
|
46
|
+
isSignup?: boolean;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Popup window dimensions (for popup method)
|
|
50
|
+
*/
|
|
51
|
+
popupDimensions?: {
|
|
52
|
+
width?: number;
|
|
53
|
+
height?: number;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Callback when auth method is selected
|
|
58
|
+
*/
|
|
59
|
+
onMethodSelected?: (method: 'fedcm' | 'popup' | 'redirect') => void;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export class CrossDomainAuth {
|
|
63
|
+
constructor(private oxyServices: OxyServices) {}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Sign in with automatic method selection
|
|
67
|
+
*
|
|
68
|
+
* Tries methods in this order:
|
|
69
|
+
* 1. FedCM (if supported and not in private browsing)
|
|
70
|
+
* 2. Popup (if not blocked)
|
|
71
|
+
* 3. Redirect (always works)
|
|
72
|
+
*
|
|
73
|
+
* @param options - Authentication options
|
|
74
|
+
* @returns Session with user data and access token
|
|
75
|
+
*/
|
|
76
|
+
async signIn(options: CrossDomainAuthOptions = {}): Promise<SessionLoginResponse | null> {
|
|
77
|
+
const method = options.method || 'auto';
|
|
78
|
+
|
|
79
|
+
// If specific method requested, use it directly
|
|
80
|
+
if (method === 'fedcm') {
|
|
81
|
+
return this.signInWithFedCM(options);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (method === 'popup') {
|
|
85
|
+
return this.signInWithPopup(options);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (method === 'redirect') {
|
|
89
|
+
this.signInWithRedirect(options);
|
|
90
|
+
return null; // Redirect doesn't return immediately
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Auto mode: Try methods in order of preference
|
|
94
|
+
return this.autoSignIn(options);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Automatic sign-in with progressive enhancement
|
|
99
|
+
*
|
|
100
|
+
* @private
|
|
101
|
+
*/
|
|
102
|
+
private async autoSignIn(options: CrossDomainAuthOptions): Promise<SessionLoginResponse | null> {
|
|
103
|
+
// 1. Try FedCM first (best UX, most modern)
|
|
104
|
+
if (this.isFedCMSupported()) {
|
|
105
|
+
try {
|
|
106
|
+
options.onMethodSelected?.('fedcm');
|
|
107
|
+
return await this.signInWithFedCM(options);
|
|
108
|
+
} catch (error) {
|
|
109
|
+
console.warn('[CrossDomainAuth] FedCM failed, trying popup...', error);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// 2. Try popup (good UX, widely supported)
|
|
114
|
+
try {
|
|
115
|
+
options.onMethodSelected?.('popup');
|
|
116
|
+
return await this.signInWithPopup(options);
|
|
117
|
+
} catch (error) {
|
|
118
|
+
console.warn('[CrossDomainAuth] Popup failed, falling back to redirect...', error);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// 3. Fallback to redirect (always works)
|
|
122
|
+
options.onMethodSelected?.('redirect');
|
|
123
|
+
this.signInWithRedirect(options);
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Sign in using FedCM (Federated Credential Management)
|
|
129
|
+
*
|
|
130
|
+
* Best method - browser-native, no popups, Google-like experience
|
|
131
|
+
*/
|
|
132
|
+
async signInWithFedCM(options: CrossDomainAuthOptions = {}): Promise<SessionLoginResponse> {
|
|
133
|
+
return (this.oxyServices as any).signInWithFedCM({
|
|
134
|
+
context: options.isSignup ? 'signup' : 'signin',
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Sign in using popup window
|
|
140
|
+
*
|
|
141
|
+
* Good method - preserves app state, no full page reload
|
|
142
|
+
*/
|
|
143
|
+
async signInWithPopup(options: CrossDomainAuthOptions = {}): Promise<SessionLoginResponse> {
|
|
144
|
+
return (this.oxyServices as any).signInWithPopup({
|
|
145
|
+
mode: options.isSignup ? 'signup' : 'login',
|
|
146
|
+
width: options.popupDimensions?.width,
|
|
147
|
+
height: options.popupDimensions?.height,
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Sign in using full-page redirect
|
|
153
|
+
*
|
|
154
|
+
* Fallback method - works everywhere but loses app state
|
|
155
|
+
*/
|
|
156
|
+
signInWithRedirect(options: CrossDomainAuthOptions = {}): void {
|
|
157
|
+
(this.oxyServices as any).signInWithRedirect({
|
|
158
|
+
redirectUri: options.redirectUri,
|
|
159
|
+
mode: options.isSignup ? 'signup' : 'login',
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Handle redirect callback
|
|
165
|
+
*
|
|
166
|
+
* Call this on app startup to check if we're returning from auth redirect
|
|
167
|
+
*/
|
|
168
|
+
handleRedirectCallback(): SessionLoginResponse | null {
|
|
169
|
+
return (this.oxyServices as any).handleAuthCallback();
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Silent sign-in (check for existing session)
|
|
174
|
+
*
|
|
175
|
+
* Tries to automatically sign in without user interaction.
|
|
176
|
+
* Works with both FedCM and popup/iframe methods.
|
|
177
|
+
*
|
|
178
|
+
* @returns Session if user is already signed in, null otherwise
|
|
179
|
+
*/
|
|
180
|
+
async silentSignIn(): Promise<SessionLoginResponse | null> {
|
|
181
|
+
// Try FedCM silent sign-in first (if supported)
|
|
182
|
+
if (this.isFedCMSupported()) {
|
|
183
|
+
try {
|
|
184
|
+
const session = await (this.oxyServices as any).silentSignInWithFedCM();
|
|
185
|
+
if (session) {
|
|
186
|
+
return session;
|
|
187
|
+
}
|
|
188
|
+
} catch (error) {
|
|
189
|
+
console.warn('[CrossDomainAuth] FedCM silent sign-in failed:', error);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// Fallback to iframe-based silent auth
|
|
194
|
+
try {
|
|
195
|
+
return await (this.oxyServices as any).silentSignIn();
|
|
196
|
+
} catch (error) {
|
|
197
|
+
console.warn('[CrossDomainAuth] Silent sign-in failed:', error);
|
|
198
|
+
return null;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Restore session from storage
|
|
204
|
+
*
|
|
205
|
+
* For redirect method - restores previously authenticated session from localStorage
|
|
206
|
+
*/
|
|
207
|
+
restoreSession(): boolean {
|
|
208
|
+
return (this.oxyServices as any).restoreSession?.() || false;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Check if FedCM is supported in current browser
|
|
213
|
+
*/
|
|
214
|
+
isFedCMSupported(): boolean {
|
|
215
|
+
return (this.oxyServices as any).constructor.isFedCMSupported?.() || false;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Get recommended authentication method for current environment
|
|
220
|
+
*
|
|
221
|
+
* @returns Recommended method name and reason
|
|
222
|
+
*/
|
|
223
|
+
getRecommendedMethod(): { method: 'fedcm' | 'popup' | 'redirect'; reason: string } {
|
|
224
|
+
if (this.isFedCMSupported()) {
|
|
225
|
+
return {
|
|
226
|
+
method: 'fedcm',
|
|
227
|
+
reason: 'FedCM is supported - provides best UX with browser-native auth',
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
if (typeof window !== 'undefined') {
|
|
232
|
+
return {
|
|
233
|
+
method: 'popup',
|
|
234
|
+
reason: 'Browser environment - popup preserves app state',
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
return {
|
|
239
|
+
method: 'redirect',
|
|
240
|
+
reason: 'Fallback method - works in all environments',
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Initialize cross-domain auth on app startup
|
|
246
|
+
*
|
|
247
|
+
* This handles:
|
|
248
|
+
* 1. Redirect callback (if returning from auth.oxy.so)
|
|
249
|
+
* 2. Session restoration (from localStorage)
|
|
250
|
+
* 3. Silent sign-in (check for existing SSO session)
|
|
251
|
+
*
|
|
252
|
+
* @returns Session if user is authenticated, null otherwise
|
|
253
|
+
*/
|
|
254
|
+
async initialize(): Promise<SessionLoginResponse | null> {
|
|
255
|
+
// 1. Check if this is a redirect callback
|
|
256
|
+
const callbackSession = this.handleRedirectCallback();
|
|
257
|
+
if (callbackSession) {
|
|
258
|
+
return callbackSession;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// 2. Try to restore existing session from storage
|
|
262
|
+
const restored = this.restoreSession();
|
|
263
|
+
if (restored) {
|
|
264
|
+
// Verify session is still valid by fetching user
|
|
265
|
+
try {
|
|
266
|
+
const user = await (this.oxyServices as any).getCurrentUser();
|
|
267
|
+
if (user) {
|
|
268
|
+
return {
|
|
269
|
+
sessionId: (this.oxyServices as any).getStoredSessionId?.() || '',
|
|
270
|
+
deviceId: '',
|
|
271
|
+
expiresAt: new Date(Date.now() + 24 * 60 * 60 * 1000).toISOString(),
|
|
272
|
+
user,
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
} catch (error) {
|
|
276
|
+
console.warn('[CrossDomainAuth] Stored session invalid:', error);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// 3. Try silent sign-in (check for SSO session at auth.oxy.so)
|
|
281
|
+
return await this.silentSignIn();
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Helper function to create CrossDomainAuth instance
|
|
287
|
+
*
|
|
288
|
+
* @example
|
|
289
|
+
* ```typescript
|
|
290
|
+
* import { createCrossDomainAuth } from '@oxyhq/services';
|
|
291
|
+
*
|
|
292
|
+
* const oxyServices = new OxyServices({ baseURL: 'https://api.oxy.so' });
|
|
293
|
+
* const auth = createCrossDomainAuth(oxyServices);
|
|
294
|
+
*
|
|
295
|
+
* // On app startup
|
|
296
|
+
* const session = await auth.initialize();
|
|
297
|
+
* if (session) {
|
|
298
|
+
* console.log('User is signed in:', session.user);
|
|
299
|
+
* }
|
|
300
|
+
*
|
|
301
|
+
* // Sign in button click
|
|
302
|
+
* const session = await auth.signIn();
|
|
303
|
+
* ```
|
|
304
|
+
*/
|
|
305
|
+
export function createCrossDomainAuth(oxyServices: OxyServices): CrossDomainAuth {
|
|
306
|
+
return new CrossDomainAuth(oxyServices);
|
|
307
|
+
}
|