From 9be249dba7737e26010d2f363ed80bca431c38d9 Mon Sep 17 00:00:00 2001 From: Innei Date: Wed, 2 Apr 2025 17:49:32 +0800 Subject: [PATCH] feat(mobile): enhance TOTP modal handling and invitation confirmation - Added a new function to validate authentication codes in the auth module. - Updated the useTOTPModalWrapper to include a dismiss option for better modal management. - Integrated the TOTP modal wrapper into the invitation confirmation dialog for improved user experience. - Refactored the Invitations screen to utilize the updated TOTP modal handling. These changes improve the security and usability of the invitation process by ensuring proper two-factor authentication handling. Signed-off-by: Innei --- .../src/modules/settings/tabs/invitations.tsx | 3 +- .../layouts/header/HeaderElements.tsx | 9 +- .../layouts/header/NavigationHeader.tsx | 5 +- apps/mobile/src/lib/auth.ts | 6 + .../src/lib/navigation/StackNavigation.tsx | 54 +++--- .../modules/settings/compoents/OTPWindow.tsx | 181 ++++++++++++++++++ .../settings/hooks/useTOTPModalWrapper.tsx | 5 +- .../modules/settings/routes/Invitations.tsx | 8 +- apps/mobile/src/screens/(modal)/2fa.tsx | 8 +- 9 files changed, 242 insertions(+), 37 deletions(-) create mode 100644 apps/mobile/src/modules/settings/compoents/OTPWindow.tsx diff --git a/apps/desktop/src/renderer/src/modules/settings/tabs/invitations.tsx b/apps/desktop/src/renderer/src/modules/settings/tabs/invitations.tsx index 989ede1c8..d14db2eaa 100644 --- a/apps/desktop/src/renderer/src/modules/settings/tabs/invitations.tsx +++ b/apps/desktop/src/renderer/src/modules/settings/tabs/invitations.tsx @@ -31,6 +31,7 @@ import { usePresentUserProfileModal, useTOTPModalWrapper } from "~/modules/profi import { UserAvatar } from "~/modules/user/UserAvatar" import { Queries } from "~/queries" +const numberFormatter = new Intl.NumberFormat("en-US") export const SettingInvitations = () => { const { t } = useTranslation("settings") const invitations = useAuthQuery(Queries.invitations.list()) @@ -66,7 +67,7 @@ export const SettingInvitations = () => { diff --git a/apps/mobile/src/components/layouts/header/HeaderElements.tsx b/apps/mobile/src/components/layouts/header/HeaderElements.tsx index b37d2c3a8..a276942a1 100644 --- a/apps/mobile/src/components/layouts/header/HeaderElements.tsx +++ b/apps/mobile/src/components/layouts/header/HeaderElements.tsx @@ -1,6 +1,7 @@ import { cn, withOpacity } from "@follow/utils" import { useCallback } from "react" import { Text, TouchableOpacity, View } from "react-native" +import { useSafeAreaInsets } from "react-native-safe-area-context" import { CheckLineIcon } from "@/src/icons/check_line" import { CloseCuteReIcon } from "@/src/icons/close_cute_re" @@ -98,9 +99,15 @@ export const HeaderSubmitTextButton = ({ } export const HeaderCloseOnly = () => { + const insets = useSafeAreaInsets() return ( - + diff --git a/apps/mobile/src/components/layouts/header/NavigationHeader.tsx b/apps/mobile/src/components/layouts/header/NavigationHeader.tsx index d85b1ba96..41868f604 100644 --- a/apps/mobile/src/components/layouts/header/NavigationHeader.tsx +++ b/apps/mobile/src/components/layouts/header/NavigationHeader.tsx @@ -10,7 +10,7 @@ import { useRef, useState, } from "react" -import type { LayoutChangeEvent } from "react-native" +import type { LayoutChangeEvent, StyleProp, ViewStyle } from "react-native" import { Alert, StyleSheet, TouchableOpacity, View } from "react-native" import type { AnimatedProps } from "react-native-reanimated" import Animated, { @@ -402,11 +402,13 @@ export const UINavigationHeaderActionButton = ({ onPress, disabled, className, + style, }: { children: ReactNode onPress?: () => void disabled?: boolean className?: string + style?: StyleProp }) => { return ( {children} diff --git a/apps/mobile/src/lib/auth.ts b/apps/mobile/src/lib/auth.ts index 3ca4edeb4..c13bc4fd0 100644 --- a/apps/mobile/src/lib/auth.ts +++ b/apps/mobile/src/lib/auth.ts @@ -98,3 +98,9 @@ export const useAuthProviders = () => { queryFn: async () => (await getProviders()).data, }) } + +export function isAuthCodeValid(authCode: string) { + return ( + authCode.length === 6 && !Array.from(authCode).some((c) => Number.isNaN(Number.parseInt(c))) + ) +} diff --git a/apps/mobile/src/lib/navigation/StackNavigation.tsx b/apps/mobile/src/lib/navigation/StackNavigation.tsx index ffe3edcc1..2dac5e4e9 100644 --- a/apps/mobile/src/lib/navigation/StackNavigation.tsx +++ b/apps/mobile/src/lib/navigation/StackNavigation.tsx @@ -187,30 +187,36 @@ const ModalScreenStackItems: FC<{ screenOptions={rootModalRoute.screenOptions} {...modalScreenOptions} > - - - - - {routes.slice(1).map((route) => { - return ( - - - - ) - })} - + + + + + + {routes.slice(1).map((route) => { + return ( + + + + ) + })} + + ) diff --git a/apps/mobile/src/modules/settings/compoents/OTPWindow.tsx b/apps/mobile/src/modules/settings/compoents/OTPWindow.tsx new file mode 100644 index 000000000..8427808c5 --- /dev/null +++ b/apps/mobile/src/modules/settings/compoents/OTPWindow.tsx @@ -0,0 +1,181 @@ +import { useMutation } from "@tanstack/react-query" +import type { FC } from "react" +import { useEffect, useRef, useState } from "react" +import { Keyboard, Modal, StyleSheet, Text, useWindowDimensions, View } from "react-native" +import type { OtpInputRef } from "react-native-otp-entry" +import { OtpInput } from "react-native-otp-entry" +import Animated, { FadeIn, FadeOut, useSharedValue, withSpring } from "react-native-reanimated" +import { useSafeAreaInsets } from "react-native-safe-area-context" +import { useColor } from "react-native-uikit-colors" + +import { isAuthCodeValid, twoFactor } from "@/src/lib/auth" +import { toast } from "@/src/lib/toast" +import { accentColor } from "@/src/theme/colors" + +export const OTPWindow: FC<{ + onSuccess: (token: string) => void +}> = ({ onSuccess }) => { + const otpInputRef = useRef(null) + const label = useColor("label") + const tertiaryLabel = useColor("tertiaryLabel") + const secondaryBackground = useColor("gray5") + const tertiaryBackground = useColor("gray6") + + const submitMutation = useMutation({ + onError(error) { + toast.error(`Failed to verify: ${error.message}`) + }, + onSuccess(data) { + onSuccess(data.token) + }, + onSettled() { + setOpen(false) + }, + mutationFn: async ({ code }: { code: string }) => { + const { data, error } = await twoFactor.verifyTotp({ code }) + if (!data || error) { + const errorMessage = error?.message ?? "Invalid TOTP code" + toast.error(errorMessage) + throw new Error(errorMessage) + } + + return data + }, + }) + + const windowScaleValue = useSharedValue(0.9) + const windowOpacityValue = useSharedValue(0) + + const [open, setOpen] = useState(false) + + useEffect(() => { + setOpen(true) + }, []) + + useEffect(() => { + if (open) { + windowScaleValue.value = withSpring(1, { stiffness: 100, damping: 10 }) + windowOpacityValue.value = withSpring(1, { stiffness: 100, damping: 10 }) + } else { + windowOpacityValue.value = withSpring(0, { stiffness: 100, damping: 10 }) + windowScaleValue.value = withSpring(0.9, { stiffness: 100, damping: 10 }) + } + }, [open, windowOpacityValue, windowScaleValue]) + + const { height } = useWindowDimensions() + + const insets = useSafeAreaInsets() + const [nextHeight, setNextHeight] = useState(height - insets.top) + useEffect(() => { + const sub = [ + Keyboard.addListener("keyboardDidShow", () => { + const metrics = Keyboard.metrics() + + if (!metrics) return + setNextHeight(height - metrics.height) + }), + Keyboard.addListener("keyboardWillHide", () => { + setNextHeight(height - insets.top) + }), + ] + return () => sub.forEach((listener) => listener.remove()) + }, [height, insets.top]) + + return ( + setOpen(false)}> + + + + + {/* Window */} + + + + + + Verification Required + + + Please enter the code from your authenticator app. + + + + + { + if (isAuthCodeValid(code)) { + submitMutation.mutate({ code }) + } + }} + /> + + + + + { + setOpen(false) + }} + suppressHighlighting + > + Cancel + + + + + + + + ) +} + +const styles = StyleSheet.create({ + window: { + shadowColor: "#000", + shadowOffset: { width: 0, height: 2 }, + shadowOpacity: 0.15, + shadowRadius: 10, + elevation: 5, + maxWidth: 400, + }, +}) diff --git a/apps/mobile/src/modules/settings/hooks/useTOTPModalWrapper.tsx b/apps/mobile/src/modules/settings/hooks/useTOTPModalWrapper.tsx index e21704ec6..0706b4977 100644 --- a/apps/mobile/src/modules/settings/hooks/useTOTPModalWrapper.tsx +++ b/apps/mobile/src/modules/settings/hooks/useTOTPModalWrapper.tsx @@ -8,13 +8,14 @@ import { useWhoami } from "@/src/store/user/hooks" export const useTOTPModalWrapper = ( callback: (input: T) => Promise, - options?: { force?: boolean }, + options?: { force?: boolean; dismiss?: () => any }, ) => { const user = useWhoami() const navigation = useNavigation() return useCallback( async (input: T) => { const presentTOTPModal = () => { + options?.dismiss?.() if (!user?.twoFactorEnabled) { toast.error("You need to enable two-factor authentication to perform this action.") @@ -53,6 +54,6 @@ export const useTOTPModalWrapper = ( } } }, - [callback, navigation, options?.force, user?.twoFactorEnabled], + [callback, navigation, options, user?.twoFactorEnabled], ) } diff --git a/apps/mobile/src/modules/settings/routes/Invitations.tsx b/apps/mobile/src/modules/settings/routes/Invitations.tsx index 74beefa3a..387012708 100644 --- a/apps/mobile/src/modules/settings/routes/Invitations.tsx +++ b/apps/mobile/src/modules/settings/routes/Invitations.tsx @@ -31,6 +31,8 @@ import { queryClient } from "@/src/lib/query-client" import { toast } from "@/src/lib/toast" import { accentColor } from "@/src/theme/colors" +import { useTOTPModalWrapper } from "../hooks/useTOTPModalWrapper" + const invitationQueryKey = ["invitations"] const useInvitationsQuery = () => { return useQuery({ @@ -184,6 +186,10 @@ const ConfirmGenerateDialog: DialogComponent = () => { }, }) + const confirm = useTOTPModalWrapper(() => newInvitation.mutateAsync({}), { + dismiss, + }) + return ( @@ -196,7 +202,7 @@ const ConfirmGenerateDialog: DialogComponent = () => { { - newInvitation.mutateAsync({}) + confirm({}) }} /> diff --git a/apps/mobile/src/screens/(modal)/2fa.tsx b/apps/mobile/src/screens/(modal)/2fa.tsx index 2b2b130b8..1b1aec2d2 100644 --- a/apps/mobile/src/screens/(modal)/2fa.tsx +++ b/apps/mobile/src/screens/(modal)/2fa.tsx @@ -7,7 +7,7 @@ import { OtpInput } from "react-native-otp-entry" import { useColor } from "react-native-uikit-colors" import { HeaderCloseOnly } from "@/src/components/layouts/header/HeaderElements" -import { twoFactor } from "@/src/lib/auth" +import { isAuthCodeValid, twoFactor } from "@/src/lib/auth" import { useNavigation } from "@/src/lib/navigation/hooks" import type { NavigationControllerView } from "@/src/lib/navigation/types" import { queryClient } from "@/src/lib/query-client" @@ -15,12 +15,6 @@ import { toast } from "@/src/lib/toast" import { whoamiQueryKey } from "@/src/store/user/hooks" import { accentColor } from "@/src/theme/colors" -function isAuthCodeValid(authCode: string) { - return ( - authCode.length === 6 && !Array.from(authCode).some((c) => Number.isNaN(Number.parseInt(c))) - ) -} - export const TwoFactorAuthScreen: NavigationControllerView = () => { const label = useColor("label") const tertiaryLabel = useColor("tertiaryLabel")