@dynamic-labs/sdk-react-core 2.5.3 → 2.6.1
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/package.cjs +2 -2
- package/package.js +2 -2
- package/package.json +14 -14
- package/src/lib/components/QRCode/QRCode.d.ts +15 -0
- package/src/lib/components/UserProfile/parts/UserProfileSocialAccount/UserProfileSocialAccount.cjs +1 -1
- package/src/lib/components/UserProfile/parts/UserProfileSocialAccount/UserProfileSocialAccount.js +1 -1
- package/src/lib/config/ApiEndpoint.cjs +6 -20
- package/src/lib/config/ApiEndpoint.d.ts +1 -2
- package/src/lib/config/ApiEndpoint.js +6 -20
- package/src/lib/context/DynamicContext/DynamicContext.cjs +2 -0
- package/src/lib/context/DynamicContext/DynamicContext.d.ts +2 -0
- package/src/lib/context/DynamicContext/DynamicContext.js +2 -0
- package/src/lib/context/DynamicContext/types/SocialSettings.d.ts +7 -0
- package/src/lib/context/DynamicContext/types/index.d.ts +2 -0
- package/src/lib/context/SocialRedirectContext/SocialRedirectContext.cjs +4 -0
- package/src/lib/context/SocialRedirectContext/SocialRedirectContext.js +4 -0
- package/src/lib/context/SocialRedirectContext/hooks/useRedirectSocialHandler/index.d.ts +1 -0
- package/src/lib/context/SocialRedirectContext/hooks/useRedirectSocialHandler/useRedirectSocialHandler.cjs +150 -0
- package/src/lib/context/SocialRedirectContext/hooks/useRedirectSocialHandler/useRedirectSocialHandler.d.ts +7 -0
- package/src/lib/context/SocialRedirectContext/hooks/useRedirectSocialHandler/useRedirectSocialHandler.js +146 -0
- package/src/lib/data/api/embeddedWallets/embeddedWallets.cjs +1 -1
- package/src/lib/data/api/embeddedWallets/embeddedWallets.js +1 -1
- package/src/lib/data/api/oauth/oauth.cjs +13 -0
- package/src/lib/data/api/oauth/oauth.d.ts +2 -1
- package/src/lib/data/api/oauth/oauth.js +13 -1
- package/src/lib/shared/utils/hooks/index.d.ts +1 -0
- package/src/lib/shared/utils/hooks/useEffectOnceCondition/index.d.ts +1 -0
- package/src/lib/shared/utils/hooks/useEffectOnceCondition/useEffectOnceCondition.cjs +26 -0
- package/src/lib/shared/utils/hooks/useEffectOnceCondition/useEffectOnceCondition.d.ts +7 -0
- package/src/lib/shared/utils/hooks/useEffectOnceCondition/useEffectOnceCondition.js +22 -0
- package/src/lib/styles/index.shadow.cjs +1 -1
- package/src/lib/styles/index.shadow.js +1 -1
- package/src/lib/utils/functions/index.d.ts +1 -0
- package/src/lib/utils/functions/socialStorage/index.d.ts +1 -0
- package/src/lib/utils/functions/socialStorage/socialStorage.cjs +36 -0
- package/src/lib/utils/functions/socialStorage/socialStorage.d.ts +17 -0
- package/src/lib/utils/functions/socialStorage/socialStorage.js +29 -0
- package/src/lib/utils/hooks/useSocialAuth/apple/connectWithAppleId.cjs +26 -0
- package/src/lib/utils/hooks/useSocialAuth/apple/connectWithAppleId.d.ts +7 -0
- package/src/lib/utils/hooks/useSocialAuth/apple/connectWithAppleId.js +22 -0
- package/src/lib/utils/hooks/useSocialAuth/apple/loadAppleId.cjs +34 -0
- package/src/lib/utils/hooks/useSocialAuth/apple/loadAppleId.d.ts +1 -0
- package/src/lib/utils/hooks/useSocialAuth/apple/loadAppleId.js +30 -0
- package/src/lib/utils/hooks/useSocialAuth/index.d.ts +1 -1
- package/src/lib/utils/hooks/useSocialAuth/useSocialAuth.cjs +85 -27
- package/src/lib/utils/hooks/useSocialAuth/useSocialAuth.d.ts +9 -1
- package/src/lib/utils/hooks/useSocialAuth/useSocialAuth.js +90 -32
- package/src/lib/widgets/DynamicEmbeddedWidget/DynamicEmbeddedAuthFlow/DynamicEmbeddedAuthFlow.cjs +1 -2
- package/src/lib/widgets/DynamicEmbeddedWidget/DynamicEmbeddedAuthFlow/DynamicEmbeddedAuthFlow.js +1 -2
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './socialStorage';
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
+
|
|
6
|
+
var localStorage = require('../../../shared/utils/classes/storage/localStorage.cjs');
|
|
7
|
+
|
|
8
|
+
const SOCIAL_STORAGE_KEY = 'dynamic_social_storage';
|
|
9
|
+
const searchSocialStorageByState = (targetState) => {
|
|
10
|
+
var _a;
|
|
11
|
+
const socialStorage = localStorage.LocalStorage.getFromLS(SOCIAL_STORAGE_KEY);
|
|
12
|
+
if (!socialStorage)
|
|
13
|
+
return undefined;
|
|
14
|
+
const storageEntries = Object.entries(socialStorage);
|
|
15
|
+
const [provider] = (_a = storageEntries.find(([, { state }]) => state === targetState)) !== null && _a !== void 0 ? _a : [];
|
|
16
|
+
if (!provider)
|
|
17
|
+
return undefined;
|
|
18
|
+
return socialStorage[provider];
|
|
19
|
+
};
|
|
20
|
+
const setSocialStorageFor = (provider, socialStorageItem) => {
|
|
21
|
+
var _a;
|
|
22
|
+
const socialStorage = (_a = localStorage.LocalStorage.getFromLS(SOCIAL_STORAGE_KEY)) !== null && _a !== void 0 ? _a : {};
|
|
23
|
+
socialStorage[provider] = Object.assign({ provider }, socialStorageItem);
|
|
24
|
+
localStorage.LocalStorage.setToLS(SOCIAL_STORAGE_KEY, socialStorage);
|
|
25
|
+
};
|
|
26
|
+
const deleteSocialStorage = (provider) => {
|
|
27
|
+
var _a;
|
|
28
|
+
const socialStorage = (_a = localStorage.LocalStorage.getFromLS(SOCIAL_STORAGE_KEY)) !== null && _a !== void 0 ? _a : {};
|
|
29
|
+
delete socialStorage[provider];
|
|
30
|
+
localStorage.LocalStorage.setToLS(SOCIAL_STORAGE_KEY, socialStorage);
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
exports.SOCIAL_STORAGE_KEY = SOCIAL_STORAGE_KEY;
|
|
34
|
+
exports.deleteSocialStorage = deleteSocialStorage;
|
|
35
|
+
exports.searchSocialStorageByState = searchSocialStorageByState;
|
|
36
|
+
exports.setSocialStorageFor = setSocialStorageFor;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { ProviderEnum } from '@dynamic-labs/sdk-api-core';
|
|
2
|
+
export declare const SOCIAL_STORAGE_KEY = "dynamic_social_storage";
|
|
3
|
+
export type SocialStorageItem = {
|
|
4
|
+
provider: ProviderEnum;
|
|
5
|
+
/** State issued to IDP on social authentication */
|
|
6
|
+
state: string;
|
|
7
|
+
/** Code verifier used to generate the code challenge for PKCE */
|
|
8
|
+
codeVerifier: string | undefined;
|
|
9
|
+
/** Captcha token used to verify the user is not a bot */
|
|
10
|
+
captchaToken?: string;
|
|
11
|
+
/** Oauth code from redirect from oauth provider, this will be provided on response from redirect endpoint **/
|
|
12
|
+
code?: string;
|
|
13
|
+
};
|
|
14
|
+
export type SocialStorage = Partial<Record<ProviderEnum, SocialStorageItem>>;
|
|
15
|
+
export declare const searchSocialStorageByState: (targetState: string) => SocialStorageItem | undefined;
|
|
16
|
+
export declare const setSocialStorageFor: (provider: ProviderEnum, socialStorageItem: Omit<SocialStorageItem, 'provider'>) => void;
|
|
17
|
+
export declare const deleteSocialStorage: (provider: ProviderEnum) => void;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
import { LocalStorage } from '../../../shared/utils/classes/storage/localStorage.js';
|
|
3
|
+
|
|
4
|
+
const SOCIAL_STORAGE_KEY = 'dynamic_social_storage';
|
|
5
|
+
const searchSocialStorageByState = (targetState) => {
|
|
6
|
+
var _a;
|
|
7
|
+
const socialStorage = LocalStorage.getFromLS(SOCIAL_STORAGE_KEY);
|
|
8
|
+
if (!socialStorage)
|
|
9
|
+
return undefined;
|
|
10
|
+
const storageEntries = Object.entries(socialStorage);
|
|
11
|
+
const [provider] = (_a = storageEntries.find(([, { state }]) => state === targetState)) !== null && _a !== void 0 ? _a : [];
|
|
12
|
+
if (!provider)
|
|
13
|
+
return undefined;
|
|
14
|
+
return socialStorage[provider];
|
|
15
|
+
};
|
|
16
|
+
const setSocialStorageFor = (provider, socialStorageItem) => {
|
|
17
|
+
var _a;
|
|
18
|
+
const socialStorage = (_a = LocalStorage.getFromLS(SOCIAL_STORAGE_KEY)) !== null && _a !== void 0 ? _a : {};
|
|
19
|
+
socialStorage[provider] = Object.assign({ provider }, socialStorageItem);
|
|
20
|
+
LocalStorage.setToLS(SOCIAL_STORAGE_KEY, socialStorage);
|
|
21
|
+
};
|
|
22
|
+
const deleteSocialStorage = (provider) => {
|
|
23
|
+
var _a;
|
|
24
|
+
const socialStorage = (_a = LocalStorage.getFromLS(SOCIAL_STORAGE_KEY)) !== null && _a !== void 0 ? _a : {};
|
|
25
|
+
delete socialStorage[provider];
|
|
26
|
+
LocalStorage.setToLS(SOCIAL_STORAGE_KEY, socialStorage);
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export { SOCIAL_STORAGE_KEY, deleteSocialStorage, searchSocialStorageByState, setSocialStorageFor };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
+
|
|
6
|
+
var _tslib = require('../../../../../../_virtual/_tslib.cjs');
|
|
7
|
+
var loadAppleId = require('./loadAppleId.cjs');
|
|
8
|
+
|
|
9
|
+
const connectWithAppleId = (_a) => _tslib.__awaiter(void 0, [_a], void 0, function* ({ clientId, oauthLoginUrl, state, }) {
|
|
10
|
+
yield loadAppleId.loadAppleId();
|
|
11
|
+
// Because the AppleID is loaded from a script tag, there is not type for it
|
|
12
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
13
|
+
// @ts-ignore
|
|
14
|
+
AppleID.auth.init({
|
|
15
|
+
clientId,
|
|
16
|
+
redirectURI: oauthLoginUrl.searchParams.get('redirect_uri'),
|
|
17
|
+
scope: 'name email',
|
|
18
|
+
state,
|
|
19
|
+
usePopup: false,
|
|
20
|
+
});
|
|
21
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
22
|
+
// @ts-ignore
|
|
23
|
+
AppleID.auth.signIn();
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
exports.connectWithAppleId = connectWithAppleId;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
import { __awaiter } from '../../../../../../_virtual/_tslib.js';
|
|
3
|
+
import { loadAppleId } from './loadAppleId.js';
|
|
4
|
+
|
|
5
|
+
const connectWithAppleId = (_a) => __awaiter(void 0, [_a], void 0, function* ({ clientId, oauthLoginUrl, state, }) {
|
|
6
|
+
yield loadAppleId();
|
|
7
|
+
// Because the AppleID is loaded from a script tag, there is not type for it
|
|
8
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
9
|
+
// @ts-ignore
|
|
10
|
+
AppleID.auth.init({
|
|
11
|
+
clientId,
|
|
12
|
+
redirectURI: oauthLoginUrl.searchParams.get('redirect_uri'),
|
|
13
|
+
scope: 'name email',
|
|
14
|
+
state,
|
|
15
|
+
usePopup: false,
|
|
16
|
+
});
|
|
17
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
18
|
+
// @ts-ignore
|
|
19
|
+
AppleID.auth.signIn();
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
export { connectWithAppleId };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
+
|
|
6
|
+
const appleIdScriptSrc = 'https://appleid.cdn-apple.com/appleauth/static/jsapi/appleid/1/en_US/appleid.auth.js';
|
|
7
|
+
const loadAppleId = () => new Promise((resolve, reject) => {
|
|
8
|
+
const script = document.querySelector('script[data-apple-sdk]');
|
|
9
|
+
if (script) {
|
|
10
|
+
// Script already exists, attach event listeners
|
|
11
|
+
if (script.hasAttribute('data-loaded')) {
|
|
12
|
+
resolve();
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
script.addEventListener('load', () => resolve());
|
|
16
|
+
script.addEventListener('error', () => reject(new Error('Failed to load Apple SDK')));
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
// If the script does not exist, create and add it
|
|
21
|
+
const script = document.createElement('script');
|
|
22
|
+
script.type = 'text/javascript';
|
|
23
|
+
script.src = appleIdScriptSrc;
|
|
24
|
+
script.setAttribute('data-apple-sdk', 'true');
|
|
25
|
+
script.onload = () => {
|
|
26
|
+
script === null || script === void 0 ? void 0 : script.setAttribute('data-loaded', 'true');
|
|
27
|
+
resolve();
|
|
28
|
+
};
|
|
29
|
+
script.onerror = () => reject(new Error('Failed to load Apple SDK'));
|
|
30
|
+
window.document.head.appendChild(script);
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
exports.loadAppleId = loadAppleId;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const loadAppleId: () => Promise<void>;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
const appleIdScriptSrc = 'https://appleid.cdn-apple.com/appleauth/static/jsapi/appleid/1/en_US/appleid.auth.js';
|
|
3
|
+
const loadAppleId = () => new Promise((resolve, reject) => {
|
|
4
|
+
const script = document.querySelector('script[data-apple-sdk]');
|
|
5
|
+
if (script) {
|
|
6
|
+
// Script already exists, attach event listeners
|
|
7
|
+
if (script.hasAttribute('data-loaded')) {
|
|
8
|
+
resolve();
|
|
9
|
+
}
|
|
10
|
+
else {
|
|
11
|
+
script.addEventListener('load', () => resolve());
|
|
12
|
+
script.addEventListener('error', () => reject(new Error('Failed to load Apple SDK')));
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
// If the script does not exist, create and add it
|
|
17
|
+
const script = document.createElement('script');
|
|
18
|
+
script.type = 'text/javascript';
|
|
19
|
+
script.src = appleIdScriptSrc;
|
|
20
|
+
script.setAttribute('data-apple-sdk', 'true');
|
|
21
|
+
script.onload = () => {
|
|
22
|
+
script === null || script === void 0 ? void 0 : script.setAttribute('data-loaded', 'true');
|
|
23
|
+
resolve();
|
|
24
|
+
};
|
|
25
|
+
script.onerror = () => reject(new Error('Failed to load Apple SDK'));
|
|
26
|
+
window.document.head.appendChild(script);
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
export { loadAppleId };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export { useSocialAuth } from './useSocialAuth';
|
|
1
|
+
export { useSocialAuth, type SocialAuthMode } from './useSocialAuth';
|
|
@@ -37,6 +37,7 @@ require('../../../config/ApiEndpoint.cjs');
|
|
|
37
37
|
require('@dynamic-labs/store');
|
|
38
38
|
var oauth = require('../../../data/api/oauth/oauth.cjs');
|
|
39
39
|
require('../../../locale/locale.cjs');
|
|
40
|
+
var socialStorage = require('../../functions/socialStorage/socialStorage.cjs');
|
|
40
41
|
require('../../../context/AccessDeniedContext/AccessDeniedContext.cjs');
|
|
41
42
|
require('../../../context/AccountExistsContext/AccountExistsContext.cjs');
|
|
42
43
|
require('../../../context/UserWalletsContext/UserWalletsContext.cjs');
|
|
@@ -47,6 +48,7 @@ require('../../../context/ThemeContext/ThemeContext.cjs');
|
|
|
47
48
|
require('../useUserUpdateRequest/useUpdateUser/userFieldsSchema.cjs');
|
|
48
49
|
var useUserAuth = require('../useUserAuth/useUserAuth.cjs');
|
|
49
50
|
var farcaster = require('./farcaster.cjs');
|
|
51
|
+
var connectWithAppleId = require('./apple/connectWithAppleId.cjs');
|
|
50
52
|
require('yup');
|
|
51
53
|
require('../../../context/MockContext/MockContext.cjs');
|
|
52
54
|
require('../../../views/CollectUserDataView/useFields.cjs');
|
|
@@ -105,7 +107,11 @@ const socialProviders = [
|
|
|
105
107
|
const providersRequiringPkce = ['twitter', 'github'];
|
|
106
108
|
// Hook to handle common social auth logic (for social linking and social sign in)
|
|
107
109
|
const useSocialAuth = ({ sessionTimeout, onSettled, onError, onFarcasterUrl, }) => {
|
|
108
|
-
const { environmentId, setShowAuthFlow, projectSettings, consumeNonce, redirectUrl, } = useInternalDynamicContext.useInternalDynamicContext();
|
|
110
|
+
const { environmentId, setShowAuthFlow, projectSettings, consumeNonce, socialSettings, redirectUrl, } = useInternalDynamicContext.useInternalDynamicContext();
|
|
111
|
+
/**
|
|
112
|
+
* Represents if the SDK will use a popup or redirect for social auth
|
|
113
|
+
*/
|
|
114
|
+
const strategy = React.useMemo(() => (socialSettings === null || socialSettings === void 0 ? void 0 : socialSettings.strategy) || 'popup', [socialSettings]);
|
|
109
115
|
const { setErrorMessage, setError: setContextError } = ErrorContext.useErrorContext();
|
|
110
116
|
const { initAuth } = useUserAuth.useUserAuth({
|
|
111
117
|
authMethod: 'social',
|
|
@@ -171,7 +177,7 @@ const useSocialAuth = ({ sessionTimeout, onSettled, onError, onFarcasterUrl, })
|
|
|
171
177
|
});
|
|
172
178
|
});
|
|
173
179
|
const connectSocialAccount = (_a) => _tslib.__awaiter(void 0, [_a], void 0, function* ({ authMode, provider, validator, captchaToken, }) {
|
|
174
|
-
var _b, _c;
|
|
180
|
+
var _b, _c, _d;
|
|
175
181
|
clearInterval(authWindowInterval.current);
|
|
176
182
|
clearError();
|
|
177
183
|
setIsProcessing(true);
|
|
@@ -193,17 +199,40 @@ const useSocialAuth = ({ sessionTimeout, onSettled, onError, onFarcasterUrl, })
|
|
|
193
199
|
});
|
|
194
200
|
if (provider === sdkApiCore.ProviderEnum.Farcaster)
|
|
195
201
|
return connectWithFarcaster(authMode, { captchaToken });
|
|
196
|
-
|
|
202
|
+
let authWindow = null;
|
|
197
203
|
const state = randomString.randomString(32);
|
|
198
204
|
const verifier = randomString.randomString(43);
|
|
205
|
+
const apiProvider = getProviderByType.getProviderByType((_b = projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.providers) !== null && _b !== void 0 ? _b : [], provider);
|
|
206
|
+
const loginUrl = getOauthLoginUrl.getOauthLoginUrl((_c = projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.providers) !== null && _c !== void 0 ? _c : [], provider);
|
|
207
|
+
// Whether we will use PKCE (code verifier/challenge) for this provider
|
|
208
|
+
const usingPkce = providersRequiringPkce.includes(provider);
|
|
209
|
+
socialStorage.setSocialStorageFor(provider, {
|
|
210
|
+
captchaToken,
|
|
211
|
+
codeVerifier: verifier,
|
|
212
|
+
state,
|
|
213
|
+
});
|
|
214
|
+
if (provider === sdkApiCore.ProviderEnum.Apple &&
|
|
215
|
+
(utils.isSafariBrowser() || utils.isIOS() || strategy === 'redirect')) {
|
|
216
|
+
yield oauth.initAuth(environmentId, provider, {
|
|
217
|
+
// When we set the redirectUrl, the backend gives preference to a redirect
|
|
218
|
+
// experience, which is only desired for mobile
|
|
219
|
+
redirectUrl: removeDynamicOauthParamsFromUrl(redirectUrl || window.location.href),
|
|
220
|
+
state,
|
|
221
|
+
});
|
|
222
|
+
yield connectWithAppleId.connectWithAppleId({
|
|
223
|
+
clientId: apiProvider === null || apiProvider === void 0 ? void 0 : apiProvider.clientId,
|
|
224
|
+
oauthLoginUrl: new URL(loginUrl),
|
|
225
|
+
state,
|
|
226
|
+
});
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
199
229
|
const clearListeners = () => {
|
|
200
230
|
window.removeEventListener('message', handleAuthWindowMessage);
|
|
201
231
|
providersWaitingOauthMessage.current = Object.assign(Object.assign({}, providersWaitingOauthMessage.current), { [provider]: false });
|
|
202
232
|
};
|
|
203
233
|
const handleAuthWindowMessage = (event) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
|
|
204
|
-
var
|
|
234
|
+
var _e;
|
|
205
235
|
const message = event.data;
|
|
206
|
-
const apiProvider = getProviderByType.getProviderByType((_d = projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.providers) !== null && _d !== void 0 ? _d : [], provider);
|
|
207
236
|
if (!(apiProvider === null || apiProvider === void 0 ? void 0 : apiProvider.redirectUrl)) {
|
|
208
237
|
return;
|
|
209
238
|
}
|
|
@@ -260,31 +289,20 @@ const useSocialAuth = ({ sessionTimeout, onSettled, onError, onFarcasterUrl, })
|
|
|
260
289
|
handleError(provider, types.SocialOAuthErrorCode.NO_AUTH_CODE, `Failed to connect ${provider} social account: no authorization code`, { raiseAuthFailure: authMode === 'signin' });
|
|
261
290
|
return;
|
|
262
291
|
}
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
}
|
|
272
|
-
else {
|
|
273
|
-
const apiCall = () => oauth.verifyOAuth(environmentId, provider, {
|
|
274
|
-
code,
|
|
275
|
-
codeVerifier: verifier,
|
|
276
|
-
state,
|
|
277
|
-
});
|
|
278
|
-
yield linkAccount(provider, apiCall);
|
|
279
|
-
}
|
|
280
|
-
setIsProcessing(false);
|
|
292
|
+
yield completeConnection({
|
|
293
|
+
authCode: code,
|
|
294
|
+
authMode,
|
|
295
|
+
captchaToken,
|
|
296
|
+
provider,
|
|
297
|
+
state,
|
|
298
|
+
verifier,
|
|
299
|
+
});
|
|
281
300
|
});
|
|
282
|
-
const loginUrl = getOauthLoginUrl.getOauthLoginUrl((_b = projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.providers) !== null && _b !== void 0 ? _b : [], provider);
|
|
283
301
|
if (!loginUrl) {
|
|
284
302
|
handleError(provider, types.SocialOAuthErrorCode.NO_OAUTH_URL, `Failed to get login url to connect social account: ${provider}`, { raiseAuthFailure: authMode === 'signin' });
|
|
285
303
|
return;
|
|
286
304
|
}
|
|
287
|
-
if (!((
|
|
305
|
+
if (!((_d = providersWaitingOauthMessage.current) === null || _d === void 0 ? void 0 : _d[provider])) {
|
|
288
306
|
window.addEventListener('message', handleAuthWindowMessage);
|
|
289
307
|
providersWaitingOauthMessage.current = Object.assign(Object.assign({}, providersWaitingOauthMessage.current), { [provider]: true });
|
|
290
308
|
}
|
|
@@ -294,7 +312,7 @@ const useSocialAuth = ({ sessionTimeout, onSettled, onError, onFarcasterUrl, })
|
|
|
294
312
|
});
|
|
295
313
|
let oauthLoginUrl = `${loginUrl}&${stateParam}`;
|
|
296
314
|
// only add pkce params for providers that support/require it
|
|
297
|
-
if (
|
|
315
|
+
if (usingPkce) {
|
|
298
316
|
const digest = yield digestSHA256.digestSHA256(verifier);
|
|
299
317
|
const pkceParams = stringifyURIQuery.stringifyURIQuery({
|
|
300
318
|
code_challenge: encodeBase64URL.encodeBase64URL(digest),
|
|
@@ -302,6 +320,19 @@ const useSocialAuth = ({ sessionTimeout, onSettled, onError, onFarcasterUrl, })
|
|
|
302
320
|
});
|
|
303
321
|
oauthLoginUrl = `${oauthLoginUrl}&${pkceParams}`;
|
|
304
322
|
}
|
|
323
|
+
yield oauth.initAuth(environmentId, provider, {
|
|
324
|
+
// When we set the redirectUrl, the backend gives preference to a redirect
|
|
325
|
+
// experience, which is only desired for mobile
|
|
326
|
+
redirectUrl: strategy === 'redirect'
|
|
327
|
+
? removeDynamicOauthParamsFromUrl(redirectUrl || window.location.href)
|
|
328
|
+
: undefined,
|
|
329
|
+
state,
|
|
330
|
+
});
|
|
331
|
+
if (strategy === 'redirect') {
|
|
332
|
+
window.location.assign(oauthLoginUrl);
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
authWindow = window.open('', '_blank', 'width=500,height=600');
|
|
305
336
|
authWindow === null || authWindow === void 0 ? void 0 : authWindow.location.assign(oauthLoginUrl);
|
|
306
337
|
// For provider that support window.opener, we need to clear all states/listeners when the window is closed
|
|
307
338
|
authWindowInterval.current = setInterval(() => {
|
|
@@ -317,7 +348,27 @@ const useSocialAuth = ({ sessionTimeout, onSettled, onError, onFarcasterUrl, })
|
|
|
317
348
|
}
|
|
318
349
|
}, 2000);
|
|
319
350
|
});
|
|
320
|
-
const
|
|
351
|
+
const completeConnection = (_f) => _tslib.__awaiter(void 0, [_f], void 0, function* ({ authMode, provider, state, authCode, verifier, captchaToken, }) {
|
|
352
|
+
if (authMode === 'signin') {
|
|
353
|
+
const apiCall = () => oauth.signInOAuth(environmentId, provider, {
|
|
354
|
+
captchaToken,
|
|
355
|
+
code: authCode,
|
|
356
|
+
codeVerifier: verifier,
|
|
357
|
+
state,
|
|
358
|
+
});
|
|
359
|
+
yield signInAccount(provider, apiCall);
|
|
360
|
+
}
|
|
361
|
+
else {
|
|
362
|
+
const apiCall = () => oauth.verifyOAuth(environmentId, provider, {
|
|
363
|
+
code: authCode,
|
|
364
|
+
codeVerifier: verifier,
|
|
365
|
+
state,
|
|
366
|
+
});
|
|
367
|
+
yield linkAccount(provider, apiCall);
|
|
368
|
+
}
|
|
369
|
+
setIsProcessing(false);
|
|
370
|
+
});
|
|
371
|
+
const connectWithFarcaster = (authMode_1, _g) => _tslib.__awaiter(void 0, [authMode_1, _g], void 0, function* (authMode, { captchaToken }) {
|
|
321
372
|
const dynamicNonce = consumeNonce();
|
|
322
373
|
const domain = utils.PlatformService.getHost();
|
|
323
374
|
const data = yield farcaster.connect({
|
|
@@ -386,6 +437,7 @@ const useSocialAuth = ({ sessionTimeout, onSettled, onError, onFarcasterUrl, })
|
|
|
386
437
|
});
|
|
387
438
|
return {
|
|
388
439
|
checkValidProvider,
|
|
440
|
+
completeConnection,
|
|
389
441
|
connectSocialAccount,
|
|
390
442
|
error,
|
|
391
443
|
handleError,
|
|
@@ -394,5 +446,11 @@ const useSocialAuth = ({ sessionTimeout, onSettled, onError, onFarcasterUrl, })
|
|
|
394
446
|
setIsProcessing,
|
|
395
447
|
};
|
|
396
448
|
};
|
|
449
|
+
const removeDynamicOauthParamsFromUrl = (url) => {
|
|
450
|
+
const urlObject = new URL(url);
|
|
451
|
+
urlObject.searchParams.delete('dynamicOauthState');
|
|
452
|
+
urlObject.searchParams.delete('dynamicOauthCode');
|
|
453
|
+
return urlObject.toString();
|
|
454
|
+
};
|
|
397
455
|
|
|
398
456
|
exports.useSocialAuth = useSocialAuth;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
2
|
import { ProviderEnum } from '@dynamic-labs/sdk-api-core';
|
|
3
3
|
import { SocialOAuthError, SocialOAuthErrorCode } from '@dynamic-labs/types';
|
|
4
|
-
type SocialAuthMode = 'link' | 'signin' | 'unlink';
|
|
4
|
+
export type SocialAuthMode = 'link' | 'signin' | 'unlink';
|
|
5
5
|
type UseSocialAuthProps = {
|
|
6
6
|
sessionTimeout: number;
|
|
7
7
|
onSettled?: () => void;
|
|
@@ -16,6 +16,14 @@ export type ConnectSocialAccountProps = {
|
|
|
16
16
|
};
|
|
17
17
|
export declare const useSocialAuth: ({ sessionTimeout, onSettled, onError, onFarcasterUrl, }: UseSocialAuthProps) => {
|
|
18
18
|
readonly checkValidProvider: (provider: ProviderEnum, authMode: SocialAuthMode) => boolean;
|
|
19
|
+
readonly completeConnection: ({ authMode, provider, state, authCode, verifier, captchaToken, }: {
|
|
20
|
+
authMode: SocialAuthMode;
|
|
21
|
+
provider: ProviderEnum;
|
|
22
|
+
state: string;
|
|
23
|
+
captchaToken?: string | undefined;
|
|
24
|
+
authCode?: string | undefined;
|
|
25
|
+
verifier?: string | undefined;
|
|
26
|
+
}) => Promise<void>;
|
|
19
27
|
readonly connectSocialAccount: ({ authMode, provider, validator, captchaToken, }: ConnectSocialAccountProps) => Promise<void>;
|
|
20
28
|
readonly error: SocialOAuthError | undefined;
|
|
21
29
|
readonly handleError: (provider: ProviderEnum | undefined, code: SocialOAuthErrorCode, message: string, options?: {
|