53 lines
1.4 KiB
TypeScript
53 lines
1.4 KiB
TypeScript
import { router } from "expo-router";
|
|
import { Alert } from "react-native";
|
|
import type { createAuthClient } from "better-auth/react";
|
|
|
|
import type { RemoveAccountResult } from "@/contexts/AccountsContext";
|
|
import { performAuthReset } from "@/lib/auth-session";
|
|
|
|
type AuthClient = ReturnType<typeof createAuthClient>;
|
|
|
|
type FinishAccountRemovalInput = {
|
|
result: RemoveAccountResult;
|
|
authClient: AuthClient;
|
|
clearActiveAccount: () => Promise<void>;
|
|
activeAccountId: string | null;
|
|
};
|
|
|
|
/** Navigate to sign-in when the last saved account was removed. */
|
|
export async function finishAccountRemoval({
|
|
result,
|
|
authClient,
|
|
clearActiveAccount,
|
|
activeAccountId,
|
|
}: FinishAccountRemovalInput): Promise<void> {
|
|
if (result.remainingCount > 0) return;
|
|
|
|
await performAuthReset({
|
|
authClient,
|
|
clearActiveAccount,
|
|
activeAccountId,
|
|
});
|
|
router.replace("/(auth)/sign-in");
|
|
}
|
|
|
|
export function confirmRemoveAccount(
|
|
label: string,
|
|
onRemove: () => Promise<RemoveAccountResult>,
|
|
onFinished: (result: RemoveAccountResult) => Promise<void>,
|
|
) {
|
|
Alert.alert("Remove account", `Remove ${label} from this device?`, [
|
|
{ text: "Cancel", style: "cancel" },
|
|
{
|
|
text: "Remove",
|
|
style: "destructive",
|
|
onPress: () => {
|
|
void (async () => {
|
|
const result = await onRemove();
|
|
await onFinished(result);
|
|
})();
|
|
},
|
|
},
|
|
]);
|
|
}
|