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 <tukon479@gmail.com>
This commit is contained in:
@@ -37,7 +37,7 @@ const BaseField = forwardRef<TextInput, TextInputProps & BaseFieldProps>(
|
||||
ref,
|
||||
) => {
|
||||
return (
|
||||
<View className="flex-1">
|
||||
<View className="w-full flex-1">
|
||||
{!!label && <FormLabel className="pl-2.5" label={label} optional={!required} />}
|
||||
{!!description && (
|
||||
<Text className="text-secondary-label mb-1 pl-2.5 text-sm">{description}</Text>
|
||||
|
||||
@@ -67,6 +67,7 @@ const authClient = createAuthClient({
|
||||
// @keep-sorted
|
||||
export const {
|
||||
changeEmail,
|
||||
changePassword,
|
||||
forgetPassword,
|
||||
getAccountInfo,
|
||||
getCookie,
|
||||
|
||||
@@ -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<string, AuthProvider>,
|
||||
})
|
||||
|
||||
const providerToAccountMap = useMemo(() => {
|
||||
return Object.keys(providers || {}).reduce(
|
||||
(acc, provider) => {
|
||||
acc[provider] = accounts?.data?.find((account) => account.provider === provider)!
|
||||
return acc
|
||||
},
|
||||
{} as Record<string, Account>,
|
||||
)
|
||||
}, [accounts?.data, providers])
|
||||
|
||||
}
|
||||
export const AccountScreen = () => {
|
||||
return (
|
||||
<SafeNavigationScrollView className="bg-system-grouped-background">
|
||||
<NavigationBlurEffectHeader title="Account" />
|
||||
|
||||
<View className="mt-6">
|
||||
<GroupedInsetListSectionHeader label="Authentication" />
|
||||
<GroupedInsetListCard>
|
||||
{providers ? (
|
||||
Object.keys(providers).map((provider) => (
|
||||
<AccountLinker
|
||||
key={provider}
|
||||
provider={provider as any}
|
||||
account={providerToAccountMap[provider]}
|
||||
/>
|
||||
))
|
||||
) : isLoading ? (
|
||||
<View className="flex h-12 flex-1 items-center justify-center">
|
||||
<ActivityIndicator />
|
||||
</View>
|
||||
) : null}
|
||||
</GroupedInsetListCard>
|
||||
<AuthenticationSection />
|
||||
</View>
|
||||
|
||||
<SecuritySection />
|
||||
|
||||
{/* Danger Zone */}
|
||||
<View className="mt-6">
|
||||
<GroupedInsetListSectionHeader label="Danger Zone" />
|
||||
@@ -158,7 +143,7 @@ const AccountLinker: FC<{
|
||||
label={provider2LabelMap[provider]}
|
||||
icon={provider2IconMap[provider]}
|
||||
postfix={
|
||||
<Text className="text-secondary-label mr-1 max-w-[100px]">
|
||||
<Text ellipsizeMode="tail" className="text-secondary-label mr-1 max-w-[100px]">
|
||||
{account?.profile?.email || account?.profile?.name || ""}
|
||||
</Text>
|
||||
}
|
||||
@@ -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<string, AuthProvider>,
|
||||
})
|
||||
|
||||
const providerToAccountMap = useMemo(() => {
|
||||
return Object.keys(providers || {}).reduce(
|
||||
(acc, provider) => {
|
||||
acc[provider] = accounts?.data?.find((account) => account.provider === provider)!
|
||||
return acc
|
||||
},
|
||||
{} as Record<string, Account>,
|
||||
)
|
||||
}, [accounts?.data, providers])
|
||||
return (
|
||||
<>
|
||||
<GroupedInsetListSectionHeader label="Authentication" />
|
||||
<GroupedInsetListCard>
|
||||
{providers ? (
|
||||
Object.keys(providers).map((provider) => (
|
||||
<AccountLinker
|
||||
key={provider}
|
||||
provider={provider as any}
|
||||
account={providerToAccountMap[provider]}
|
||||
/>
|
||||
))
|
||||
) : isLoading ? (
|
||||
<View className="flex h-12 flex-1 items-center justify-center">
|
||||
<ActivityIndicator />
|
||||
</View>
|
||||
) : null}
|
||||
</GroupedInsetListCard>
|
||||
<GroupedOutlineDescription description="You can currently only connect social accounts with the same email." />
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
const SecuritySection = () => {
|
||||
const { data: account } = useAccount()
|
||||
const hasPassword = account?.data?.find((account) => account.provider === "credential")
|
||||
const router = useSettingsNavigation()
|
||||
const whoAmI = useWhoami()
|
||||
return (
|
||||
<View className="mt-6">
|
||||
<GroupedInsetListSectionHeader label="Security" />
|
||||
<GroupedInsetListCard>
|
||||
<GroupedPlainButtonCell
|
||||
textClassName="text-left"
|
||||
label="Change password"
|
||||
onPress={() => {
|
||||
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")
|
||||
}
|
||||
}}
|
||||
/>
|
||||
<GroupedPlainButtonCell textClassName="text-left" label="Setting 2FA" onPress={() => {}} />
|
||||
</GroupedInsetListCard>
|
||||
</View>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -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 (
|
||||
<SafeNavigationScrollView className="bg-system-grouped-background flex-1">
|
||||
<NavigationBlurEffectHeader
|
||||
title="Reset Password"
|
||||
headerRight={useCallback(
|
||||
() => (
|
||||
<UIBarButton
|
||||
label="Save"
|
||||
normalIcon={
|
||||
isPending ? (
|
||||
<RotateableLoading color={labelColor} />
|
||||
) : (
|
||||
<CheckLineIcon height={18} width={18} color={labelColor} />
|
||||
)
|
||||
}
|
||||
disabled={
|
||||
!currentPassword ||
|
||||
!newPassword ||
|
||||
!confirmNewPassword ||
|
||||
newPassword !== confirmNewPassword
|
||||
}
|
||||
onPress={handleSave}
|
||||
/>
|
||||
),
|
||||
[confirmNewPassword, currentPassword, handleSave, isPending, labelColor, newPassword],
|
||||
)}
|
||||
/>
|
||||
<View className="h-4" />
|
||||
<GroupedInsetListSectionHeader label="Current Password" />
|
||||
<GroupedInsetListCard>
|
||||
<GroupedInsetListBaseCell className="py-3">
|
||||
<PlainTextField
|
||||
className="w-full"
|
||||
hitSlop={10}
|
||||
secureTextEntry={true}
|
||||
keyboardType="visible-password"
|
||||
placeholder="Enter your current password"
|
||||
value={currentPassword}
|
||||
onChangeText={setCurrentPassword}
|
||||
/>
|
||||
</GroupedInsetListBaseCell>
|
||||
</GroupedInsetListCard>
|
||||
|
||||
<View className="h-4" />
|
||||
<GroupedInsetListSectionHeader label="New Password" />
|
||||
<GroupedInsetListCard>
|
||||
<GroupedInsetListBaseCell className="py-3">
|
||||
<PlainTextField
|
||||
className="w-full"
|
||||
keyboardType="visible-password"
|
||||
secureTextEntry={true}
|
||||
hitSlop={10}
|
||||
placeholder="Enter your new password"
|
||||
value={newPassword}
|
||||
onChangeText={setNewPassword}
|
||||
/>
|
||||
</GroupedInsetListBaseCell>
|
||||
</GroupedInsetListCard>
|
||||
|
||||
<View className="h-4" />
|
||||
<GroupedInsetListSectionHeader label="Confirm New Password" />
|
||||
<GroupedInsetListCard>
|
||||
<GroupedInsetListBaseCell className="py-3">
|
||||
<PlainTextField
|
||||
className="w-full"
|
||||
keyboardType="visible-password"
|
||||
secureTextEntry={true}
|
||||
hitSlop={10}
|
||||
placeholder="Enter your new password again"
|
||||
value={confirmNewPassword}
|
||||
onChangeText={setConfirmNewPassword}
|
||||
returnKeyType="done"
|
||||
onSubmitEditing={handleSave}
|
||||
/>
|
||||
</GroupedInsetListBaseCell>
|
||||
</GroupedInsetListCard>
|
||||
</SafeNavigationScrollView>
|
||||
)
|
||||
}
|
||||
@@ -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<any, any>) => {
|
||||
return (
|
||||
<Stack.Group>
|
||||
<Stack.Group screenOptions={{ headerShown: false }}>
|
||||
<Stack.Screen key="Achievement" name="Achievement" component={AchievementScreen} />
|
||||
<Stack.Screen key="General" name="General" component={GeneralScreen} />
|
||||
<Stack.Screen key="Notifications" name="Notifications" component={NotificationsScreen} />
|
||||
@@ -32,6 +33,7 @@ const SettingFlatRoutes = (Stack: TypedNavigator<any, any>) => {
|
||||
{/* @ts-expect-error */}
|
||||
<Stack.Screen key="ManageList" name="ManageList" component={ManageListScreen} />
|
||||
<Stack.Screen key="EditProfile" name="EditProfile" component={EditProfileScreen} />
|
||||
<Stack.Screen key="ResetPassword" name="ResetPassword" component={ResetPassword} />
|
||||
</Stack.Group>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -13,4 +13,5 @@ export type SettingsStackParamList = {
|
||||
About: undefined
|
||||
ManageList: { id: string }
|
||||
EditProfile: undefined
|
||||
ResetPassword: undefined
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user