service 2-5-26-1
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:service/constant/links.dart';
|
||||
import 'package:service/controller/functions/crud.dart';
|
||||
import 'package:service/controller/functions/device_helper.dart';
|
||||
import 'package:service/controller/functions/encrypt_decrypt.dart';
|
||||
|
||||
import '../constant/box_name.dart';
|
||||
import '../main.dart';
|
||||
@@ -12,73 +13,73 @@ import '../views/home/main.dart';
|
||||
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
|
||||
import 'functions/initilize.dart';
|
||||
|
||||
class LoginController extends GetxController {
|
||||
var email = TextEditingController();
|
||||
var email = TextEditingController(); // Kept for UI compatibility
|
||||
var password = TextEditingController();
|
||||
final formKey = GlobalKey<FormState>();
|
||||
|
||||
// Use FlutterSecureStorage instead of GetStorage
|
||||
final FlutterSecureStorage storage = const FlutterSecureStorage();
|
||||
|
||||
void login() async {
|
||||
String? storedEmail = await storage.read(key: 'email');
|
||||
// Ensure fingerprint is ready
|
||||
String fingerprint = box.read(BoxName.fingerPrint) ?? '';
|
||||
if (fingerprint.isEmpty) {
|
||||
fingerprint = await DeviceHelper.getDeviceFingerprint();
|
||||
}
|
||||
|
||||
String? storedPassword = await storage.read(key: 'password');
|
||||
String pass = storedPassword ?? password.text;
|
||||
|
||||
await CRUD()
|
||||
.getJWT(storedPassword ?? password.text, storedEmail ?? email.text);
|
||||
if (pass.isEmpty && !formKey.currentState!.validate()) return;
|
||||
|
||||
if (storedEmail != null) {
|
||||
var payload = {
|
||||
"email": storedEmail,
|
||||
"password": storedPassword,
|
||||
};
|
||||
Log.print('payload: ${payload}');
|
||||
var res = await CRUD().get(link: AppLink.login, payload: payload);
|
||||
Log.print('res: ${res}');
|
||||
var payload = {
|
||||
"fingerprint": fingerprint,
|
||||
"password": pass,
|
||||
"aud": "service",
|
||||
};
|
||||
|
||||
// if (res != 'failure') {
|
||||
var d = jsonDecode(res);
|
||||
Log.print('d: ${d}');
|
||||
if (d['message'] == "Login successful") {
|
||||
// Save data securely in FlutterSecureStorage
|
||||
Log.print('🚀 Login Payload: $payload');
|
||||
var res = await CRUD().post(link: AppLink.login, payload: payload);
|
||||
Log.print('📥 Login Response: $res');
|
||||
|
||||
await box.write(BoxName.employeename, d['data']['first_name']);
|
||||
Get.off(() => Main());
|
||||
// }
|
||||
}
|
||||
if (res != 'failure' && res is Map && res['status'] == 'success') {
|
||||
var d = res['message']; // V1 returns {status, message: {jwt, data: {user...}}}
|
||||
|
||||
// Store JWT
|
||||
final jwt = d['jwt'];
|
||||
box.write(BoxName.jwt, c(jwt));
|
||||
|
||||
// Store User Data
|
||||
var userData = d['data'];
|
||||
await storage.write(key: 'name', value: userData['first_name']);
|
||||
await storage.write(key: 'driverID', value: userData['id'].toString());
|
||||
await storage.write(key: 'password', value: pass);
|
||||
await box.write(BoxName.employeename, userData['first_name']);
|
||||
await box.write(BoxName.password, pass);
|
||||
|
||||
Get.offAll(() => Main());
|
||||
} else {
|
||||
if (formKey.currentState!.validate()) {
|
||||
var payload = {
|
||||
"email": storedEmail ?? email.text,
|
||||
"password": storedPassword ?? password.text,
|
||||
};
|
||||
Log.print('payload: ${payload}');
|
||||
var res = await CRUD().get(link: AppLink.login, payload: payload);
|
||||
Log.print('res: ${res}');
|
||||
|
||||
// if (res != 'failure') {
|
||||
var d = jsonDecode(res);
|
||||
Log.print('d: ${d}');
|
||||
if (d['message'] == "Login successful") {
|
||||
// Save data securely in FlutterSecureStorage
|
||||
await storage.write(key: 'email', value: d['data']['email']);
|
||||
await storage.write(key: 'name', value: d['data']['first_name']);
|
||||
await storage.write(key: 'driverID', value: d['data']['id']);
|
||||
await storage.write(key: 'password', value: password.text);
|
||||
await box.write(BoxName.employeename, d['data']['first_name']);
|
||||
|
||||
Get.off(() => Main());
|
||||
// }
|
||||
}
|
||||
}
|
||||
Get.snackbar(
|
||||
'خطأ'.tr,
|
||||
res is Map ? res['message'].toString().tr : 'فشل تسجيل الدخول'.tr,
|
||||
backgroundColor: Colors.red.withOpacity(0.7),
|
||||
colorText: Colors.white,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
login();
|
||||
void onInit() async {
|
||||
await EncryptionHelper.initialize();
|
||||
await DeviceHelper.getDeviceFingerprint();
|
||||
|
||||
// Auto login if credentials exist
|
||||
String? storedPassword = await storage.read(key: 'password');
|
||||
if (storedPassword != null) {
|
||||
login();
|
||||
}
|
||||
|
||||
|
||||
super.onInit();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user