Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

mobile: fix showing 2fa error when field is empty and show error below input field

Ammar Ahmedcf6262e9957d
diff --git a/apps/mobile/app/components/auth/two-factor.tsx b/apps/mobile/app/components/auth/two-factor.tsx
index 4803b0549..8cfffa116 100644
--- a/apps/mobile/app/components/auth/two-factor.tsx
+++ b/apps/mobile/app/components/auth/two-factor.tsx
@@ -68,15 +68,24 @@ const TwoFactorVerification = ({
method: mfaInfo?.primaryMethod,
isPrimary: true
});
- const { seconds, start, reset } = useTimer(currentMethod.method!);
+ const { seconds, start, reset, secondsRef } = useTimer(currentMethod.method!);
const [loading, setLoading] = useState(false);
const inputRef = useRef<TextInput>(null);
const [sending, setSending] = useState(false);
const [error, setError] = useState<Error | undefined>(undefined);
const onNext = async () => {
- if (!code.current || code.current.length < 6 || !currentMethod.method)
+ if (!code.current || code.current.length < 6) {
+ setError(
+ new Error("Please provide a valid multi-factor authentication code.")
+ );
return;
+ }
+
+ if (!currentMethod.method) {
+ return;
+ }
+
setLoading(true);
setError(undefined);
inputRef.current?.blur();
@@ -138,7 +147,7 @@ const TwoFactorVerification = ({
};
const onSendCode = useCallback(async () => {
- if (seconds || sending) return;
+ if (secondsRef.current || sending) return;
setSending(true);
try {
await db.mfa.sendCode(currentMethod.method as "sms" | "email");
@@ -146,15 +155,18 @@ const TwoFactorVerification = ({
setSending(false);
} catch (e) {
setSending(false);
- ToastManager.error(e as Error, "Error sending 2FA Code", "local");
+ setError(
+ new Error(`Error sending 2FA Code. Tap "Send code" to try again `)
+ );
}
- }, [currentMethod.method, mfaInfo.token, seconds, sending, start]);
+ }, [currentMethod.method, secondsRef, sending, start]);
useEffect(() => {
if (currentMethod.method === "sms" || currentMethod.method === "email") {
onSendCode();
}
- }, [currentMethod.method, onSendCode]);
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [currentMethod.method]);
return (
<ScrollView
@@ -241,6 +253,7 @@ const TwoFactorVerification = ({
fwdRef={inputRef}
textAlign="center"
onChangeText={(value) => {
+ setError(undefined);
code.current = value;
}}
cursorColor={colors.selected.accent}
@@ -248,6 +261,7 @@ const TwoFactorVerification = ({
selectionColor={colors.selected.accent}
onSubmitEditing={onNext}
height={60}
+ marginBottom={0}
inputStyle={{
fontSize: AppFontSize.lg,
textAlign: "center",
@@ -261,10 +275,26 @@ const TwoFactorVerification = ({
containerStyle={{
minWidth: "50%"
}}
- wrapperStyle={{
- height: 60
- }}
/>
+ {error ? (
+ <Paragraph
+ numberOfLines={4}
+ onPress={() => {}}
+ color={colors.error.accent}
+ style={{
+ textAlign: "center",
+ marginVertical: DefaultAppStyles.GAP_VERTICAL_SMALL,
+ maxWidth: 250
+ }}
+ >
+ <AppIcon
+ color={colors.error.accent}
+ name="alert-circle-outline"
+ size={AppFontSize.sm - 1}
+ />{" "}
+ {error?.message}
+ </Paragraph>
+ ) : null}
<Button
title={loading ? null : strings.next()}
@@ -291,24 +321,6 @@ const TwoFactorVerification = ({
onPress={onRequestSecondaryMethod}
height={30}
/>
-
- {error ? (
- <Paragraph
- numberOfLines={4}
- onPress={() => {}}
- color={colors.error.accent}
- style={{
- textAlign: "center"
- }}
- >
- <AppIcon
- color={colors.error.accent}
- name="alert-circle-outline"
- size={AppFontSize.sm - 1}
- />{" "}
- {error?.message}
- </Paragraph>
- ) : null}
</>
) : (
<>
diff --git a/apps/mobile/app/hooks/use-timer.ts b/apps/mobile/app/hooks/use-timer.ts
index f9e0ecd55..67ab7c30f 100644
--- a/apps/mobile/app/hooks/use-timer.ts
+++ b/apps/mobile/app/hooks/use-timer.ts
@@ -32,6 +32,8 @@ const useTimer = (initialId?: string) => {
const [id, setId] = useState(initialId);
const [seconds, setSeconds] = useState(getSecondsLeft(id));
const interval = useRef<NodeJS.Timeout>(undefined);
+ const secondsRef = useRef(seconds);
+ secondsRef.current = seconds;
const start = (sec: number, currentId = id) => {
if (!currentId) return;
@@ -59,7 +61,7 @@ const useTimer = (initialId?: string) => {
}
};
- return { seconds, setId, start, reset };
+ return { seconds, setId, start, reset, secondsRef: secondsRef };
};
export default useTimer;