-
Notifications
You must be signed in to change notification settings - Fork 337
/
Copy pathSignInSocialButtons.tsx
69 lines (63 loc) · 2.93 KB
/
SignInSocialButtons.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { useClerk } from '@clerk/shared/react';
import React from 'react';
import { SESSION_STORAGE_AUTH_WITH_POPUP_KEY } from '../../../core/constants';
import { buildSSOCallbackURL } from '../../common/redirects';
import { useCoreSignIn, useSignInContext } from '../../contexts';
import { useEnvironment } from '../../contexts/EnvironmentContext';
import { useCardState } from '../../elements/contexts';
import type { SocialButtonsProps } from '../../elements/SocialButtons';
import { SocialButtons } from '../../elements/SocialButtons';
import { useRouter } from '../../router';
import { handleError, originPrefersPopup, web3CallbackErrorHandler } from '../../utils';
export const SignInSocialButtons = React.memo((props: SocialButtonsProps) => {
const clerk = useClerk();
const { navigate } = useRouter();
const card = useCardState();
const { displayConfig } = useEnvironment();
const ctx = useSignInContext();
const signIn = useCoreSignIn();
const redirectUrl = buildSSOCallbackURL(ctx, displayConfig.signInUrl);
const redirectUrlComplete = ctx.afterSignInUrl || '/';
const shouldUsePopup = ctx.oauthFlow === 'popup' || (ctx.oauthFlow === 'auto' && originPrefersPopup());
return (
<SocialButtons
{...props}
idleAfterDelay={!shouldUsePopup}
oauthCallback={strategy => {
if (shouldUsePopup) {
// We create the popup window here with the `about:blank` URL since some browsers will block popups that are
// opened within async functions. The `signInWithPopup` method handles setting the URL of the popup.
const popup = window.open('about:blank', '', 'width=600,height=800');
// Unfortunately, there's no good way to detect when the popup is closed, so we simply poll and check if it's closed.
const interval = setInterval(() => {
if (!popup || popup.closed) {
clearInterval(interval);
card.setIdle();
}
}, 500);
try {
window.sessionStorage.setItem(SESSION_STORAGE_AUTH_WITH_POPUP_KEY, 'true');
} catch {
// It's okay if sessionStorage is disabled since we will treat the failure to read the value as it being true.
}
return signIn
.authenticateWithPopup({ strategy, redirectUrl, redirectUrlComplete, popup })
.catch(err => handleError(err, [], card.setError));
}
return signIn
.authenticateWithRedirect({ strategy, redirectUrl, redirectUrlComplete })
.catch(err => handleError(err, [], card.setError));
}}
web3Callback={strategy => {
return clerk
.authenticateWithWeb3({
customNavigate: navigate,
redirectUrl: redirectUrlComplete,
signUpContinueUrl: ctx.isCombinedFlow ? 'create/continue' : ctx.signUpContinueUrl,
strategy,
})
.catch(err => web3CallbackErrorHandler(err, card.setError));
}}
/>
);
});