From 3ff90bd9368ca4c9d5090fbc8902dd8f4d5af9bc Mon Sep 17 00:00:00 2001 From: thaidmfinnick Date: Mon, 15 Jul 2024 12:02:26 +0700 Subject: [PATCH] fix(mobile): update api, remove log --- mobile_app/lib/constants/api_constant.dart | 10 +++- .../bloc/authentication_bloc.dart | 20 +++++++- .../repository/authentication_repo.dart | 14 +++--- .../repository/shared_pref_repo.dart | 2 +- mobile_app/lib/providers/auth_provider.dart | 47 +------------------ .../user_screens/user_profile_screen.dart | 7 ++- 6 files changed, 41 insertions(+), 59 deletions(-) diff --git a/mobile_app/lib/constants/api_constant.dart b/mobile_app/lib/constants/api_constant.dart index 9f6a1bff..b400dd49 100644 --- a/mobile_app/lib/constants/api_constant.dart +++ b/mobile_app/lib/constants/api_constant.dart @@ -1,3 +1,4 @@ +import 'package:bluetooth_ecg/certs/secrets.dart'; import 'package:flutter/foundation.dart'; class APIConstant { @@ -14,11 +15,16 @@ class APIConstant { getMode() { if (kDebugMode || kProfileMode) { // const String hostNormal = "192.168.1.200"; - const String hostNormal = "192.168.0.9"; + const String hostNormal = Secrets.apiUrlProduction; // const String hostNormal = "192.168.1.200"; apiUrl = "http://$hostNormal/api"; socketUrl = 'ws://$hostNormal:80/socket/websocket'; - } else {} + } else { + const String hostNormal = Secrets.apiUrlProduction; + // const String hostNormal = "192.168.1.200"; + apiUrl = "http://$hostNormal/api"; + socketUrl = 'ws://$hostNormal:80/socket/websocket'; + } } void addValueHeader(Map item) { diff --git a/mobile_app/lib/features/authentication/bloc/authentication_bloc.dart b/mobile_app/lib/features/authentication/bloc/authentication_bloc.dart index d78edf84..a670ddf0 100644 --- a/mobile_app/lib/features/authentication/bloc/authentication_bloc.dart +++ b/mobile_app/lib/features/authentication/bloc/authentication_bloc.dart @@ -26,7 +26,25 @@ class AuthenticationBloc extends Bloc } } - void _onLogoutRequest(LogoutRequest event, Emitter emitter) {} + void _onLogoutRequest(LogoutRequest event, Emitter emit) async { + try { + SharedPreprerencesRepo.removeDataUser(); + emit(AuthenticationFail()); + // final Map? response = await authRepository.logoutUser(); + // if (response == null) { + // emit(AuthenticationFail()); + // return; + // } + // final bool isSuccess = response["success"] ?? false; + // if (isSuccess) { + // print('gndfjgdf:$response'); + // SharedPreprerencesRepo.removeDataUser(); + // emit(AuthenticationSuccess()); + // } + } catch (e) { + emit(AuthenticationFail()); + } + } void _onCheckAutoLogin(CheckAutoLogin event, Emitter emit) async { try { final bool hasLoggedIn = await SharedPreprerencesRepo.checkAutoLogin(); diff --git a/mobile_app/lib/features/authentication/repository/authentication_repo.dart b/mobile_app/lib/features/authentication/repository/authentication_repo.dart index 1c7ecb2e..7a5da0fe 100644 --- a/mobile_app/lib/features/authentication/repository/authentication_repo.dart +++ b/mobile_app/lib/features/authentication/repository/authentication_repo.dart @@ -1,5 +1,4 @@ import 'dart:convert'; -import 'package:bluetooth_ecg/features/authentication/repository/shared_pref_repo.dart'; import 'package:bluetooth_ecg/providers/user_provider.dart'; import 'package:bluetooth_ecg/utils/utils.dart'; import 'package:flutter/material.dart'; @@ -23,7 +22,7 @@ class AuthRepository { } Future registerUser(String email, String password) async { - String url = apiConstant.apiUrl + '/register'; + String url = apiConstant.apiUrl + '/auth/register'; final bodyEncoded = jsonEncode({"email": email, "password": password}); try { final response = await http.post(Uri.parse(url), @@ -36,8 +35,8 @@ class AuthRepository { } } - Future logoutUser() async { - String url = apiConstant.apiUrl + '/logout'; + Future logoutUser() async { + String url = apiConstant.apiUrl + '/auth/logout'; try { final String token = Provider.of(Utils.globalContext!, listen: false).token; final response = await http.get( @@ -46,11 +45,10 @@ class AuthRepository { ); final responseData = jsonDecode(response.body); - if (responseData["status"] == "success") { - SharedPreprerencesRepo.removeDataUser(); - } + return responseData; } catch (err) { - debugPrint('error from register: $err'); + debugPrint('error from logout: $err'); + return null; } } } diff --git a/mobile_app/lib/features/authentication/repository/shared_pref_repo.dart b/mobile_app/lib/features/authentication/repository/shared_pref_repo.dart index cf64df47..5f3097b1 100644 --- a/mobile_app/lib/features/authentication/repository/shared_pref_repo.dart +++ b/mobile_app/lib/features/authentication/repository/shared_pref_repo.dart @@ -12,7 +12,7 @@ class SharedPreprerencesRepo { static Future checkAutoLogin() async { SharedPreferences prefs = await SharedPreferences.getInstance(); - + final String? data = prefs.getString(keyData); final bool hasLoggedIn = data != "" && data != null; return hasLoggedIn; diff --git a/mobile_app/lib/providers/auth_provider.dart b/mobile_app/lib/providers/auth_provider.dart index 42758bf3..8a9bb37f 100644 --- a/mobile_app/lib/providers/auth_provider.dart +++ b/mobile_app/lib/providers/auth_provider.dart @@ -154,56 +154,11 @@ class AuthProvider extends ChangeNotifier { if (responseData["status"] == "success") { // do something with data _token = ""; - removeDataLogin(); notifyListeners(); } } catch (err) { - debugPrint('error from register: $err'); + debugPrint('error from logout: $err'); } } - void setDataLogin() async { - SharedPreferences preferences = await SharedPreferences.getInstance(); - // final userData = json.encode( - // { - // 'token': _token, - // 'userId': _userId, - // 'roleId': _roleId, - // 'firebaseToken': _firebaseToken - // }, - // ); - // preferences.setString('userData', _email); - preferences.setString('userName', _email); - } - - void removeDataLogin() async { - SharedPreferences preferences = await SharedPreferences.getInstance(); - //preferences.remove("userData"); - preferences.remove("userName"); - } - - Future checkAutoLogin() async { - final preferences = await SharedPreferences.getInstance(); - // if (!preferences.containsKey('userData')) { - // return false; - // } - if (!preferences.containsKey('userName')) { - return false; - } - - // final userDataDecoded = - // json.decode((preferences.getString('userData') ?? "")); - // _token = userDataDecoded['token'].toString(); - // _userId = userDataDecoded['userId']; - // _roleId = userDataDecoded['roleId']; - // _firebaseToken = userDataDecoded['firebaseToken']; - //final _emailUser = preferences.getString('userData'); - final _emailUser = preferences.getString('userName'); - notifyListeners(); - if (_emailUser != "") { - return true; - } else { - return false; - } - } } diff --git a/mobile_app/lib/screens/user_screens/user_profile_screen.dart b/mobile_app/lib/screens/user_screens/user_profile_screen.dart index 3993c8c4..41b8ae8e 100644 --- a/mobile_app/lib/screens/user_screens/user_profile_screen.dart +++ b/mobile_app/lib/screens/user_screens/user_profile_screen.dart @@ -1,4 +1,7 @@ +import 'package:bluetooth_ecg/features/authentication/bloc/authentication_bloc.dart'; +import 'package:bluetooth_ecg/features/authentication/bloc/authentication_event.dart'; import 'package:flutter/material.dart'; +import 'package:provider/provider.dart'; import '../../constants/color_constant.dart'; @@ -109,7 +112,9 @@ class _UserProfileScreenState extends State { _buildListTile(title: 'My Doctors', icon: Icons.local_hospital, iconColor: Colors.red, onTap: () {}), _buildListTile(title: 'EHR Files', icon: Icons.folder, iconColor: Colors.green, onTap: () {}), _buildListTile(title: 'Wallet', icon: Icons.account_balance_wallet, iconColor: Colors.purple, onTap: () {}), - _buildListTile(title: 'Log out', icon: Icons.logout, iconColor: Colors.redAccent, onTap: () {}), + _buildListTile(title: 'Log out', icon: Icons.logout, iconColor: Colors.redAccent, onTap: () { + context.read().add(LogoutRequest()); + }), ], ), ),