Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 - router-outlet with login structure

Tags:

I'm building angular2 app and currently I have an home component with navbar, toolbar and router-outlet for the main content. I want to add one extra page for the login mechanism, so if the user is not authenticated the login page will be displayed in the entire screen and after login to user will be navigated to the component with the structure above.

How can I manage this structure? Do I need two router outlets? the first one for the navigation between login and home pages and one for the main content in the home page? Any other common structure which is more simple than two router outlets?

like image 362
galvan Avatar asked Jul 11 '16 18:07

galvan


People also ask

What is router outlet ></ router outlet?

The Router-Outlet is a directive that's available from the router library where the Router inserts the component that gets matched based on the current browser's URL. You can add multiple outlets in your Angular application which enables you to implement advanced routing scenarios. <router-outlet></router-outlet>

Is it possible to have a multiple router outlet in the same template?

You can have multiple router-outlet in same template by configuring your router and providing name to your router-outlet, you can achieve this as follows. Advantage of below approach is thats you can avoid dirty looking URL with it. eg: /home(aux:login) etc. Assuming on load you are bootstraping appComponent.

Can Angular 12 use multiple router outlets?

Using named outlets and secondary routes, you can target multiple outlets in the same RouterLink directive.

How many router outlets can be used in an Angular application?

Remember all this route config only has one router-outlet , so the end result of the matching must be a single component and not multiple.


2 Answers

I succeed achieving this workflow by implementing this structure. I have two main components:

LoginComponent which it's route is '/login'. HomeComponent which it's route is ''. (empty route).

In addition I set a guard for my HomeComponent which checks if the user is authenticated in his canActivate. If no I navigates the user to the '/login' route.

Then in my home component I have template with the following structure: tool-bar, side-menu, and router-outlet.

The last thing I have to do is to configure other app routes as children routes of my HomeComponent. (for example: '/news' is a child route of '').

like image 88
galvan Avatar answered Oct 06 '22 17:10

galvan


First of all, I highly recommend utilizing the updated Angular2 router. The newest router includes support for guards which are added to your route configuration to prevent access to certain routes.

After ensuring you have the latest router, you'll need to do several things:

1) Create a top-level component and call this App. This is where your <router-outlet> will go.

2) Create a Login component. This component should include a form for logging in to your application, along with the logic for handling login. Create a route for this component.

3) Create a Home component (you have already done this).

4) Use a guard (new functionality in the latest router) to prevent users from accessing the home component before logging in.

Your code will look something like this (for more info, see: https://medium.com/@blacksonic86/upgrading-to-the-new-angular-2-router-255605d9da26#.n7n1lc5cl)

// auth-guard.ts import { Injectable } from '@angular/core'; import {   CanActivate,   Router,   ActivatedRouteSnapshot,   RouterStateSnapshot } from '@angular/router';  import { AuthService } from './services/auth/auth.service';  @Injectable() export class AuthGuard implements CanActivate {   constructor(private authService: AuthService, private router: Router) {}    canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot) {     if (this.authService.isLoggedIn()) {       return true;     }      this.router.navigate(['login']);     return false;   } } 

For more information on guards:

I would also highly suggest reading up on the latest Angular router (the docs have recently been updated): https://angular.io/docs/ts/latest/guide/router.html

like image 43
Joel Brewer Avatar answered Oct 06 '22 17:10

Joel Brewer