From efcb18a98f41f3e5db39e7fc11a1ac97f1f3fef0 Mon Sep 17 00:00:00 2001 From: Till JS Date: Mon, 23 Mar 2026 12:59:13 +0100 Subject: [PATCH] refactor(auth): standardize mobile auth return format to { success, error } Align mobile AuthProviders (chat, manacore) with web auth stores: - Replace { error: { message } | null } with { success: boolean, error?: string } - Add needsVerification support to signUp return type - Update all consumer screens (login, register, reset-password) accordingly All auth methods across web and mobile now use the same return pattern. Co-Authored-By: Claude Opus 4.6 (1M context) --- apps/chat/apps/mobile/app/auth/login.tsx | 6 +-- apps/chat/apps/mobile/app/auth/register.tsx | 14 +++-- .../apps/mobile/app/auth/reset-password.tsx | 6 +-- .../chat/apps/mobile/context/AuthProvider.tsx | 53 +++++++++---------- apps/manacore/apps/mobile/components/Auth.tsx | 23 ++++---- .../apps/mobile/context/AuthProvider.tsx | 48 +++++++++-------- 6 files changed, 81 insertions(+), 69 deletions(-) diff --git a/apps/chat/apps/mobile/app/auth/login.tsx b/apps/chat/apps/mobile/app/auth/login.tsx index 793405288..8987a749d 100644 --- a/apps/chat/apps/mobile/app/auth/login.tsx +++ b/apps/chat/apps/mobile/app/auth/login.tsx @@ -34,10 +34,10 @@ export default function LoginScreen() { try { setLoading(true); - const { error } = await signIn(email, password); + const result = await signIn(email, password); - if (error) { - Alert.alert('Anmeldung fehlgeschlagen', error.message || 'Unbekannter Fehler'); + if (!result.success) { + Alert.alert('Anmeldung fehlgeschlagen', result.error || 'Unbekannter Fehler'); } else { // Erfolgreich angemeldet, navigiere zur Hauptseite router.replace('/'); diff --git a/apps/chat/apps/mobile/app/auth/register.tsx b/apps/chat/apps/mobile/app/auth/register.tsx index 219360fba..ad2a58117 100644 --- a/apps/chat/apps/mobile/app/auth/register.tsx +++ b/apps/chat/apps/mobile/app/auth/register.tsx @@ -44,11 +44,17 @@ export default function RegisterScreen() { try { setLoading(true); - const { data, error } = await signUp(email, password); + const result = await signUp(email, password); - if (error) { - Alert.alert('Registrierung fehlgeschlagen', error.message); - } else if (data?.user) { + if (!result.success) { + Alert.alert('Registrierung fehlgeschlagen', result.error || 'Unbekannter Fehler'); + } else if (result.needsVerification) { + Alert.alert( + 'E-Mail bestätigen', + 'Bitte überprüfe dein Postfach und bestätige deine E-Mail-Adresse.', + [{ text: 'OK', onPress: () => router.replace('/auth/login') }] + ); + } else { Alert.alert( 'Registrierung erfolgreich', 'Dein Konto wurde erfolgreich erstellt. Du wirst jetzt angemeldet.', diff --git a/apps/chat/apps/mobile/app/auth/reset-password.tsx b/apps/chat/apps/mobile/app/auth/reset-password.tsx index cec42e890..15d0c0ebe 100644 --- a/apps/chat/apps/mobile/app/auth/reset-password.tsx +++ b/apps/chat/apps/mobile/app/auth/reset-password.tsx @@ -31,10 +31,10 @@ export default function ResetPasswordScreen() { try { setLoading(true); - const { error } = await resetPassword(email); + const result = await resetPassword(email); - if (error) { - Alert.alert('Fehler', error.message); + if (!result.success) { + Alert.alert('Fehler', result.error || 'Unbekannter Fehler'); } else { Alert.alert( 'E-Mail gesendet', diff --git a/apps/chat/apps/mobile/context/AuthProvider.tsx b/apps/chat/apps/mobile/context/AuthProvider.tsx index b68550103..2f5ad78f2 100644 --- a/apps/chat/apps/mobile/context/AuthProvider.tsx +++ b/apps/chat/apps/mobile/context/AuthProvider.tsx @@ -81,13 +81,18 @@ const authService = createAuthService({ baseUrl: MANA_AUTH_URL }); const tokenManager = createTokenManager(authService); // Auth context type +type AuthResult = { success: boolean; error?: string }; + type AuthContextType = { user: UserData | null; loading: boolean; - signIn: (email: string, password: string) => Promise<{ error: any | null }>; - signUp: (email: string, password: string) => Promise<{ error: any | null; data: any | null }>; + signIn: (email: string, password: string) => Promise; + signUp: ( + email: string, + password: string + ) => Promise; signOut: () => Promise; - resetPassword: (email: string) => Promise<{ error: any | null }>; + resetPassword: (email: string) => Promise; }; // Create auth context @@ -132,25 +137,21 @@ export function AuthProvider({ children }: { children: React.ReactNode }) { }, []); // Sign in with email and password - const signIn = async (email: string, password: string) => { + const signIn = async (email: string, password: string): Promise => { try { - console.log('Versuche Anmeldung mit:', email); const result = await authService.signIn(email, password); if (!result.success) { - console.error('Auth Fehler:', result.error); - return { error: { message: result.error } }; + return { success: false, error: result.error || 'Login failed' }; } - // Get user data from token const userData = await authService.getUserFromToken(); setUser(userData); - console.log('Anmeldung erfolgreich:', userData?.userId); - return { error: null }; + return { success: true }; } catch (error: any) { - console.error('Unerwarteter Fehler beim Anmelden:', error.message || error); - return { error }; + console.error('Fehler beim Anmelden:', error.message || error); + return { success: false, error: error.message || 'Unknown error' }; } }; @@ -160,20 +161,18 @@ export function AuthProvider({ children }: { children: React.ReactNode }) { const result = await authService.signUp(email, password); if (!result.success) { - return { data: null, error: { message: result.error } }; + return { success: false, error: result.error || 'Signup failed' }; + } + + if (result.needsVerification) { + return { success: true, needsVerification: true }; } // Auto sign in after successful signup - const signInResult = await signIn(email, password); - - if (signInResult.error) { - return { data: null, error: signInResult.error }; - } - - return { data: user, error: null }; - } catch (error) { + return signIn(email, password); + } catch (error: any) { console.error('Fehler beim Registrieren:', error); - return { data: null, error }; + return { success: false, error: error.message || 'Unknown error' }; } }; @@ -188,18 +187,18 @@ export function AuthProvider({ children }: { children: React.ReactNode }) { }; // Reset password - const resetPassword = async (email: string) => { + const resetPassword = async (email: string): Promise => { try { const result = await authService.forgotPassword(email); if (!result.success) { - return { error: { message: result.error } }; + return { success: false, error: result.error || 'Password reset failed' }; } - return { error: null }; - } catch (error) { + return { success: true }; + } catch (error: any) { console.error('Fehler beim Zurücksetzen des Passworts:', error); - return { error }; + return { success: false, error: error.message || 'Unknown error' }; } }; diff --git a/apps/manacore/apps/mobile/components/Auth.tsx b/apps/manacore/apps/mobile/components/Auth.tsx index 34f8a3c19..76da4dc1d 100644 --- a/apps/manacore/apps/mobile/components/Auth.tsx +++ b/apps/manacore/apps/mobile/components/Auth.tsx @@ -16,20 +16,25 @@ export default function Auth() { async function signInWithEmail() { setLoading(true); - const { error } = await signIn(email, password); + const result = await signIn(email, password); - if (error) { - Alert.alert('Fehler bei der Anmeldung', error.message || 'Anmeldung fehlgeschlagen'); + if (!result.success) { + Alert.alert('Fehler bei der Anmeldung', result.error || 'Anmeldung fehlgeschlagen'); } setLoading(false); } async function signUpWithEmail() { setLoading(true); - const { error } = await signUp(email, password); + const result = await signUp(email, password); - if (error) { - Alert.alert('Fehler bei der Registrierung', error.message || 'Registrierung fehlgeschlagen'); + if (!result.success) { + Alert.alert('Fehler bei der Registrierung', result.error || 'Registrierung fehlgeschlagen'); + } else if (result.needsVerification) { + Alert.alert( + 'E-Mail bestätigen', + 'Bitte überprüfen Sie Ihre E-Mail und bestätigen Sie Ihre Adresse.' + ); } else { Alert.alert( 'Registrierung erfolgreich', @@ -48,10 +53,10 @@ export default function Auth() { setLoading(true); try { - const { error } = await resetPassword(email); + const result = await resetPassword(email); - if (error) { - Alert.alert('Fehler', error.message || 'Fehler beim Zurücksetzen des Passworts'); + if (!result.success) { + Alert.alert('Fehler', result.error || 'Fehler beim Zurücksetzen des Passworts'); } else { Alert.alert( 'E-Mail gesendet', diff --git a/apps/manacore/apps/mobile/context/AuthProvider.tsx b/apps/manacore/apps/mobile/context/AuthProvider.tsx index 00f2b442a..c8d48cb6f 100644 --- a/apps/manacore/apps/mobile/context/AuthProvider.tsx +++ b/apps/manacore/apps/mobile/context/AuthProvider.tsx @@ -81,13 +81,18 @@ const authService = createAuthService({ baseUrl: MANA_AUTH_URL }); const tokenManager = createTokenManager(authService); // Auth context type +type AuthResult = { success: boolean; error?: string }; + type AuthContextType = { user: UserData | null; loading: boolean; - signIn: (email: string, password: string) => Promise<{ error: any | null }>; - signUp: (email: string, password: string) => Promise<{ error: any | null; data: any | null }>; + signIn: (email: string, password: string) => Promise; + signUp: ( + email: string, + password: string + ) => Promise; signOut: () => Promise; - resetPassword: (email: string) => Promise<{ error: any | null }>; + resetPassword: (email: string) => Promise; }; // Create auth context @@ -132,22 +137,21 @@ export function AuthProvider({ children }: { children: React.ReactNode }) { }, []); // Sign in with email and password - const signIn = async (email: string, password: string) => { + const signIn = async (email: string, password: string): Promise => { try { const result = await authService.signIn(email, password); if (!result.success) { - return { error: { message: result.error } }; + return { success: false, error: result.error || 'Login failed' }; } - // Get user data from token const userData = await authService.getUserFromToken(); setUser(userData); - return { error: null }; + return { success: true }; } catch (error: any) { console.error('Fehler beim Anmelden:', error.message || error); - return { error }; + return { success: false, error: error.message || 'Unknown error' }; } }; @@ -157,20 +161,18 @@ export function AuthProvider({ children }: { children: React.ReactNode }) { const result = await authService.signUp(email, password); if (!result.success) { - return { data: null, error: { message: result.error } }; + return { success: false, error: result.error || 'Signup failed' }; + } + + if (result.needsVerification) { + return { success: true, needsVerification: true }; } // Auto sign in after successful signup - const signInResult = await signIn(email, password); - - if (signInResult.error) { - return { data: null, error: signInResult.error }; - } - - return { data: user, error: null }; - } catch (error) { + return signIn(email, password); + } catch (error: any) { console.error('Fehler beim Registrieren:', error); - return { data: null, error }; + return { success: false, error: error.message || 'Unknown error' }; } }; @@ -185,18 +187,18 @@ export function AuthProvider({ children }: { children: React.ReactNode }) { }; // Reset password - const resetPassword = async (email: string) => { + const resetPassword = async (email: string): Promise => { try { const result = await authService.forgotPassword(email); if (!result.success) { - return { error: { message: result.error } }; + return { success: false, error: result.error || 'Password reset failed' }; } - return { error: null }; - } catch (error) { + return { success: true }; + } catch (error: any) { console.error('Fehler beim Zurücksetzen des Passworts:', error); - return { error }; + return { success: false, error: error.message || 'Unknown error' }; } };