Skip to content

Commit

Permalink
fix(mobile): update api, remove log
Browse files Browse the repository at this point in the history
  • Loading branch information
thaidmfinnick committed Jul 15, 2024
1 parent fe8968c commit 3ff90bd
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 59 deletions.
10 changes: 8 additions & 2 deletions mobile_app/lib/constants/api_constant.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:bluetooth_ecg/certs/secrets.dart';
import 'package:flutter/foundation.dart';

class APIConstant {
Expand All @@ -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<String, String> item) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,25 @@ class AuthenticationBloc extends Bloc<AuthenticationEvent, AuthenticationState>
}
}

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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -23,7 +22,7 @@ class AuthRepository {
}

Future<Map?> 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),
Expand All @@ -36,8 +35,8 @@ class AuthRepository {
}
}

Future<void> logoutUser() async {
String url = apiConstant.apiUrl + '/logout';
Future<Map?> logoutUser() async {
String url = apiConstant.apiUrl + '/auth/logout';
try {
final String token = Provider.of<UserProvider>(Utils.globalContext!, listen: false).token;
final response = await http.get(
Expand All @@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class SharedPreprerencesRepo {

static Future<bool> checkAutoLogin() async {
SharedPreferences prefs = await SharedPreferences.getInstance();

final String? data = prefs.getString(keyData);
final bool hasLoggedIn = data != "" && data != null;
return hasLoggedIn;
Expand Down
47 changes: 1 addition & 46 deletions mobile_app/lib/providers/auth_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool> 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;
}
}
}
7 changes: 6 additions & 1 deletion mobile_app/lib/screens/user_screens/user_profile_screen.dart
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -109,7 +112,9 @@ class _UserProfileScreenState extends State<UserProfileScreen> {
_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<AuthenticationBloc>().add(LogoutRequest());
}),
],
),
),
Expand Down

0 comments on commit 3ff90bd

Please sign in to comment.