Angular 4 - 通过服务器端呈现支持重定向到外部URL

时间:2017-11-22 14:53:39

标签: angular

我还没有找到解决这个小问题的方法。但情况如下:

我有一名警卫必须检查是否存在auth-cookie。如果它不存在,那么后卫应该重定向到其他地方(不在同一个子域)的单点登录页面。使用Location.go()似乎不起作用,路由器也没有任何导航到外部URL的能力。

这是守卫代码:

import { Injectable } from '@angular/core';
import { Location } from '@angular/common';
import { CanActivate, RouterStateSnapshot, ActivatedRouteSnapshot } from '@angular/router';
import { CookieService } from '../services/cookie.service';
import URL = require('url-parse');
import { Document } from '../components/document/document';

@Injectable()
export class CookieGuard implements CanActivate {

constructor(private cookieService: CookieService, private location: Location) { }

private getLoginRedirectUrl(currentUrl) {
        const protocol = new URL(currentUrl).protocol;
        switch (process.env.NODE_ENV) {
            case 'PROD':
            case 'production':
            case 'build':
                return `http://sso-portal.xxxxxxx.com`;
            case 'TEST':
            case 'testing':
                return `http://sso-portal-test.xxxxxxx.com`;
            default:
                return `http://sso-portal-dev.xxxxxxx.com`;
        }
    }

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
        if (!this.cookieService.checkCookie(('auth-cookie'))) {
            const redirectUrl = this.getLoginRedirectUrl(state.url);
            this.location.go(redirectUrl);
            return false;
        }
        return true;
    }

canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
        return this.canActivate(route, state);
    }
}

1 个答案:

答案 0 :(得分:0)

好吧,也许你可以试试

window.location(redirectUrl);