Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to navigate user to sign in page when not logged in

I have a structural question regarding angulardart and sign in flows. I have a service that talks a server and has methods like login(), logout(), loggedIn(). That is all working really nicely in angular but i am looking for a best practice way to incorporate the login flow into my application.

The app requires the user to be logged in. So would i reroute to a custom "login" view at startup when the user is not logged in? Would i switch parts of my main index.html when not logged in? Would i create a custom component handling the login?

Is there a common angular pattern for such an scenario?

like image 845
Moritz Avatar asked Feb 10 '14 12:02

Moritz


People also ask

How do I redirect to login page if not logged in in react?

Path: /src/_components/PrivateRoute.jsx If the user is logged in the Component prop is rendered, otherwise if the user is not logged in the React Router Redirect component is rendered which redirects the user to the /login page. The requested path ( props. location ) is passed with the redirect in the state.

How to redirect to login page if not logged in html?

You can use session variable to do this, you ust be set session on login. So on edit page starting you can write following code to check wheter user is logged in or not.. your condition is bad, a simple issset($_SESSION) is not enough. another session might be set somewhere else.

How to redirect to login page if not logged in PHP?

When you determine that they are not logged in you can issue a redirection header: header("Location: http://www.example.com/log-in/"); This is described in detail in the PHP Manual: Header. Show activity on this post.

How to redirect user to login page in PHP?

Answer: Use the PHP header() Function You can simply use the PHP header() function to redirect a user to a different page. The PHP code in the following example will redirect the user from the page in which it is placed to the URL http://www.example.com/another-page.php .


1 Answers

My current solution is based on routing. I created views for sign in and a landing page. When the user is on the sign in page and logs in he is routed to the landing page. When a user is not authenticated and enters any path on the website he is redirected to the sign in 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 _ensureAuthenticated() method has to be set as the preEnter callback and evaluates the login state:

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

While this works nicely, the _ensureAuthenticated() method has to be set as a preEnter callback in every route. Is there a way to more easily catch the preEnter globally? Setting it on the root route does not work.

like image 165
Moritz Avatar answered Nov 15 '22 10:11

Moritz