From 268d4da58f2f3ee29c04efc80964d62327350ca8 Mon Sep 17 00:00:00 2001 From: Innei Date: Sat, 8 Mar 2025 14:25:54 +0800 Subject: [PATCH] feat(mobile): add password reset functionality to account settings Implement password reset screen with current and new password fields Add changePassword method to auth client Update settings navigation to include ResetPassword route Enhance account security section with password change option Signed-off-by: Innei --- .../src/components/ui/form/TextField.tsx | 2 +- apps/mobile/src/lib/auth.ts | 1 + .../src/modules/settings/routes/Account.tsx | 129 +++++++++++++----- .../modules/settings/routes/ResetPassword.tsx | 120 ++++++++++++++++ .../src/modules/settings/routes/index.tsx | 4 +- apps/mobile/src/modules/settings/types.ts | 1 + 6 files changed, 219 insertions(+), 38 deletions(-) create mode 100644 apps/mobile/src/modules/settings/routes/ResetPassword.tsx diff --git a/apps/mobile/src/components/ui/form/TextField.tsx b/apps/mobile/src/components/ui/form/TextField.tsx index a9ef4b1dc..f43f21dd4 100644 --- a/apps/mobile/src/components/ui/form/TextField.tsx +++ b/apps/mobile/src/components/ui/form/TextField.tsx @@ -37,7 +37,7 @@ const BaseField = forwardRef( ref, ) => { return ( - + {!!label && } {!!description && ( {description} diff --git a/apps/mobile/src/lib/auth.ts b/apps/mobile/src/lib/auth.ts index 5a7602eae..760a1b14d 100644 --- a/apps/mobile/src/lib/auth.ts +++ b/apps/mobile/src/lib/auth.ts @@ -67,6 +67,7 @@ const authClient = createAuthClient({ // @keep-sorted export const { changeEmail, + changePassword, forgetPassword, getAccountInfo, getCookie, diff --git a/apps/mobile/src/modules/settings/routes/Account.tsx b/apps/mobile/src/modules/settings/routes/Account.tsx index 736115f68..f589a38d3 100644 --- a/apps/mobile/src/modules/settings/routes/Account.tsx +++ b/apps/mobile/src/modules/settings/routes/Account.tsx @@ -14,6 +14,7 @@ import { GroupedInsetListNavigationLink, GroupedInsetListNavigationLinkIcon, GroupedInsetListSectionHeader, + GroupedOutlineDescription, GroupedPlainButtonCell, } from "@/src/components/ui/grouped/GroupedList" import { getDbPath } from "@/src/database" @@ -21,9 +22,19 @@ import { AppleCuteFiIcon } from "@/src/icons/apple_cute_fi" import { GithubCuteFiIcon } from "@/src/icons/github_cute_fi" import { GoogleCuteFiIcon } from "@/src/icons/google_cute_fi" import type { AuthProvider } from "@/src/lib/auth" -import { getAccountInfo, getProviders, linkSocial, signOut, unlinkAccount } from "@/src/lib/auth" +import { + forgetPassword, + getAccountInfo, + getProviders, + linkSocial, + signOut, + unlinkAccount, +} from "@/src/lib/auth" import { openLink } from "@/src/lib/native" import { toast } from "@/src/lib/toast" +import { useWhoami } from "@/src/store/user/hooks" + +import { useSettingsNavigation } from "../hooks" type Account = { id: string @@ -37,50 +48,24 @@ type Account = { } const accountInfoKey = ["account-info"] -export const AccountScreen = () => { - const { data: accounts } = useQuery({ + +const useAccount = () => { + return useQuery({ queryKey: accountInfoKey, queryFn: () => getAccountInfo(), }) - - const { data: providers, isLoading } = useQuery({ - queryKey: ["providers"], - queryFn: async () => (await getProviders()).data as Record, - }) - - const providerToAccountMap = useMemo(() => { - return Object.keys(providers || {}).reduce( - (acc, provider) => { - acc[provider] = accounts?.data?.find((account) => account.provider === provider)! - return acc - }, - {} as Record, - ) - }, [accounts?.data, providers]) - +} +export const AccountScreen = () => { return ( - - - {providers ? ( - Object.keys(providers).map((provider) => ( - - )) - ) : isLoading ? ( - - - - ) : null} - + + + {/* Danger Zone */} @@ -158,7 +143,7 @@ const AccountLinker: FC<{ label={provider2LabelMap[provider]} icon={provider2IconMap[provider]} postfix={ - + {account?.profile?.email || account?.profile?.name || ""} } @@ -189,3 +174,75 @@ const AccountLinker: FC<{ ) } ;(AccountLinker as any).itemStyle = GroupedInsetListCardItemStyle.NavigationLink + +const AuthenticationSection = () => { + const { data: accounts } = useAccount() + + const { data: providers, isLoading } = useQuery({ + queryKey: ["providers"], + queryFn: async () => (await getProviders()).data as Record, + }) + + const providerToAccountMap = useMemo(() => { + return Object.keys(providers || {}).reduce( + (acc, provider) => { + acc[provider] = accounts?.data?.find((account) => account.provider === provider)! + return acc + }, + {} as Record, + ) + }, [accounts?.data, providers]) + return ( + <> + + + {providers ? ( + Object.keys(providers).map((provider) => ( + + )) + ) : isLoading ? ( + + + + ) : null} + + + + ) +} + +const SecuritySection = () => { + const { data: account } = useAccount() + const hasPassword = account?.data?.find((account) => account.provider === "credential") + const router = useSettingsNavigation() + const whoAmI = useWhoami() + return ( + + + + { + const email = whoAmI?.email || "" + if (!email) { + toast.error("You need to login with email first") + return + } + if (!hasPassword) { + forgetPassword({ email }) + toast.success("We have sent you an email with instructions to reset your password.") + } else { + router.navigate("ResetPassword") + } + }} + /> + {}} /> + + + ) +} diff --git a/apps/mobile/src/modules/settings/routes/ResetPassword.tsx b/apps/mobile/src/modules/settings/routes/ResetPassword.tsx new file mode 100644 index 000000000..ef15639ba --- /dev/null +++ b/apps/mobile/src/modules/settings/routes/ResetPassword.tsx @@ -0,0 +1,120 @@ +import { useMutation } from "@tanstack/react-query" +import { useCallback, useState } from "react" +import { View } from "react-native" +import { useColor } from "react-native-uikit-colors" + +import { RotateableLoading } from "@/src/components/common/RotateableLoading" +import { + NavigationBlurEffectHeader, + SafeNavigationScrollView, +} from "@/src/components/layouts/views/SafeNavigationScrollView" +import { UIBarButton } from "@/src/components/ui/button/UIBarButton" +import { PlainTextField } from "@/src/components/ui/form/TextField" +import { + GroupedInsetListBaseCell, + GroupedInsetListCard, + GroupedInsetListSectionHeader, +} from "@/src/components/ui/grouped/GroupedList" +import { CheckLineIcon } from "@/src/icons/check_line" +import { changePassword } from "@/src/lib/auth" + +export const ResetPassword = () => { + const labelColor = useColor("label") + + const [currentPassword, setCurrentPassword] = useState("") + const [newPassword, setNewPassword] = useState("") + const [confirmNewPassword, setConfirmNewPassword] = useState("") + + const { mutateAsync: changePasswordAsync, isPending } = useMutation({ + mutationFn: () => { + return changePassword({ + currentPassword, + newPassword, + revokeOtherSessions: true, + }) + }, + }) + + const handleSave = useCallback(() => { + return changePasswordAsync() + }, [changePasswordAsync]) + + return ( + + ( + + ) : ( + + ) + } + disabled={ + !currentPassword || + !newPassword || + !confirmNewPassword || + newPassword !== confirmNewPassword + } + onPress={handleSave} + /> + ), + [confirmNewPassword, currentPassword, handleSave, isPending, labelColor, newPassword], + )} + /> + + + + + + + + + + + + + + + + + + + + + + + + + ) +} diff --git a/apps/mobile/src/modules/settings/routes/index.tsx b/apps/mobile/src/modules/settings/routes/index.tsx index 5adc9dc71..90ba38cbc 100644 --- a/apps/mobile/src/modules/settings/routes/index.tsx +++ b/apps/mobile/src/modules/settings/routes/index.tsx @@ -13,10 +13,11 @@ import { ListsScreen } from "./Lists" import { ManageListScreen } from "./ManageList" import { NotificationsScreen } from "./Notifications" import { PrivacyScreen } from "./Privacy" +import { ResetPassword } from "./ResetPassword" const SettingFlatRoutes = (Stack: TypedNavigator) => { return ( - + @@ -32,6 +33,7 @@ const SettingFlatRoutes = (Stack: TypedNavigator) => { {/* @ts-expect-error */} + ) } diff --git a/apps/mobile/src/modules/settings/types.ts b/apps/mobile/src/modules/settings/types.ts index b370c4c47..f4e9569c2 100644 --- a/apps/mobile/src/modules/settings/types.ts +++ b/apps/mobile/src/modules/settings/types.ts @@ -13,4 +13,5 @@ export type SettingsStackParamList = { About: undefined ManageList: { id: string } EditProfile: undefined + ResetPassword: undefined }