Skip to content

Commit

Permalink
fix(core): fix sign in with email (#9108)
Browse files Browse the repository at this point in the history
  • Loading branch information
EYHN authored Dec 12, 2024
1 parent fea4777 commit 91089ff
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
type SetStateAction,
useCallback,
useEffect,
useRef,
useState,
} from 'react';

Expand All @@ -32,7 +33,8 @@ export const SignInWithEmailStep = ({
changeState: Dispatch<SetStateAction<SignInState>>;
close: () => void;
}) => {
const [resendCountDown, setResendCountDown] = useState(60);
const initialSent = useRef(false);
const [resendCountDown, setResendCountDown] = useState(0);

const email = state.email;

Expand Down Expand Up @@ -72,7 +74,7 @@ export const SignInWithEmailStep = ({
}
}, [close, loginStatus, t]);

const onResendClick = useAsyncCallback(async () => {
const sendEmail = useAsyncCallback(async () => {
if (isSending || (!verifyToken && needCaptcha)) return;
setIsSending(true);
try {
Expand Down Expand Up @@ -102,6 +104,13 @@ export const SignInWithEmailStep = ({
verifyToken,
]);

useEffect(() => {
if (!initialSent.current && (verifyToken || !needCaptcha)) {
initialSent.current = true;
sendEmail();
}
}, [initialSent, needCaptcha, sendEmail, verifyToken]);

const onSignInWithPasswordClick = useCallback(() => {
changeState(prev => ({ ...prev, step: 'signInWithPassword' }));
}, [changeState]);
Expand Down Expand Up @@ -139,7 +148,7 @@ export const SignInWithEmailStep = ({
disabled={isSending}
variant="plain"
size="large"
onClick={onResendClick}
onClick={sendEmail}
>
{t['com.affine.auth.sign.auth.code.resend.hint']()}
</Button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ export const SignInWithPasswordStep = ({
const needCaptcha = useLiveData(captchaService.needCaptcha$);
const challenge = useLiveData(captchaService.challenge$);
const [isLoading, setIsLoading] = useState(false);
const [sendingEmail, setSendingEmail] = useState(false);

const loginStatus = useLiveData(authService.session.status$);

Expand Down Expand Up @@ -100,20 +99,9 @@ export const SignInWithPasswordStep = ({
challenge,
]);

const sendMagicLink = useAsyncCallback(async () => {
if (sendingEmail) return;
setSendingEmail(true);
try {
changeState(prev => ({ ...prev, step: 'signInWithEmail' }));
} catch (err) {
console.error(err);
notify.error({
title: 'Failed to send email, please try again.',
});
// TODO(@eyhn): handle error better
}
setSendingEmail(false);
}, [sendingEmail, changeState]);
const sendMagicLink = useCallback(() => {
changeState(prev => ({ ...prev, step: 'signInWithEmail' }));
}, [changeState]);

return (
<>
Expand Down

0 comments on commit 91089ff

Please sign in to comment.