You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to implement VGuard from the VRouter package with the help of Riverpod as State Notifier. In the case, the states are loading, unauthenticated and authenticated.
Below you will find two GIFs with their respective debug console outputs and a table with the list of actions taken and their respective results. The first GIF shows everything working as it should. The second GIF shows a unique case where it is not working.
In the end you will find the code for each class in order to recreate this issue.
enumAuthStatus { loading, authenticated, unauthenticated }
classAuthStateextendsEquatable {
constAuthState._(
{this.user =UserModel.empty, this.status =AuthStatus.loading});
constAuthState.loading() :this._();
constAuthState.authenticated(UserModel user)
:this._(user: user, status:AuthStatus.authenticated);
constAuthState.unauthenticated()
:this._(status:AuthStatus.unauthenticated);
finalUserModel user;
finalAuthStatus status;
@overrideList<Object?> get props => [user, status];
}
classAuthNotifierextendsStateNotifier<AuthState> {
finalAuthService _authService;
AuthNotifier(AuthService authService)
: _authService = authService,
super(constAuthState.loading()) {
checkUserAuth();
}
Future<void> checkUserAuth() async {
/// this is where you can check if you have the cached token on the phone /// on app startup /// for now we assume no such caching is doneif (await _authService.authChangeStream.any((element) => element ==null)) {
state =constAuthState.unauthenticated();
} else {
state =AuthState.authenticated(_authService.currentUser);
}
}
Future<void> loginUser(String username, String password) async {
state =constAuthState.loading();
UserModel user =await _authService.loginWithEmail(email: username, password: password);
if (user ==UserModel.empty) {
state =constAuthState.unauthenticated();
} else {
/// do your pre-checks about the user before marking the state as /// authenticated
state =AuthState.authenticated(user);
}
}
Future<void> logoutUser() async {
await _authService.logOut();
state =constAuthState.unauthenticated();
}
}
provider.dart
final _authInstance =Provider<FirebaseAuth>(
(ref) =>FirebaseAuth.instance,
);
final authServiceProvider =Provider<AuthService>((ref) =>AuthService(ref.watch(_authInstance)));
final authStateNotifier =StateNotifierProvider<AuthNotifier, AuthState>(
(ref) =>AuthNotifier(ref.watch(authServiceProvider)));
I am trying to implement
VGuard
from theVRouter
package with the help of Riverpod as State Notifier. In the case, the states areloading
,unauthenticated
andauthenticated
.Below you will find two GIFs with their respective debug console outputs and a table with the list of actions taken and their respective results. The first GIF shows everything working as it should. The second GIF shows a unique case where it is not working.
In the end you will find the code for each class in order to recreate this issue.
GIF 01
GIF 02
main.dart
model_user.dart
service_auth.dart
auth_notifier.dart
provider.dart
page_login.dart
page_dashboard.dart
I have been trying to figure out why this is happening and I cannot seem to fix it. At this point, it feels like a bug but I am not sure.
The text was updated successfully, but these errors were encountered: