import 'package:firebase_auth/firebase_auth.dart'; import 'package:flutter/foundation.dart'; class AuthService { final FirebaseAuth _auth = FirebaseAuth.instance; // Get current user User? get currentUser => _auth.currentUser; // Auth state changes stream Stream get authStateChanges => _auth.authStateChanges(); // Sign in with email and password Future signInWithEmailAndPassword({ required String email, required String password, }) async { try { UserCredential result = await _auth.signInWithEmailAndPassword( email: email, password: password, ); return result; } on FirebaseAuthException catch (e) { if (kDebugMode) { print('Firebase Auth Error: ${e.message}'); } throw _handleAuthException(e); } catch (e) { if (kDebugMode) { print('General Auth Error: $e'); } throw 'Terjadi kesalahan saat login'; } } // Register with email and password Future registerWithEmailAndPassword({ required String email, required String password, }) async { try { UserCredential result = await _auth.createUserWithEmailAndPassword( email: email, password: password, ); return result; } on FirebaseAuthException catch (e) { if (kDebugMode) { print('Firebase Auth Error: ${e.message}'); } throw _handleAuthException(e); } catch (e) { if (kDebugMode) { print('General Auth Error: $e'); } throw 'Terjadi kesalahan saat mendaftar'; } } // Sign out Future signOut() async { try { await _auth.signOut(); } catch (e) { if (kDebugMode) { print('Sign out error: $e'); } throw 'Terjadi kesalahan saat logout'; } } // Reset password Future sendPasswordResetEmail({required String email}) async { try { await _auth.sendPasswordResetEmail(email: email); } on FirebaseAuthException catch (e) { if (kDebugMode) { print('Password reset error: ${e.message}'); } throw _handleAuthException(e); } catch (e) { if (kDebugMode) { print('General password reset error: $e'); } throw 'Terjadi kesalahan saat mengirim email reset password'; } } // Update user profile Future updateUserProfile({ String? displayName, String? photoURL, }) async { try { await currentUser?.updateDisplayName(displayName); if (photoURL != null) { await currentUser?.updatePhotoURL(photoURL); } } catch (e) { if (kDebugMode) { print('Update profile error: $e'); } throw 'Terjadi kesalahan saat update profil'; } } // Handle Firebase Auth exceptions String _handleAuthException(FirebaseAuthException e) { switch (e.code) { case 'weak-password': return 'Password terlalu lemah'; case 'email-already-in-use': return 'Email sudah digunakan'; case 'invalid-email': return 'Email tidak valid'; case 'user-not-found': return 'User tidak ditemukan'; case 'wrong-password': return 'Password salah'; case 'user-disabled': return 'Akun telah dinonaktifkan'; case 'too-many-requests': return 'Terlalu banyak percobaan login. Coba lagi nanti'; case 'operation-not-allowed': return 'Operasi tidak diizinkan'; case 'invalid-credential': return 'Kredensial tidak valid'; case 'network-request-failed': return 'Koneksi internet bermasalah'; default: return e.message ?? 'Terjadi kesalahan yang tidak diketahui'; } } }