fix(core): fix sign in with email (#9108)

This commit is contained in:
EYHN
2024-12-12 11:48:36 +09:00
committed by GitHub
parent fea4777ef2
commit 91089ff5a2
2 changed files with 15 additions and 18 deletions
@@ -16,6 +16,7 @@ import {
type SetStateAction, type SetStateAction,
useCallback, useCallback,
useEffect, useEffect,
useRef,
useState, useState,
} from 'react'; } from 'react';
@@ -32,7 +33,8 @@ export const SignInWithEmailStep = ({
changeState: Dispatch<SetStateAction<SignInState>>; changeState: Dispatch<SetStateAction<SignInState>>;
close: () => void; close: () => void;
}) => { }) => {
const [resendCountDown, setResendCountDown] = useState(60); const initialSent = useRef(false);
const [resendCountDown, setResendCountDown] = useState(0);
const email = state.email; const email = state.email;
@@ -72,7 +74,7 @@ export const SignInWithEmailStep = ({
} }
}, [close, loginStatus, t]); }, [close, loginStatus, t]);
const onResendClick = useAsyncCallback(async () => { const sendEmail = useAsyncCallback(async () => {
if (isSending || (!verifyToken && needCaptcha)) return; if (isSending || (!verifyToken && needCaptcha)) return;
setIsSending(true); setIsSending(true);
try { try {
@@ -102,6 +104,13 @@ export const SignInWithEmailStep = ({
verifyToken, verifyToken,
]); ]);
useEffect(() => {
if (!initialSent.current && (verifyToken || !needCaptcha)) {
initialSent.current = true;
sendEmail();
}
}, [initialSent, needCaptcha, sendEmail, verifyToken]);
const onSignInWithPasswordClick = useCallback(() => { const onSignInWithPasswordClick = useCallback(() => {
changeState(prev => ({ ...prev, step: 'signInWithPassword' })); changeState(prev => ({ ...prev, step: 'signInWithPassword' }));
}, [changeState]); }, [changeState]);
@@ -139,7 +148,7 @@ export const SignInWithEmailStep = ({
disabled={isSending} disabled={isSending}
variant="plain" variant="plain"
size="large" size="large"
onClick={onResendClick} onClick={sendEmail}
> >
{t['com.affine.auth.sign.auth.code.resend.hint']()} {t['com.affine.auth.sign.auth.code.resend.hint']()}
</Button> </Button>
@@ -57,7 +57,6 @@ export const SignInWithPasswordStep = ({
const needCaptcha = useLiveData(captchaService.needCaptcha$); const needCaptcha = useLiveData(captchaService.needCaptcha$);
const challenge = useLiveData(captchaService.challenge$); const challenge = useLiveData(captchaService.challenge$);
const [isLoading, setIsLoading] = useState(false); const [isLoading, setIsLoading] = useState(false);
const [sendingEmail, setSendingEmail] = useState(false);
const loginStatus = useLiveData(authService.session.status$); const loginStatus = useLiveData(authService.session.status$);
@@ -100,20 +99,9 @@ export const SignInWithPasswordStep = ({
challenge, challenge,
]); ]);
const sendMagicLink = useAsyncCallback(async () => { const sendMagicLink = useCallback(() => {
if (sendingEmail) return; changeState(prev => ({ ...prev, step: 'signInWithEmail' }));
setSendingEmail(true); }, [changeState]);
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]);
return ( return (
<> <>