feat: refresh captcha correctly (#8491)

fix AF-1482
This commit is contained in:
darkskygit
2024-10-15 09:10:28 +00:00
committed by forehalo
parent fa2690064d
commit 610cfdffb7
5 changed files with 33 additions and 12 deletions
@@ -25,7 +25,7 @@ export const SignInWithPassword: FC<AuthPanelProps<'signInWithPassword'>> = ({
const [password, setPassword] = useState(''); const [password, setPassword] = useState('');
const [passwordError, setPasswordError] = useState(false); const [passwordError, setPasswordError] = useState(false);
const [verifyToken, challenge] = useCaptcha(); const [verifyToken, challenge, refreshChallenge] = useCaptcha();
const [isLoading, setIsLoading] = useState(false); const [isLoading, setIsLoading] = useState(false);
const [sendingEmail, setSendingEmail] = useState(false); const [sendingEmail, setSendingEmail] = useState(false);
@@ -43,10 +43,19 @@ export const SignInWithPassword: FC<AuthPanelProps<'signInWithPassword'>> = ({
} catch (err) { } catch (err) {
console.error(err); console.error(err);
setPasswordError(true); setPasswordError(true);
refreshChallenge?.();
} finally { } finally {
setIsLoading(false); setIsLoading(false);
} }
}, [isLoading, authService, email, password, verifyToken, challenge]); }, [
isLoading,
verifyToken,
authService,
email,
password,
challenge,
refreshChallenge,
]);
const sendMagicLink = useAsyncCallback(async () => { const sendMagicLink = useAsyncCallback(async () => {
if (sendingEmail) return; if (sendingEmail) return;
@@ -29,7 +29,7 @@ export const SignIn: FC<AuthPanelProps<'signIn'>> = ({
const authService = useService(AuthService); const authService = useService(AuthService);
const [searchParams] = useSearchParams(); const [searchParams] = useSearchParams();
const [isMutating, setIsMutating] = useState(false); const [isMutating, setIsMutating] = useState(false);
const [verifyToken, challenge] = useCaptcha(); const [verifyToken, challenge, refreshChallenge] = useCaptcha();
const [email, setEmail] = useState(''); const [email, setEmail] = useState('');
const [isValidEmail, setIsValidEmail] = useState(true); const [isValidEmail, setIsValidEmail] = useState(true);
@@ -53,6 +53,7 @@ export const SignIn: FC<AuthPanelProps<'signIn'>> = ({
// provider password sign-in if user has by default // provider password sign-in if user has by default
// If with payment, onl support email sign in to avoid redirect to affine app // If with payment, onl support email sign in to avoid redirect to affine app
if (hasPassword) { if (hasPassword) {
refreshChallenge?.();
setAuthState({ setAuthState({
state: 'signInWithPassword', state: 'signInWithPassword',
email, email,
@@ -82,7 +83,14 @@ export const SignIn: FC<AuthPanelProps<'signIn'>> = ({
} }
setIsMutating(false); setIsMutating(false);
}, [authService, challenge, email, setAuthState, verifyToken]); }, [
authService,
challenge,
email,
refreshChallenge,
setAuthState,
verifyToken,
]);
return ( return (
<> <>
@@ -73,15 +73,19 @@ export const Captcha = () => {
); );
}; };
export const useCaptcha = (): [string | undefined, string?] => { export const useCaptcha = (): [string | undefined, string?, (() => void)?] => {
const [verifyToken] = useAtom(captchaAtom); const [verifyToken] = useAtom(captchaAtom);
const [response, setResponse] = useAtom(responseAtom); const [response, setResponse] = useAtom(responseAtom);
const hasCaptchaFeature = useHasCaptcha(); const hasCaptchaFeature = useHasCaptcha();
const { data: challenge } = useSWR('/api/auth/challenge', challengeFetcher, { const { data: challenge, mutate } = useSWR(
suspense: false, '/api/auth/challenge',
revalidateOnFocus: false, challengeFetcher,
}); {
suspense: false,
revalidateOnFocus: false,
}
);
const prevChallenge = useRef(''); const prevChallenge = useRef('');
useEffect(() => { useEffect(() => {
@@ -106,7 +110,7 @@ export const useCaptcha = (): [string | undefined, string?] => {
if (BUILD_CONFIG.isElectron) { if (BUILD_CONFIG.isElectron) {
if (response) { if (response) {
return [response, challenge?.challenge]; return [response, challenge?.challenge, mutate];
} else { } else {
return [undefined, challenge?.challenge]; return [undefined, challenge?.challenge];
} }
@@ -1,4 +1,4 @@
import { toURLSearchParams } from '@affine/core/modules/navigation'; import { toURLSearchParams } from '@affine/core/modules/navigation/utils';
import type { DocMode } from '@blocksuite/affine/blocks'; import type { DocMode } from '@blocksuite/affine/blocks';
import { createContext, useCallback, useContext, useMemo } from 'react'; import { createContext, useCallback, useContext, useMemo } from 'react';
import type { NavigateFunction, NavigateOptions } from 'react-router-dom'; import type { NavigateFunction, NavigateOptions } from 'react-router-dom';
@@ -1,4 +1,4 @@
import { WorkbenchLink } from '@affine/core/modules/workbench'; import { WorkbenchLink } from '@affine/core/modules/workbench/view/workbench-link';
import { ArrowDownSmallIcon } from '@blocksuite/icons/rc'; import { ArrowDownSmallIcon } from '@blocksuite/icons/rc';
import clsx from 'clsx'; import clsx from 'clsx';
import React from 'react'; import React from 'react';