Search code examples
dartangular-dartangular-router

CanActivate does not work AngularDart 4


I recently had a problem with the CanActivate annotation since I migrated from version 4 of AngularDart. Indeed, the CanActivate annotation does not run. My Code :

import 'dart:html' as html;

import 'package:angular/angular.dart';
import 'package:angular_router/angular_router.dart';
import 'package:maho_enterprise/app_component.dart';
// Others import

@Component(selector: "view-main"
  ,templateUrl: "main_component.html"
  ,styleUrls: const ["main_component.css"]
  ,directives: const [TopBarComponent,SideBarComponent])
@CanActivate(verifyIfUserIfAuthenticate)
class MainComponent {

}

bool verifyIfUserIfAuthenticate(ComponentInstruction next, ComponentInstruction prev) {
  html.window.console.info("CanActivate works correctly");
  AuthenticationService service = new AuthenticationService.empty();
  bool connected = service.isConnected();
  html.window.console.info("Value connected : "+connected.toString());
  if(!connected) {
    Router router = myInjector.get(Router);
    router.navigateByUrl("/login");
    return false;
  }
  return true;
}

Has anyone encountered this problem with version 4 of AngularDart? thanks for the help.


Solution

  • Unfortunately CanActivate is no longer supported in AngularDart 4.0.0. It required very special compiler code that can't be supported long-term, and the router is changing in 5.x.x. As a workaround I suggest using the OnActivate lifecycle method.

    I'll add a bug to remove the API and document this change.