Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions apps/expo/src/app/(auth)/sign-in.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { msg, Trans } from "@lingui/macro";
import { useLingui } from "@lingui/react";
import { useTheme } from "@react-navigation/native";
import { useMutation } from "@tanstack/react-query";
import { LockIcon, ShieldAlertIcon, UserIcon } from "lucide-react-native";
import { LockIcon, ShieldAlertIcon, UserIcon, RectangleEllipsis } from "lucide-react-native";
import { z } from "zod";

import { Button } from "~/components/button";
Expand All @@ -27,6 +27,8 @@ import { TextInput } from "~/components/themed/text-input";
import { TransparentHeaderUntilScrolled } from "~/components/transparent-header";
import { useAuth } from "~/lib/agent";
import { useLinkPress } from "~/lib/hooks/link-press";
import { is2FactorError } from "~/lib/utils/login";


export default function SignIn() {
const { login: authLogin } = useAuth();
Expand All @@ -39,6 +41,8 @@ export default function SignIn() {

const [identifier, setIdentifier] = useState(handle ?? "");
const [password, setPassword] = useState("");
const [requiredTwoFactor, setRequiredTwoFactor] = useState(false);
const [twoFactor, setTwoFactor] = useState<string>("");
const [hasFocusedPassword, setHasFocusedPassword] = useState(false);

const login = useMutation({
Expand All @@ -47,7 +51,7 @@ export default function SignIn() {
const normalizedIdentifier = identifier.startsWith("@")
? identifier.slice(1).trim()
: identifier.trim();
await authLogin(normalizedIdentifier, password);
await authLogin(normalizedIdentifier, password, twoFactor !== '' ? twoFactor : undefined);
},
onSuccess: () => {
// Dismiss the auth modal and navigate to feeds
Expand All @@ -56,12 +60,17 @@ export default function SignIn() {
}
router.replace("/(feeds)/feeds");
},
onError: (err) =>
onError: (err) => {
if (is2FactorError(err)) {
setRequiredTwoFactor(true);
return;
}
showToastable({
title: _(msg`Could not log you in`),
message: err instanceof Error ? err.message : _(msg`Unknown error`),
status: "warning",
}),
})
},
});

return (
Expand Down Expand Up @@ -163,6 +172,20 @@ export default function SignIn() {
</View>
</Animated.View>
)}

{requiredTwoFactor && (<View
className="mb-1 flex-row items-center rounded-b-sm rounded-t-lg pl-3"
style={{ backgroundColor: theme.colors.card }}
>
<RectangleEllipsis size={18} color="rgb(163 163 163)" />
<TextInput
className="flex-1 flex-row items-center px-2 py-3 text-base leading-5"
placeholder={_(msg`2Factor Code`)}
value={twoFactor}
onChangeText={setTwoFactor}
autoCapitalize="none"
/>
</View>)}
</KeyboardAwareScrollView>
</TransparentHeaderUntilScrolled>
<KeyboardStickyView
Expand Down
7 changes: 7 additions & 0 deletions apps/expo/src/lib/utils/login.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

/* matches: 'A sign in code has been sent to your email address' */
const TwoFactorRegex = /sign in code.*sent/i

export function is2FactorError(error: Error): boolean {
return TwoFactorRegex.test(error?.message)
}