Skip to content

Instantly share code, notes, and snippets.

@KingDarBoja
Created March 4, 2019 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save KingDarBoja/460ffa94f2bc356a2da348540c39c7fc to your computer and use it in GitHub Desktop.
Save KingDarBoja/460ffa94f2bc356a2da348540c39c7fc to your computer and use it in GitHub Desktop.
AuthGuard example in AngularDart
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