Fixes reported from APK field testing: DO/Product scan mode was inconsistent between the camera drawer and documents screen (now one shared provider, with an orange/green color cue); unconfirmed scans leaked into history with placeholder data before the user tapped confirm (backend now gates GET /documents on a new `confirmed` column, flipped only by PUT); and Product Scan ran the GPU classifier twice, once at upload and again on review (now a single pass at upload, persisted and read directly by the editor). Also removes the unused "Hubungkan ke PO" field and fabricated PO/SO/DO placeholder values from the Product Scan flow, closes out the per-document-polling and save-recovery tasks (6.1/6.3), and splits several touched files to stay under the repo's 256-line guideline. Full detail in docs/iteration-log.md and backend/docs/iteration-log.md. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
100 lines
3.1 KiB
Dart
100 lines
3.1 KiB
Dart
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import '../../core/network/api_client.dart';
|
|
import '../../config/app_config.dart';
|
|
import '../../main.dart';
|
|
import '../documents/pending_documents_provider.dart';
|
|
|
|
class AuthNotifier extends StateNotifier<bool> {
|
|
AuthNotifier(this._ref) : super(false);
|
|
|
|
final Ref _ref;
|
|
|
|
Future<bool> checkLoginState() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final token = prefs.getString('auth_token');
|
|
if (token != null) {
|
|
state = true;
|
|
try {
|
|
final apiClient = _ref.read(apiClientProvider);
|
|
final response = await apiClient.client.get('/auth/me');
|
|
if (response.data is Map && response.data['data'] is Map) {
|
|
final profile = response.data['data']['profile'];
|
|
if (profile != null) {
|
|
await prefs.setString('nama_toko', profile['namaToko'] ?? '');
|
|
await prefs.setString('kode_toko', profile['kodeToko'] ?? '');
|
|
}
|
|
}
|
|
} catch (e) {
|
|
// If unauthorized, perform clean logout
|
|
if (e.toString().contains('401') || e.toString().contains('Unauthorized')) {
|
|
await logout();
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
state = false;
|
|
return false;
|
|
}
|
|
|
|
Future<void> login(String username, String password) async {
|
|
final apiClient = _ref.read(apiClientProvider);
|
|
try {
|
|
final response = await apiClient.client.post(
|
|
AppConfig.loginEndpoint,
|
|
data: {
|
|
'username': username,
|
|
'password': password,
|
|
},
|
|
);
|
|
|
|
if (response.data is! Map || response.data['data'] is! Map) {
|
|
throw Exception("Format respon server tidak valid (bukan JSON)");
|
|
}
|
|
|
|
final token = response.data['data']['token'];
|
|
final profile = response.data['data']['profile'];
|
|
if (token == null) {
|
|
throw Exception("Token otentikasi tidak ditemukan dalam respon");
|
|
}
|
|
|
|
// Clear any pre-existing local data before saving the new token/profile
|
|
final localStorage = _ref.read(localStorageProvider);
|
|
await localStorage.clearAll();
|
|
_ref.read(pendingDocumentsProvider.notifier).clearQueue();
|
|
|
|
final prefs = await SharedPreferences.getInstance();
|
|
await prefs.setString('auth_token', token);
|
|
if (profile != null) {
|
|
await prefs.setString('nama_toko', profile['namaToko'] ?? '');
|
|
await prefs.setString('kode_toko', profile['kodeToko'] ?? '');
|
|
}
|
|
|
|
state = true;
|
|
} catch (e) {
|
|
state = false;
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
Future<void> logout() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
await prefs.remove('auth_token');
|
|
await prefs.remove('nama_toko');
|
|
await prefs.remove('kode_toko');
|
|
|
|
final localStorage = _ref.read(localStorageProvider);
|
|
await localStorage.clearAll();
|
|
|
|
// Clear pending documents queue in memory
|
|
_ref.read(pendingDocumentsProvider.notifier).clearQueue();
|
|
|
|
state = false;
|
|
}
|
|
}
|
|
|
|
final authProvider = StateNotifierProvider<AuthNotifier, bool>((ref) {
|
|
return AuthNotifier(ref);
|
|
});
|