LoggedInOutlet angular2 authentication - Router v3.0.0-alpha8 - Where is ComponentInstruction?

你。 提交于 2019-12-31 03:17:09

问题


I am using code like this to extend RouterOutlet and create app wide authentication and route protection

import {Directive, Attribute, ViewContainerRef, DynamicComponentLoader} from '@angular/core';
import {Router, ComponentInstruction} from '@angular/router';
import {Router} from '@angular/router';
import {RouterOutletMap} from '@angular/router/src/router_outlet_map';
import {RouterOutlet} from '@angular/router/src/directives/router_outlet';

import {Authentication} from '../common/authentication.service';

@Directive({
  selector: 'router-outlet'
})
export class LoggedInRouterOutlet extends RouterOutlet {
  publicRoutes:any;
  isAuthenticated:boolean;
  //private router: any;

  constructor(public _elementRef: ElementRef, public _loader: DynamicComponentLoader,
              public _parentRouter: Router, @Attribute('name') nameAttr: string, public authService:Authentication) {
    super(_elementRef, _loader, _parentRouter, nameAttr);
    this.isAuthenticated = authService.isLoggedIn();


    //this.router = _parentRouter;
    /**
     * DEFINE PUBLIC ROUTES
     *
     * The Boolean following each route below denotes whether the route requires authentication to view.
     *
     * Format: key/value pair
     * - key is the /route url "/login", "/signup", etc
     * - value is a boolean true/false
     *    `true` means it's a publicly available route. No authentication required
     *    `false` means it's a protected route which is hidden until user is authenticated
     *
     */
    this.publicRoutes = {
      'login': true,
      'signup': true,
      '404': true
    };
  } // end constructor

  routeIsActive(routePath:string) {
    return this.router.url == routePath;
  }

  activate(instruction: ComponentInstruction) {
    // let url = instruction.urlPath;
    let url = this.router.url;
    // If the url doesn't match publicRoutes and they are not authenticated...
    if (!this.publicRoutes[url] && !this.isAuthenticated) {
      // todo: redirect to Login, may be there a better way?
      this.router.navigateByUrl('/login');
    }
    return super.activate(instruction);
  }
}

Problem is that ComponentInstruction does not exist in the new v3.0.0-alpha8 router, and the super method signature has changed. How do I update this to work in the new router? I cannot find any documentation explaining the changes.


回答1:


ComponentInstruction has been deprecated. In the current RC4 version of Angular2, this class has been listed under reouter-deprecated. With RC5 coming in, this package would be dropped.

RouterOutlet has changed a lot over time and to make your class LoggedInRouterOultet work, you have to use CanActivate interface.

You can do something like this:

Have an injectable service like LoggedInActivator shown here:

import { Injectable } from '@angular/core';
import { Router, CanActivate } from '@angular/router';
import { LogInService } from './login.service';

@Injectable()
export class LoggedInActivator implements CanActivate {
  constructor(private loginService: LogInService) {}

  canActivate() {
    return this.loginService.isLoggedIn();
  }
}

Add canActivate and map it to LoggedInActivator on component while defining route:

{ path: 'home', component: HomeComponent, canActivate: [LoggedInActivator] }

I hope this helps!




回答2:


because in new router, it uses CanActivate



来源:https://stackoverflow.com/questions/38127746/loggedinoutlet-angular2-authentication-router-v3-0-0-alpha8-where-is-compone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!