connect with backend
This commit is contained in:
@@ -4,25 +4,43 @@ import 'package:shared_preferences/shared_preferences.dart';
|
||||
import '../services/auth_service.dart';
|
||||
|
||||
class AuthProvider extends ChangeNotifier {
|
||||
final AuthService _service = AuthService();
|
||||
AuthService? _service;
|
||||
|
||||
bool _loading = false;
|
||||
bool get loading => _loading;
|
||||
|
||||
bool initialized = false;
|
||||
|
||||
String? _token;
|
||||
Map<String, dynamic>? _user;
|
||||
|
||||
String? get token => _token;
|
||||
Map<String, dynamic>? get user => _user;
|
||||
|
||||
bool get isLoggedIn => _token != null && _token!.isNotEmpty;
|
||||
|
||||
// Inject context after provider initializes
|
||||
void initContext(BuildContext context) {
|
||||
_service = AuthService(context);
|
||||
}
|
||||
|
||||
AuthProvider() {
|
||||
_loadFromPrefs();
|
||||
}
|
||||
|
||||
// ---------------------- NEW FIX: SAFE INIT -----------------------
|
||||
Future<void> init() async {
|
||||
if (!initialized) {
|
||||
await _loadFromPrefs();
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------- LOAD FROM PREFS -----------------------
|
||||
Future<void> _loadFromPrefs() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
|
||||
_token = prefs.getString('token');
|
||||
|
||||
final userJson = prefs.getString('user');
|
||||
if (userJson != null) {
|
||||
try {
|
||||
@@ -31,22 +49,33 @@ class AuthProvider extends ChangeNotifier {
|
||||
_user = null;
|
||||
}
|
||||
}
|
||||
|
||||
initialized = true;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> login(String loginId, String password) async {
|
||||
// -------------------------- LOGIN -----------------------------
|
||||
Future<Map<String, dynamic>> login(
|
||||
BuildContext context, String loginId, String password) async {
|
||||
_service ??= AuthService(context);
|
||||
|
||||
_loading = true;
|
||||
notifyListeners();
|
||||
|
||||
final res = await _service.login(loginId, password);
|
||||
final res = await _service!.login(loginId, password);
|
||||
|
||||
_loading = false;
|
||||
|
||||
if (res['success'] == true) {
|
||||
final token = res['token']?.toString();
|
||||
final userMap = res['user'] is Map ? Map<String, dynamic>.from(res['user']) : null;
|
||||
final userMap =
|
||||
res['user'] is Map ? Map<String, dynamic>.from(res['user']) : null;
|
||||
|
||||
if (token != null && userMap != null) {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setString("saved_login_id", loginId);
|
||||
await prefs.setString("saved_password", password);
|
||||
|
||||
await _saveSession(token, userMap);
|
||||
}
|
||||
}
|
||||
@@ -55,27 +84,64 @@ class AuthProvider extends ChangeNotifier {
|
||||
return res;
|
||||
}
|
||||
|
||||
Future<void> _saveSession(String token, Map<String, dynamic> userMap) async {
|
||||
// --------------------- SAVE SESSION ---------------------------
|
||||
Future<void> _saveSession(
|
||||
String token, Map<String, dynamic> userMap) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setString('token', token);
|
||||
await prefs.setString('user', jsonEncode(userMap));
|
||||
|
||||
_token = token;
|
||||
_user = userMap;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> logout() async {
|
||||
// optional: call API logout if implemented
|
||||
if (_token != null) {
|
||||
try {
|
||||
await _service.logout(_token!);
|
||||
} catch (_) {}
|
||||
}
|
||||
// ----------------------- LOGOUT -------------------------------
|
||||
Future<void> logout(BuildContext context) async {
|
||||
_service ??= AuthService(context);
|
||||
|
||||
try {
|
||||
await _service!.logout();
|
||||
} catch (_) {}
|
||||
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.remove('token');
|
||||
await prefs.remove('user');
|
||||
|
||||
_token = null;
|
||||
_user = null;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
// -------------------- AUTO LOGIN ------------------------------
|
||||
Future<bool> autoLoginFromSavedCredentials(BuildContext context) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final loginId = prefs.getString('saved_login_id');
|
||||
final password = prefs.getString('saved_password');
|
||||
|
||||
if (loginId == null || password == null) return false;
|
||||
|
||||
final res = await login(context, loginId, password);
|
||||
return res['success'] == true;
|
||||
}
|
||||
|
||||
// --------------------- REFRESH TOKEN --------------------------
|
||||
Future<bool> tryRefreshToken(BuildContext context) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final oldToken = prefs.getString('token');
|
||||
|
||||
if (oldToken == null) return false;
|
||||
|
||||
_service ??= AuthService(context);
|
||||
|
||||
final res = await _service!.refreshToken(oldToken);
|
||||
|
||||
if (res['success'] == true && res['token'] != null) {
|
||||
await prefs.setString('token', res['token']);
|
||||
_token = res['token'];
|
||||
notifyListeners();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user