在angular2中使用auth0并启用routeConfig

时间:2016-02-09 09:09:54

标签: angular auth0

我有一个问题,希望你们有些人能够回答,因为我无法在谷歌上找到解决方案。现在,我正在尝试对我的应用程序实施Auth0身份验证。

为了清楚说明,我创建了一个App组件,如下所示:

应用组件

@Component({
selector: 'app',
template: `
<div>
    <div class="header wrapper">
        <header class="row">
            <div class="column small-12">
                <a href="/">
                    <img src="/assets/img/logo.svg" title="" />
                </a>
                <a class="navigation-toggle icon-menu hide-for-large-up" (click)="toggleMenuState()" ></a>
            </div>
        </header>
    </div>

    <router-outlet></router-outlet>

    <div class="footer wrapper">
        <footer class="row">
            <div class="column small-12">
                <div class="container">
                    <div class="icon icon-ship hide-for-medium-down"></div>
                </div>
            </div>
        </footer>
    </div>
</div>
`,
encapsulation: ViewEncapsulation.None,
directives: [ ROUTER_DIRECTIVES, RouterOutlet, AmendmentComponent, AmendmentAccountComponent ]
})

@RouteConfig([
    { path: '/amendment/...', name: 'Amendment', component:    AmendmentComponent, useAsDefault: true }    
])

添加了routeConfig,所以现在URL看起来像这样: localhost:3000 /修订

然后我创建了一个看起来像这样的modificationComponent:

@Component({
selector: 'amendment',
template: `  
        <div class="help wrapper">
            <div class="row">
                <ul class="column small-12">
                    <amendment-account></amendment-account>
                </ul>
            </div>
        </div>

        <div class="main-area wrapper">
            <div class="row">
                <router-outlet></router-outlet>
            </div>
        </div>
`,
encapsulation: ViewEncapsulation.None,
directives: [ CORE_DIRECTIVES, RouterOutlet, AmendmentAccountComponent, BookingsComponent, MainComponent, RegisterOrLoginComponent ],
styles: [`
    agent {
        display: block;
    }
`]
})
@RouteConfig([
    { path: '/amendment/', name: 'Bookings', component: BookingsComponent,  useAsDefault: true },
    { path: '/bookings', name: 'Bookings', component: BookingsComponent },
    { path: '/main/:id', name: 'Main', component: MainComponent },
    { path: '/main', redirectTo: ["Bookings"] },
])

在HTML中,您可以看到我正在加载组件(AccountComponent)。我正在处理这个组件中的Auth0。

AccountComponent html如下所示:

<li><a href="#" *ngIf="!loggedIn()" (click)="login()">Login</a></li>
<li><a href="#" *ngIf="loggedIn()" (click)="logout()">Logout</a></li>

我要做的是在localhost:3000 / modification / login Url上加载Auth0框。这对我不起作用,因为当我点击登录Href链接时,由于RouterConfig,快速转到localhost:3000 /修订/预订:(

所以我的问题是它不会停止并加载Auth0登录框。我可以看到它一瞬间,然后它重定向我。

对此有何解决方案?

请注意,如果我删除所有routerConfig路径,它就可以工作,所以不会发生重定向,但这对我来说不是解决方案..

1 个答案:

答案 0 :(得分:4)

我想

(click)="login(); false"

正是您要找的。