Skip to content

Commit

Permalink
Fix logout issue (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ida631 authored Nov 18, 2024
1 parent 9b6e2c9 commit ba33ac1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 18 deletions.
20 changes: 13 additions & 7 deletions lib/presentation/settings/viewModels/settings_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,34 @@ class SettingsViewModel extends ChangeNotifier {

Future<void> fetchAuthSessionNew() async {
try {
final isLoggedIn = await authUseCase.isLoggedIn();
isLoggedIn = await authUseCase.isLoggedIn();
if (isLoggedIn) {
fetchUser();
await fetchUser();
} else {
user = null; // Ensure user is null if not logged in
}
} catch (e) {
if (kDebugMode) {
print(e);
}
isLoggedIn = false;
user = null; // Ensure proper reset
}
notifyListeners();
}

Future<void> fetchUser() async {
try {
user = await userUserCase.fetchMe();
print(user);
isLoggedIn = true;
notifyListeners();
isLoggedIn = true; // Ensure isLoggedIn is updated correctly
} catch (e) {
isLoggedIn = false;
notifyListeners();
if (kDebugMode) {
print(e);
}
isLoggedIn = false; // Handle fetch user failure
user = null;
}
notifyListeners();
}

Future<void> initPackageInfo() async {
Expand Down
24 changes: 20 additions & 4 deletions lib/presentation/settings/viewModels/user_profile_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import 'package:domain/entities/user.dart';
import 'package:domain/usecases/user_usercase.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:domain/usecases/auth_usecase.dart'; // Assuming your use cases are here
import 'package:domain/usecases/auth_usecase.dart';
import 'package:fluttertoast/fluttertoast.dart'; // Assuming your use cases are here

class UserProfileViewModel extends ChangeNotifier {
final AuthUseCase authUseCase;
Expand All @@ -21,32 +22,47 @@ class UserProfileViewModel extends ChangeNotifier {
Future<void> fetchAuthSessionNew() async {
try {
isLoggedIn = await authUseCase.isLoggedIn();
notifyListeners();
if (isLoggedIn) {
fetchUser();
await fetchUser(); // Fetch user only if logged in
} else {
user = null; // Clear user data when not logged in
}
} catch (e) {
if (kDebugMode) {
print(e);
}
isLoggedIn = false;
user = null; // Ensure user data is cleared on error
}
notifyListeners(); // Notify listeners about state changes
}

Future<void> fetchUser() async {
try {
user = await userUseCase.fetchMe();
print(user);
notifyListeners();
} catch (e) {
isLoggedIn = false;
user = null; // Clear user data in case of an error
notifyListeners(); // Ensure the UI is updated
}
}

Future<void> logoutUser() async {
setLoading(true);
try {
await authUseCase.logout();
Fluttertoast.showToast(
msg: "Logged out",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER,
backgroundColor: Colors.black,
textColor: Colors.white,
fontSize: 16.0,
);
isLoggedIn = false; // Explicitly set isLoggedIn to false
user = null; // Clear user data
notifyListeners();
} catch (e) {
print("Logout failed: $e");
}
Expand Down
28 changes: 21 additions & 7 deletions lib/presentation/settings/views/settings_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,27 @@ class SettingsPage extends StatefulWidget {
}

class _SettingsPageState extends State<SettingsPage> {
@override
/* @override
Future<void> didChangeDependencies() async {
super.didChangeDependencies();
// Fetch the latest auth session when dependencies change
final viewModel = Provider.of<SettingsViewModel>(context, listen: false);
await viewModel.fetchAuthSessionNew();
}
*/
late SettingsViewModel viewModel;
@override
void initState() {
super.initState();
viewModel = Provider.of<SettingsViewModel>(context, listen: false);
_fetchAuthSession();
}

Future<void> _fetchAuthSession() async {
await viewModel.fetchAuthSessionNew();
setState(() {});
}



@override
Expand Down Expand Up @@ -67,14 +81,14 @@ class _SettingsPageState extends State<SettingsPage> {
fontSize: 16, fontWeight: FontWeight.bold),
),
subtitle: Text(viewModel.user?.email ?? ""),
onTap: () {
Navigator.push(
onTap: () async {
await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => UserProfilePage()),
).then((value) {
viewModel.fetchAuthSessionNew();
});
builder: (context) => UserProfilePage(),
),
);
await _fetchAuthSession();
},
),
MoreRow(
Expand Down

0 comments on commit ba33ac1

Please sign in to comment.