mirror of
https://github.com/Memo-2023/mana-monorepo.git
synced 2026-05-14 20:01:09 +02:00
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) <noreply@anthropic.com>
This commit is contained in:
parent
71277ba7aa
commit
efcb18a98f
6 changed files with 81 additions and 69 deletions
|
|
@ -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('/');
|
||||
|
|
|
|||
|
|
@ -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.',
|
||||
|
|
|
|||
|
|
@ -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',
|
||||
|
|
|
|||
|
|
@ -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<AuthResult>;
|
||||
signUp: (
|
||||
email: string,
|
||||
password: string
|
||||
) => Promise<AuthResult & { needsVerification?: boolean }>;
|
||||
signOut: () => Promise<void>;
|
||||
resetPassword: (email: string) => Promise<{ error: any | null }>;
|
||||
resetPassword: (email: string) => Promise<AuthResult>;
|
||||
};
|
||||
|
||||
// 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<AuthResult> => {
|
||||
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<AuthResult> => {
|
||||
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' };
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -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',
|
||||
|
|
|
|||
|
|
@ -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<AuthResult>;
|
||||
signUp: (
|
||||
email: string,
|
||||
password: string
|
||||
) => Promise<AuthResult & { needsVerification?: boolean }>;
|
||||
signOut: () => Promise<void>;
|
||||
resetPassword: (email: string) => Promise<{ error: any | null }>;
|
||||
resetPassword: (email: string) => Promise<AuthResult>;
|
||||
};
|
||||
|
||||
// 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<AuthResult> => {
|
||||
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<AuthResult> => {
|
||||
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' };
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue