Skip to content

Commit

Permalink
fix: iOS redirect delay (#116)
Browse files Browse the repository at this point in the history
* fix: iOS redirect delay

* chore: add changeset
  • Loading branch information
horsefacts authored Jan 22, 2024
1 parent 47a8553 commit be9a35a
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/famous-glasses-exercise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@farcaster/auth-kit": patch
---

fix: iOS redirect delay
2 changes: 1 addition & 1 deletion packages/auth-kit/src/components/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export type Props = ButtonHTMLAttributes<HTMLButtonElement> & {

export function Button({ kind = "primary", children, className, ...rest }: Props) {
return (
<button className={kindClass[kind] || className} {...rest}>
<button type="button" className={kindClass[kind] || className} {...rest}>
{children}
</button>
);
Expand Down
26 changes: 20 additions & 6 deletions packages/auth-kit/src/components/SignInButton/SignInButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export function SignInButton({ debug, ...signInArgs }: SignInButtonProps) {
[onError]
);


const signInState = useSignIn({
...signInArgs,
onSuccess: onSuccessCallback,
Expand All @@ -42,34 +43,47 @@ export function SignInButton({ debug, ...signInArgs }: SignInButtonProps) {
const {
signIn,
signOut,
connect,
reconnect,
isSuccess,
isError,
error,
channelToken,
url,
data,
validSignature,
} = signInState;

const onSignOut = useCallback(() => {
setShowDialog(false);
signOut();
}, [signOut]);

const [showDialog, setShowDialog] = useState(false);

const onClick = useCallback(() => {
isError ? reconnect() : signIn();
if (isError) {
reconnect()
}
setShowDialog(true);
}, [isError, reconnect, signIn]);
signIn();
if (url && isMobile()) {
window.location.href = url;
}
}, [isError, reconnect, signIn, url]);

const authenticated = isSuccess && validSignature;

useEffect(() => {
if (url && isMobile()) {
window.location.href = url;
if (!channelToken) {
connect();
}
}, [url, setShowDialog]);
}, [channelToken, connect]);

return (
<div className="fc-authkit-signin-button">
{authenticated ? (
<ProfileButton userData={data} signOut={signOut} />
<ProfileButton userData={data} signOut={onSignOut} />
) : (
<>
<ActionButton onClick={onClick} label="Sign in" />
Expand Down
6 changes: 5 additions & 1 deletion packages/auth-kit/src/hooks/useSignIn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ export function useSignIn(args: UseSignInArgs) {
reconnect,
reset,
data: { channelToken, url, qrCodeUri, nonce },
isSuccess: isConnected,
isError: isCreateChannelError,
error: createChannelError,
} = useCreateChannel({ ...createChannelArgs, onError });

const {
watch,
isPolling,
data: statusData,
isError: isWatchStatusError,
Expand Down Expand Up @@ -73,7 +75,7 @@ export function useSignIn(args: UseSignInArgs) {
const error = createChannelError || watchStatusError || verifyError;

const signIn = () => {
connect();
watch();
};

const signOut = () => {
Expand All @@ -91,7 +93,9 @@ export function useSignIn(args: UseSignInArgs) {
return {
signIn,
signOut,
connect,
reconnect,
isConnected,
isSuccess,
isPolling,
isError,
Expand Down
11 changes: 9 additions & 2 deletions packages/auth-kit/src/hooks/useWatchStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,26 @@ export function useWatchStatus(args: UseWatchStatusArgs) {
};

const [statusData, setStatusData] = useState<StatusAPIResponse>();
const [isEnabled, setIsEnabled] = useState<boolean>(false);
const [isSuccess, setIsSuccess] = useState<boolean>(false);
const [isPolling, setIsPolling] = useState<boolean>(false);
const [isError, setIsError] = useState<boolean>(false);
const [error, setError] = useState<AuthClientError>();

const resetState = () => {
setStatusData(undefined);
setIsEnabled(false);
setIsError(false);
setIsSuccess(false);
setError(undefined);
};

const watch = () => {
setIsEnabled(true);
};

const watchStatus = useCallback(async () => {
if (appClient && channelToken) {
if (isEnabled && appClient && channelToken) {
setIsPolling(true);
const {
data,
Expand All @@ -66,7 +72,7 @@ export function useWatchStatus(args: UseWatchStatusArgs) {
onSuccess?.(data);
}
}
}, [appClient, channelToken, timeout, interval, onSuccess, onError, onResponse]);
}, [isEnabled, appClient, channelToken, timeout, interval, onSuccess, onError, onResponse]);

useEffect(() => {
resetState();
Expand All @@ -76,6 +82,7 @@ export function useWatchStatus(args: UseWatchStatusArgs) {
}, [channelToken, watchStatus]);

return {
watch,
isSuccess,
isPolling,
isError,
Expand Down

0 comments on commit be9a35a

Please sign in to comment.