Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

feat(mobile): add two-factor authentication support (#1342)

Corentin Thomasset2079a954cb15
diff --git a/apps/mobile/src/modules/auth/auth.client.ts b/apps/mobile/src/modules/auth/auth.client.ts
index 46facd2c..bf76dd74 100644
--- a/apps/mobile/src/modules/auth/auth.client.ts
+++ b/apps/mobile/src/modules/auth/auth.client.ts
@@ -1,4 +1,5 @@
import { expoClient } from '@better-auth/expo/client';
+import { twoFactorClient } from 'better-auth/client/plugins';
import { createAuthClient as createBetterAuthClient } from 'better-auth/react';
import * as SecureStore from 'expo-secure-store';
import { Platform } from 'react-native';
@@ -10,6 +11,7 @@ export function createAuthClient({ baseUrl }: { baseUrl: string }) {
return createBetterAuthClient({
baseURL: baseUrl,
plugins: [
+ twoFactorClient(),
expoClient({
scheme: APP_SCHEME,
storagePrefix: APP_SCHEME,
diff --git a/apps/mobile/src/modules/auth/components/two-factor-verification.tsx b/apps/mobile/src/modules/auth/components/two-factor-verification.tsx
new file mode 100644
index 00000000..11a7c8d5
--- /dev/null
+++ b/apps/mobile/src/modules/auth/components/two-factor-verification.tsx
@@ -0,0 +1,191 @@
+import type { ThemeColors } from '@/modules/ui/theme.constants';
+import { useState } from 'react';
+import {
+ ActivityIndicator,
+ StyleSheet,
+ Switch,
+ Text,
+ TextInput,
+ TouchableOpacity,
+ View,
+} from 'react-native';
+import { useAuthClient } from '@/modules/api/providers/api.provider';
+import { useAlert } from '@/modules/ui/providers/alert-provider';
+import { useThemeColor } from '@/modules/ui/providers/use-theme-color';
+
+const TOTP_CODE_LENGTH = 6;
+
+export function TwoFactorVerificationForm({
+ onSuccess,
+ onBack,
+}: {
+ onSuccess: () => void;
+ onBack: () => void;
+}) {
+ const themeColors = useThemeColor();
+ const authClient = useAuthClient();
+ const { showAlert } = useAlert();
+
+ const [useBackupCode, setUseBackupCode] = useState(false);
+ const [code, setCode] = useState('');
+ const [trustDevice, setTrustDevice] = useState(false);
+ const [isVerifying, setIsVerifying] = useState(false);
+
+ const styles = createStyles({ themeColors });
+
+ const verifyCode = async (codeToVerify: string) => {
+ setIsVerifying(true);
+ try {
+ const response = useBackupCode
+ ? await authClient.twoFactor.verifyBackupCode({ code: codeToVerify, trustDevice })
+ : await authClient.twoFactor.verifyTotp({ code: codeToVerify, trustDevice });
+
+ if (response.error) {
+ throw new Error(response.error.message);
+ }
+
+ onSuccess();
+ } catch (error) {
+ showAlert({
+ title: 'Verification Failed',
+ message: error instanceof Error ? error.message : 'An error occurred',
+ });
+ } finally {
+ setIsVerifying(false);
+ }
+ };
+
+ const handleCodeChange = async (newCode: string) => {
+ setCode(newCode);
+
+ if (!useBackupCode && newCode.length === TOTP_CODE_LENGTH && !isVerifying) {
+ await verifyCode(newCode);
+ }
+ };
+
+ const switchCodeMode = () => {
+ setUseBackupCode(!useBackupCode);
+ setCode('');
+ };
+
+ return (
+ <View style={styles.formContainer}>
+ <View style={styles.fieldContainer}>
+ <Text style={styles.label}>{useBackupCode ? 'Backup code' : 'Verification code'}</Text>
+ <Text style={styles.description}>
+ {useBackupCode
+ ? 'Enter one of your backup codes'
+ : 'Enter the 6-digit code from your authenticator app'}
+ </Text>
+ <TextInput
+ style={styles.input}
+ placeholder={useBackupCode ? 'Enter a backup code' : '000000'}
+ placeholderTextColor={themeColors.mutedForeground}
+ value={code}
+ onChangeText={handleCodeChange}
+ autoCapitalize="none"
+ autoCorrect={false}
+ autoFocus
+ keyboardType={useBackupCode ? 'default' : 'number-pad'}
+ maxLength={useBackupCode ? undefined : TOTP_CODE_LENGTH}
+ textContentType="oneTimeCode"
+ editable={!isVerifying}
+ />
+ </View>
+
+ <View style={styles.trustDeviceRow}>
+ <Text style={styles.trustDeviceLabel}>Trust this device</Text>
+ <Switch
+ value={trustDevice}
+ onValueChange={setTrustDevice}
+ disabled={isVerifying}
+ trackColor={{ true: themeColors.primary }}
+ />
+ </View>
+
+ <TouchableOpacity
+ style={[styles.button, isVerifying && styles.buttonDisabled]}
+ onPress={async () => verifyCode(code)}
+ disabled={isVerifying || code.length === 0}
+ >
+ {isVerifying ? (
+ <ActivityIndicator color={themeColors.primaryForeground} />
+ ) : (
+ <Text style={styles.buttonText}>Verify</Text>
+ )}
+ </TouchableOpacity>
+
+ <TouchableOpacity style={styles.linkButton} onPress={switchCodeMode} disabled={isVerifying}>
+ <Text style={styles.linkText}>
+ {useBackupCode ? 'Use an authenticator code' : 'Use a backup code'}
+ </Text>
+ </TouchableOpacity>
+
+ <TouchableOpacity style={styles.linkButton} onPress={onBack} disabled={isVerifying}>
+ <Text style={styles.linkText}>Back to login</Text>
+ </TouchableOpacity>
+ </View>
+ );
+}
+
+function createStyles({ themeColors }: { themeColors: ThemeColors }) {
+ return StyleSheet.create({
+ formContainer: {
+ gap: 16,
+ },
+ fieldContainer: {
+ gap: 8,
+ },
+ label: {
+ fontSize: 14,
+ fontWeight: '600',
+ color: themeColors.foreground,
+ },
+ description: {
+ fontSize: 14,
+ color: themeColors.mutedForeground,
+ },
+ input: {
+ height: 50,
+ borderWidth: 1,
+ borderColor: themeColors.border,
+ borderRadius: 8,
+ paddingHorizontal: 16,
+ fontSize: 16,
+ color: themeColors.foreground,
+ backgroundColor: themeColors.secondaryBackground,
+ },
+ trustDeviceRow: {
+ flexDirection: 'row',
+ alignItems: 'center',
+ justifyContent: 'space-between',
+ },
+ trustDeviceLabel: {
+ fontSize: 14,
+ color: themeColors.foreground,
+ },
+ button: {
+ height: 50,
+ backgroundColor: themeColors.primary,
+ borderRadius: 8,
+ justifyContent: 'center',
+ alignItems: 'center',
+ marginTop: 8,
+ },
+ buttonDisabled: {
+ opacity: 0.5,
+ },
+ buttonText: {
+ color: themeColors.primaryForeground,
+ fontSize: 16,
+ fontWeight: '600',
+ },
+ linkButton: {
+ alignItems: 'center',
+ },
+ linkText: {
+ color: themeColors.primary,
+ fontSize: 14,
+ },
+ });
+}
diff --git a/apps/mobile/src/modules/auth/screens/login.screen.tsx b/apps/mobile/src/modules/auth/screens/login.screen.tsx
index 736d254c..86625f32 100644
--- a/apps/mobile/src/modules/auth/screens/login.screen.tsx
+++ b/apps/mobile/src/modules/auth/screens/login.screen.tsx
@@ -21,6 +21,7 @@ import { useThemeColor } from '@/modules/ui/providers/use-theme-color';
import { useServerConfig } from '../../config/hooks/use-server-config';
import { getEnabledOAuthProviders } from '../auth.models';
import { BackToServerSelectionButton } from '../components/back-to-server-selection';
+import { TwoFactorVerificationForm } from '../components/two-factor-verification';
const loginSchema = v.object({
email: v.pipe(v.string(), v.email('Please enter a valid email')),
@@ -35,6 +36,7 @@ export function LoginScreen() {
const insets = useSafeAreaInsets();
const [isSubmitting, setIsSubmitting] = useState(false);
+ const [requiresTwoFactor, setRequiresTwoFactor] = useState(false);
const { serverConfig, isLoading: isConfigLoading } = useServerConfig();
const form = useForm({
@@ -59,6 +61,15 @@ export function LoginScreen() {
throw new Error(response.error.message);
}
+ if (
+ response.data &&
+ 'twoFactorRedirect' in response.data &&
+ response.data.twoFactorRedirect
+ ) {
+ setRequiresTwoFactor(true);
+ return;
+ }
+
router.replace('/(app)/(with-organizations)/(tabs)/list');
} catch (error) {
showAlert({
@@ -100,6 +111,30 @@ export function LoginScreen() {
);
}
+ if (requiresTwoFactor) {
+ return (
+ <KeyboardAvoidingView
+ style={{ ...styles.container, paddingTop: insets.top }}
+ behavior={Platform.OS === 'ios' ? 'padding' : 'height'}
+ >
+ <ScrollView
+ contentContainerStyle={styles.scrollContent}
+ keyboardShouldPersistTaps="handled"
+ >
+ <View style={styles.header}>
+ <Text style={styles.title}>Two-Factor Authentication</Text>
+ <Text style={styles.subtitle}>Verify your identity to continue</Text>
+ </View>
+
+ <TwoFactorVerificationForm
+ onSuccess={() => router.replace('/(app)/(with-organizations)/(tabs)/list')}
+ onBack={() => setRequiresTwoFactor(false)}
+ />
+ </ScrollView>
+ </KeyboardAvoidingView>
+ );
+ }
+
return (
<KeyboardAvoidingView
style={{ ...styles.container, paddingTop: insets.top }}