How to navigate a user to log in when not logged in

I have a structural question regarding angular and signal flows. I have a service that speaks on the server and has methods such as login (), logout (), loggedIn (). It all works really nicely in angular, but I'm looking for the best way to enable the input stream in my application.

The application requires the user to log in. So will I be redirected to the user type of "login" at startup when the user is not logged in? Would I switch parts of my main index.html when you are not logged in? Should I create a custom component that processes login?

Is there a generic angular template for such a scenario?

+4
source share
1 answer

My current solution is based on routing. I have created submission and landing page views. When the user is on the login page and logs in, he is directed to the landing page. When the user is not authenticated and does not enter any path on the website, he is redirected to the login page.

here is the RouteInitializer:

  ..addRoute(
      name: 'signin',
      path: '/signin',
      enter: view('view/signin.html'))
  ..addRoute(
      name: 'dashboard',
      path: '/dashboard',
      defaultRoute: true,
      preEnter: _ensureAuthenticated,
      enter: view('view/dashboard.html'))  

The method _ensureAuthenticated()should be set as a callback preEnterand evaluates the input status:

  _ensureAuthenticated(RoutePreEnterEvent routeEvent) {
    if (!_userService.loggedIn) {
      routeEvent.allowEnter(new Future<bool>.value(false));
      _router.go('signin', {});
    }
  }

While this works well, the method _ensureAuthenticated()should be set as a callback preEnterin each route. Is there a easier way to catch preEnterglobally? Installing it on the track rootdoes not work.

+4
source

Source: https://habr.com/ru/post/1526160/


All Articles