MIF_E31230910_MP-HRIS-MOBILE/lib/providers/auth_provider.dart

162 lines
4.1 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../../repositories/auth_repository.dart';
import '../../models/user_model.dart';
import '../../core/cache_manager.dart';
import '../../services/reminder_service.dart';
class AuthProvider extends ChangeNotifier {
final AuthRepository _repository = AuthRepository();
UserModel? _user;
UserModel? get user => _user;
bool _isLoading = false;
bool get isLoading => _isLoading;
String? _errorMessage;
String? get errorMessage => _errorMessage;
bool get isLoggedIn => _user != null;
Future<bool> login(String email, String password) async {
_isLoading = true;
_errorMessage = null;
notifyListeners();
try {
final result = await _repository.login(email, password);
if (result['success'] == true) {
final data = result['data'];
final token = data['token'];
final userData = data['user'];
await CacheManager.authBox.put('auth_token', token);
await CacheManager.authBox.put('user_data', userData);
_user = UserModel.fromJson(userData);
_isLoading = false;
notifyListeners();
return true;
} else {
_errorMessage = result['message'] ?? 'Login gagal';
_isLoading = false;
notifyListeners();
return false;
}
} catch (e) {
_errorMessage = e.toString();
_isLoading = false;
notifyListeners();
return false;
}
}
Future<void> refreshUser() async {
try {
final userData = await _repository.getUser();
if (userData['success'] == true && userData['data'] != null) {
_user = UserModel.fromJson(userData['data']);
await CacheManager.authBox.put('user_data', userData['data']);
notifyListeners();
}
} catch (e) {
}
}
Future<bool> changePassword(String currentPassword, String newPassword, String confirmPassword) async {
_isLoading = true;
_errorMessage = null;
notifyListeners();
try {
final result = await _repository.changePassword(
currentPassword,
newPassword,
confirmPassword
);
_isLoading = false;
if (result['success'] == true) {
notifyListeners();
return true;
} else {
_errorMessage = result['message'] ?? 'Gagal mengubah password';
notifyListeners();
return false;
}
} catch (e) {
_isLoading = false;
_errorMessage = e.toString();
notifyListeners();
return false;
}
}
Future<bool> updateProfile({String? noTelp, String? alamat, File? foto}) async {
_isLoading = true;
_errorMessage = null;
notifyListeners();
try {
final result = await _repository.updateProfile(
noTelp: noTelp,
alamat: alamat,
foto: foto,
);
_isLoading = false;
if (result['success'] == true) {
await refreshUser();
notifyListeners();
return true;
} else {
_errorMessage = result['message'] ?? 'Gagal memperbarui profil';
notifyListeners();
return false;
}
} catch (e) {
_isLoading = false;
_errorMessage = e.toString();
notifyListeners();
return false;
}
}
Future<void> logout() async {
// Langkah 1: Beritahu server untuk invalidasi token API dan hapus FCM token
try {
final token = CacheManager.authBox.get('auth_token');
if (token != null) {
await _repository.logout();
}
} catch (_) {}
// MB-23: Langkah 2: Bersihkan SELURUH data lokal secara menyeluruh
// Hive boxes
await CacheManager.authBox.clear();
if (CacheManager.settingsBox.isOpen) {
await CacheManager.settingsBox.clear();
}
// SharedPreferences pastikan semua key terhapus
final prefs = await SharedPreferences.getInstance();
await prefs.clear();
// Langkah 3: Reset state provider
_user = null;
_errorMessage = null;
// Langkah 4: Batalkan semua reminder lokal
ReminderService().cancelAll();
notifyListeners();
}
}