isLoggedIn method
This method checks if a token exists and if the current user data is available, indicating that a user is logged in.
Returns:
Future<bool>
: A Future that completes withtrue
if a user is logged in, otherwisefalse
.
Example:
bool loggedIn = await isLoggedIn();
Implementation
Future<bool> isLoggedIn() async {
// Check if a token exists
bool hasToken = CoffeeStorage.getJwtToken().isNotEmpty;
// If no token, the user is not logged in
if (!hasToken) {
return false;
}
// If currentUser is already fetched, the user is logged in
if (currentUser != null) {
return true;
}
// Fetch the current user and check if it's not null
return (await getCurrentUser()) != null;
}