-
Notifications
You must be signed in to change notification settings - Fork 438
Expand file tree
/
Copy pathSignInButton.tsx
More file actions
54 lines (48 loc) · 1.69 KB
/
SignInButton.tsx
File metadata and controls
54 lines (48 loc) · 1.69 KB
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
import type { SignInButtonProps, SignInProps } from '@clerk/types';
import React from 'react';
import type { WithClerkProp } from '../types';
import { assertSingleChild, normalizeWithDefaultValue, safeExecute } from '../utils';
import { withClerk } from './withClerk';
export const SignInButton = withClerk(
({ clerk, children, ...props }: WithClerkProp<React.PropsWithChildren<SignInButtonProps>>) => {
const {
signUpFallbackRedirectUrl,
forceRedirectUrl,
fallbackRedirectUrl,
signUpForceRedirectUrl,
mode,
initialValues,
withSignUp,
...rest
} = props;
children = normalizeWithDefaultValue(children, 'Sign in');
const child = assertSingleChild(children)('SignInButton');
const clickHandler = () => {
const opts: SignInProps = {
forceRedirectUrl,
fallbackRedirectUrl,
signUpFallbackRedirectUrl,
signUpForceRedirectUrl,
initialValues,
withSignUp,
};
if (mode === 'modal') {
return clerk.openSignIn({ ...opts, appearance: props.appearance });
}
return clerk.redirectToSignIn({
...opts,
signInFallbackRedirectUrl: fallbackRedirectUrl,
signInForceRedirectUrl: forceRedirectUrl,
});
};
const wrappedChildClickHandler: React.MouseEventHandler = async e => {
if (child && typeof child === 'object' && 'props' in child) {
await safeExecute(child.props.onClick)(e);
}
return clickHandler();
};
const childProps = { ...rest, onClick: wrappedChildClickHandler };
return React.cloneElement(child as React.ReactElement<unknown>, childProps);
},
{ component: 'SignInButton', renderWhileLoading: true },
);