-
-
Save insinfo/99808768e8228f8a9e1183d4103753c5 to your computer and use it in GitHub Desktop.
AuthGuard example in AngularDart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:angular_router/angular_router.dart'; | |
import 'package:core/core.dart'; | |
import 'package:web/src/routes.dart'; | |
import 'package:web/src/route_paths.dart'; | |
class AuthGuard implements RouterHook { | |
Router _router; | |
AuthenticationState _authBlocState; | |
set router(Router value) => _router = value; | |
set authBlocState(AuthenticationState value) => _authBlocState = value; | |
@override | |
Future<bool> canActivate( | |
Object componentInstance, RouterState current, RouterState next) async { | |
if (current.toUrl() != '/login') { | |
await _router.navigate('/login'); | |
print('I Cannot Activate'); | |
return false; | |
} | |
print('I Can Activate'); | |
return true; | |
} | |
@override | |
Future<bool> canNavigate() async { | |
print('State $_authBlocState'); | |
if (_authBlocState is AuthenticationAuthenticated) { | |
print('Can Navigate? Yes, it can!'); | |
print(AppRoutePaths.dashboard.toUrl()); | |
await _router.navigate(AppRoutePaths.dashboard.toUrl()); | |
return true; | |
} | |
print('Can Navigate? No, it cannot!'); | |
print(AppRoutePaths.login.toUrl()); | |
await _router.navigate(AppRoutePaths.login.toUrl()); | |
return false; | |
} | |
// Implement noSuchMethod(): When a class has a noSuchMethod() | |
// it implements any method. | |
// Hence, warning "Missing concrete implementation" gets supressed. | |
dynamic noSuchMethod(Invocation i) => super.noSuchMethod(i); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment