feat(mobile): real user deletion

This commit is contained in:
DIYgod
2025-07-04 18:54:02 +08:00
parent b373232888
commit ad003bd3b8
4 changed files with 91 additions and 36 deletions
+20 -1
View File
@@ -6,10 +6,13 @@ import { createMobileAPIHeaders } from "@follow/utils/headers"
import { useQuery } from "@tanstack/react-query"
import { createAuthClient } from "better-auth/react"
import { nativeApplicationVersion } from "expo-application"
import * as FileSystem from "expo-file-system"
import * as SecureStore from "expo-secure-store"
import { Platform } from "react-native"
import DeviceInfo from "react-native-device-info"
import { getDbPath } from "@/src/database"
import { getClientId, getSessionId } from "./client-session"
import { getUserAgent } from "./native/user-agent"
import { getEnvProfile, proxyEnv } from "./proxy-env"
@@ -82,7 +85,6 @@ export const {
oneTimeToken,
sendVerificationEmail,
signIn,
signOut,
signUp,
twoFactor,
unlinkAccount,
@@ -117,3 +119,20 @@ export function isAuthCodeValid(authCode: string) {
authCode.length === 6 && !Array.from(authCode).some((c) => Number.isNaN(Number.parseInt(c)))
)
}
export const signOut = async () => {
await authClient.signOut()
const dbPath = getDbPath()
await FileSystem.deleteAsync(dbPath)
await expo.reloadAppAsync("User sign out")
}
export const deleteUser = async ({ TOTPCode }: { TOTPCode?: string }) => {
if (!TOTPCode) {
return
}
await authClient.deleteUserCustom({
TOTPCode,
})
await signOut()
}
@@ -0,0 +1,41 @@
import { Text, View } from "react-native"
import { useColor } from "react-native-uikit-colors"
import { PlainTextField } from "@/src/components/ui/form/TextField"
import { Key2CuteReIcon } from "@/src/icons/key_2_cute_re"
import type { DialogComponent } from "@/src/lib/dialog"
import { Dialog } from "@/src/lib/dialog"
export const ConfirmTOTPCodeDialog: DialogComponent<{
totpCode: string
}> = ({ ctx }) => {
const label = useColor("label")
const { bizOnConfirm } = Dialog.useDialogContext()!
return (
<View>
<View className="flex-row items-center gap-2">
<Key2CuteReIcon color={label} height={20} width={20} />
<Text className="text-label text-base font-medium">Enter your TOTP code to continue</Text>
</View>
<PlainTextField
autoFocus
autoCapitalize="none"
secureTextEntry
className="bg-system-background text-text my-3 rounded-xl p-2 px-4"
placeholder="TOTP Code"
onChangeText={(text) => (ctx.totpCode = text)}
returnKeyType="done"
onSubmitEditing={() => {
bizOnConfirm?.()
}}
/>
</View>
)
}
ConfirmTOTPCodeDialog.id = "confirm-password-dialog"
ConfirmTOTPCodeDialog.confirmText = "Confirm"
ConfirmTOTPCodeDialog.cancelText = "Cancel"
ConfirmTOTPCodeDialog.onConfirm = (ctx) => {
ctx.dismiss()
}
@@ -1,7 +1,6 @@
import { UserRole } from "@follow/constants"
import type { ServerConfigs } from "@follow/models/types"
import { useUserRole, useWhoami } from "@follow/store/user/hooks"
import * as FileSystem from "expo-file-system"
import type { ParseKeys } from "i18next"
import type { FC } from "react"
import { Fragment, useMemo } from "react"
@@ -14,7 +13,6 @@ import {
GroupedInsetListNavigationLink,
GroupedInsetListNavigationLinkIcon,
} from "@/src/components/ui/grouped/GroupedList"
import { getDbPath } from "@/src/database"
import { CertificateCuteFiIcon } from "@/src/icons/certificate_cute_fi"
import { DatabaseIcon } from "@/src/icons/database"
import { ExitCuteFiIcon } from "@/src/icons/exit_cute_fi"
@@ -180,11 +178,7 @@ const ActionGroupNavigationLinks: GroupNavigationLink[] = [
text: "Sign out",
style: "destructive",
onPress: async () => {
// sign out
await signOut()
const dbPath = getDbPath()
await FileSystem.deleteAsync(dbPath)
await expo.reloadAppAsync("User sign out")
},
},
])
@@ -1,7 +1,6 @@
import { useWhoami } from "@follow/store/user/hooks"
import { userSyncService } from "@follow/store/user/store"
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"
import * as FileSystem from "expo-file-system"
import type { FC } from "react"
import { useMemo } from "react"
import { useTranslation } from "react-i18next"
@@ -20,17 +19,16 @@ import {
GroupedPlainButtonCell,
} from "@/src/components/ui/grouped/GroupedList"
import { PlatformActivityIndicator } from "@/src/components/ui/loading/PlatformActivityIndicator"
import { getDbPath } from "@/src/database"
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 {
deleteUser,
forgetPassword,
getAccountInfo,
getProviders,
linkSocial,
signOut,
unlinkAccount,
} from "@/src/lib/auth"
import { Dialog } from "@/src/lib/dialog"
@@ -40,6 +38,7 @@ import { useNavigation } from "@/src/lib/navigation/hooks"
import { toast } from "@/src/lib/toast"
import { ConfirmPasswordDialog } from "../../dialogs/ConfirmPasswordDialog"
import { ConfirmTOTPCodeDialog } from "../../dialogs/ConfirmTOTPCodeDialog"
import { TwoFASetting } from "./2FASetting"
import { ResetPassword } from "./ResetPassword"
@@ -76,33 +75,7 @@ export const AccountScreen = () => {
Header={<NavigationBlurEffectHeaderView title={t("titles.account")} />}
>
<AuthenticationSection />
<SecuritySection />
{/* Danger Zone */}
<GroupedInsetListSectionHeader label={t("profile.danger_zone")} />
<GroupedInsetListCard>
<GroupedPlainButtonCell
label={t("profile.delete_account.label")}
textClassName="text-red text-left"
onPress={async () => {
Alert.alert("Delete account", "Are you sure you want to delete your account?", [
{ text: "Cancel", style: "cancel" },
{
text: "Delete",
style: "destructive",
onPress: async () => {
await signOut()
const dbPath = getDbPath()
await FileSystem.deleteAsync(dbPath)
await expo.reloadAppAsync("User sign out")
},
},
])
}}
/>
</GroupedInsetListCard>
</SafeNavigationScrollView>
)
}
@@ -317,6 +290,34 @@ const SecuritySection = () => {
})
}}
/>
<GroupedPlainButtonCell
label={t("profile.delete_account.label")}
textClassName="text-red text-left"
onPress={async () => {
Alert.alert(
"Delete account",
"Are you sure you want to delete your account? \nThis action is irreversible and may take up to two days to take effect.",
[
{ text: "Cancel", style: "cancel" },
{
text: "Delete",
style: "destructive",
onPress: async () => {
// await signOut()
Dialog.show(ConfirmTOTPCodeDialog, {
override: {
async onConfirm(ctx) {
ctx.dismiss()
await deleteUser({ TOTPCode: ctx.totpCode })
},
},
})
},
},
],
)
}}
/>
</GroupedInsetListCard>
</>
)