Angular - 受限制的页面显示为window.open重定向到guard

时间:2018-03-16 23:33:23

标签: angular typescript redirect angular-router angular-guards

我正在实现一个canActivate Guard,如果用户没有登录,它有一个重定向到登录站点的逻辑。到目前为止,我遇到的所有示例和修复都是在处理Angular本身定义的登录页面。就我而言,登录位于一个单独的子域中。

重定向正在按预期工作,但由于外部重定向,浏览器中显示受限制的页面,然后重定向开始。

我通过@inject尝试了window.open和文档。

使用Angular 5.2.5

这是我的 auth.guard.ts

import { Inject, Injectable } from '@angular/core';
import { DOCUMENT } from '@angular/common';

import { Router, CanActivate, CanActivateChild, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { CookieService } from 'ngx-cookie-service';
import { AlmVars } from '@alm/ngwebpack-vars/alm-vars.const';


@Injectable()

export class AuthGuard implements CanActivate, CanActivateChild {
    readonly almVars = AlmVars;

    constructor(private router: Router, private cookieService: CookieService, @Inject(DOCUMENT) private document: any) {

    }

    canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
        if (this.cookieService.get('almVToken')) {
            return true;
        }

        // this.document.location.href = '//login.' + this.almVars.hostname + '?referer=' + this.almVars.app;
        window.open('//login.' + this.almVars.hostname + '?referer=' + this.almVars.app, '_self');

        return false;
    }
}

应用-routing.module.ts

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

import { AppResolver } from './app.resolver';
import {AuthGuard} from './shared/alm-auth.guard';
import {ProfileComponent} from './profile/profile.component';


const appRoutes: Routes = [{
    path: '',
    component: ProfileComponent,
    canActivate: [
        AuthGuard
    ],
}];

@NgModule({
    imports: [
        RouterModule.forRoot(appRoutes),
    ],
    exports: [
        RouterModule,
    ],
    providers: [
        AppResolver,
    ]
})

export class AppRoutingModule {

}

0 个答案:

没有答案