From dbf020b1d4fa72bdc00c5eec004cbefdf196a663 Mon Sep 17 00:00:00 2001 From: amean Date: Sat, 12 Apr 2025 16:27:33 +0330 Subject: [PATCH] set auth log --- lib/screens/auth/login_screen.dart | 2 ++ lib/screens/auth/otp_screen.dart | 1 + lib/screens/auth/state/state.dart | 34 +++++++++++++++++++++++ lib/services/auth/auth.dart | 44 ++++++++++++++++++++++++++++++ lib/splash_screen.dart | 1 + pubspec.lock | 44 +++++++++++++++--------------- 6 files changed, 104 insertions(+), 22 deletions(-) diff --git a/lib/screens/auth/login_screen.dart b/lib/screens/auth/login_screen.dart index dbd2ad1..be6349f 100644 --- a/lib/screens/auth/login_screen.dart +++ b/lib/screens/auth/login_screen.dart @@ -158,6 +158,8 @@ class _LoginScreenState extends State { mobile: phoneController.text, password: passwordController.text); if (status == Status.ready) { + final status = await state.authLog(); + print(status); context.goNamed('navigate', pathParameters: {'tab': '0'}); } else { Tools.showCustomSnackBar( diff --git a/lib/screens/auth/otp_screen.dart b/lib/screens/auth/otp_screen.dart index 9332a83..dec8ae9 100644 --- a/lib/screens/auth/otp_screen.dart +++ b/lib/screens/auth/otp_screen.dart @@ -188,6 +188,7 @@ class _OtpScreenState extends State { final status = await value.login(mobile: widget.phoneNumber, otp: onSubmitted); if (status == Status.ready) { + await value.authLog(); context.goNamed('navigate', pathParameters: {'tab': '0'}); } else if (status == Status.error) { Tools.showCustomSnackBar( diff --git a/lib/screens/auth/state/state.dart b/lib/screens/auth/state/state.dart index 087093c..704c90e 100644 --- a/lib/screens/auth/state/state.dart +++ b/lib/screens/auth/state/state.dart @@ -130,5 +130,39 @@ class AuthState extends ChangeNotifier { // print(statusCheckLogin); return statusCheckLogin; } + // auth log + Status statusAuthLog = Status.empty; + String? messageAuthLog; + Map? errorsAuthLog; + Future authLog() async { + statusAuthLog = Status.loading; + notifyListeners(); + try { + final result = await authServises.authLog(); + if (result == null) { + statusAuthLog = Status.error; + } else { + print(result.message); + if (result.isOk) { + statusAuthLog = Status.ready; + messageAuthLog = result.message; + } else if (result.isOk == false) { + errorsAuthLog = result.errors; + messageAuthLog = result.message; + statusAuthLog = Status.error; + } else { + statusAuthLog = Status.error; + } + notifyListeners(); + } + notifyListeners(); + } catch (e) { + statusAuthLog = Status.error; + print(e); + } + notifyListeners(); + print(statusAuthLog); + return statusAuthLog; + } } diff --git a/lib/services/auth/auth.dart b/lib/services/auth/auth.dart index a2fef11..2ce3304 100644 --- a/lib/services/auth/auth.dart +++ b/lib/services/auth/auth.dart @@ -115,4 +115,48 @@ class AuthServices { return const Result(isOk: false); } + + Future authLog() async { + try { + print("Starting authLog..."); + Map headers = { + "Accept": "application/json", + "User-Agent": Platform.isAndroid ? "android" : "ios" + }; + String dataToken = setting.userLocalDb.getUser().token!; + print("Retrieved token: $dataToken"); + + if (dataToken != '') { + headers['Authorization'] = "Bearer $dataToken"; + print("Authorization header set."); + } + + // لینک API + final apiUrl = "${config.network.baseUrl}auth-log"; + print("API URL: $apiUrl"); + + final res = await Dio().post( + apiUrl, + options: Options(headers: headers), + ); + + print("Response received: ${res.statusCode}"); + if (res.statusCode == 200 || res.statusCode == 204) { + print("Success: ${res.data}"); + return Result(isOk: true, message: res.data['msg']); + } + } on DioException catch (e) { + print("DioException occurred: ${e.response?.data}"); + return Result( + isOk: false, + errors: e.response?.data['errors'], + message: e.response?.data['msg'], + ); + } catch (e) { + print("Unexpected error: $e"); + } + + print("Returning default failure result."); + return const Result(isOk: false); + } } diff --git a/lib/splash_screen.dart b/lib/splash_screen.dart index d14020d..c271bc4 100644 --- a/lib/splash_screen.dart +++ b/lib/splash_screen.dart @@ -63,6 +63,7 @@ class _SplashScreenState extends State { if (token != '') { final Status checkLogin = await authState.CheckLogin(); if (checkLogin == Status.ready) { + final Status checkLogin = await authState.authLog(); context.goNamed('navigate', pathParameters: {'tab': '0'}); } else { context.goNamed('login'); diff --git a/pubspec.lock b/pubspec.lock index 06b7bd4..1c1e496 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -5,10 +5,10 @@ packages: dependency: transitive description: name: _fe_analyzer_shared - sha256: f256b0c0ba6c7577c15e2e4e114755640a875e885099367bf6e012b19314c834 + sha256: "16e298750b6d0af7ce8a3ba7c18c69c3785d11b15ec83f6dcd0ad2a0009b3cab" url: "https://pub.dev" source: hosted - version: "72.0.0" + version: "76.0.0" _flutterfire_internals: dependency: transitive description: @@ -21,15 +21,15 @@ packages: dependency: transitive description: dart source: sdk - version: "0.3.2" + version: "0.3.3" analyzer: dependency: transitive description: name: analyzer - sha256: b652861553cd3990d8ed361f7979dc6d7053a9ac8843fa73820ab68ce5410139 + sha256: "1f14db053a8c23e260789e9b0980fa27f2680dd640932cae5e1137cce0e46e1e" url: "https://pub.dev" source: hosted - version: "6.7.0" + version: "6.11.0" args: dependency: transitive description: @@ -186,10 +186,10 @@ packages: dependency: transitive description: name: collection - sha256: ee67cb0715911d28db6bf4af1026078bd6f0128b07a5f66fb2ed94ec6783c09a + sha256: a1ace0a119f20aabc852d165077c036cd864315bd99b7eaa10a60100341941bf url: "https://pub.dev" source: hosted - version: "1.18.0" + version: "1.19.0" convert: dependency: transitive description: @@ -518,18 +518,18 @@ packages: dependency: transitive description: name: leak_tracker - sha256: "3f87a60e8c63aecc975dda1ceedbc8f24de75f09e4856ea27daf8958f2f0ce05" + sha256: "7bb2830ebd849694d1ec25bf1f44582d6ac531a57a365a803a6034ff751d2d06" url: "https://pub.dev" source: hosted - version: "10.0.5" + version: "10.0.7" leak_tracker_flutter_testing: dependency: transitive description: name: leak_tracker_flutter_testing - sha256: "932549fb305594d82d7183ecd9fa93463e9914e1b67cacc34bc40906594a1806" + sha256: "9491a714cca3667b60b5c420da8217e6de0d1ba7a5ec322fab01758f6998f379" url: "https://pub.dev" source: hosted - version: "3.0.5" + version: "3.0.8" leak_tracker_testing: dependency: transitive description: @@ -550,10 +550,10 @@ packages: dependency: transitive description: name: macros - sha256: "0acaed5d6b7eab89f63350bccd82119e6c602df0f391260d0e32b5e23db79536" + sha256: "1d9e801cd66f7ea3663c45fc708450db1fa57f988142c64289142c9b7ee80656" url: "https://pub.dev" source: hosted - version: "0.1.2-main.4" + version: "0.1.3-main.0" matcher: dependency: transitive description: @@ -862,7 +862,7 @@ packages: dependency: transitive description: flutter source: sdk - version: "0.0.99" + version: "0.0.0" source_gen: dependency: transitive description: @@ -915,10 +915,10 @@ packages: dependency: transitive description: name: stack_trace - sha256: "73713990125a6d93122541237550ee3352a2d84baad52d375a4cad2eb9b7ce0b" + sha256: "9f47fd3630d76be3ab26f0ee06d213679aa425996925ff3feffdec504931c377" url: "https://pub.dev" source: hosted - version: "1.11.1" + version: "1.12.0" stream_channel: dependency: transitive description: @@ -939,10 +939,10 @@ packages: dependency: transitive description: name: string_scanner - sha256: "556692adab6cfa87322a115640c11f13cb77b3f076ddcc5d6ae3c20242bedcde" + sha256: "688af5ed3402a4bde5b3a6c15fd768dbf2621a614950b17f04626c431ab3c4c3" url: "https://pub.dev" source: hosted - version: "1.2.0" + version: "1.3.0" synchronized: dependency: transitive description: @@ -963,10 +963,10 @@ packages: dependency: transitive description: name: test_api - sha256: "5b8a98dafc4d5c4c9c72d8b31ab2b23fc13422348d2997120294d3bac86b4ddb" + sha256: "664d3a9a64782fcdeb83ce9c6b39e78fd2971d4e37827b9b06c3aa1edc5e760c" url: "https://pub.dev" source: hosted - version: "0.7.2" + version: "0.7.3" timing: dependency: transitive description: @@ -1003,10 +1003,10 @@ packages: dependency: transitive description: name: vm_service - sha256: "5c5f338a667b4c644744b661f309fb8080bb94b18a7e91ef1dbd343bed00ed6d" + sha256: f6be3ed8bd01289b34d679c2b62226f63c0e69f9fd2e50a6b3c1c729a961041b url: "https://pub.dev" source: hosted - version: "14.2.5" + version: "14.3.0" watcher: dependency: transitive description: