【发布时间】:2017-11-13 06:41:13
【问题描述】:
我正在学习这个教程,这是一个很棒的教程!它展示了如何使用 Okta 进行身份验证。
https://developer.okta.com/blog/2017/04/17/angular-authentication-with-oidc
本教程有一个分配给根路由的HomeComponent,并根据用户是否登录显示或不显示元素。因此,在app.component.ts 中,您可以捕获要存储的令牌在构造函数中使用以下部分从 url 存储:
this.oauthService.loadDiscoveryDocument().then(() => {
this.oauthService.tryLogin({});
}
在auth.guard.ts 中,您可以:
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
if (this.oauthService.hasValidIdToken()) {
return true;
}
this.router.navigate(['/home']);
return false;
}
使用此路由配置:
const appRoutes: Routes = [
{ path: 'search', component: SearchComponent, canActivate: [AuthGuard] },
{ path: 'edit/:id', component: EditComponent, canActivate: [AuthGuard]},
{ path: 'home', component: HomeComponent},
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{ path: '**', redirectTo: 'home' }
];
我的问题
我的设置稍有不同,而且一切的时间安排都不正确。
- 我有一个LoginComponent,如果您未通过身份验证,您将被重定向到该地址。
- 我将根路由重定向到AuthGuarded 路由。
- 当我使用 Okta 登录时,this.oauthService.tryLogin({}) 没有及时运行以阻止 AuthGuard 将我重定向到 LoginComponent。这会导致包含令牌的 url 部分在我尝试使用它们之前消失在存储中。
这是我所拥有的:
app.component.ts
constructor(
...
private oauthService: OAuthService) {
...
this.oauthService.loadDiscoveryDocument().then(() => {
this.oauthService.tryLogin({});
});
}
auth.guard.ts
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
if (!this.oauthService.hasValidIdToken()) {
this.router.navigate(['/login']);
}
return true;
}
app-routing.module.ts
const routes: Routes = [
{ path: '', redirectTo: '/projects', pathMatch: 'full' },
{ path: 'login', component: LoginComponent },
{ path: 'profile', component: ProfileComponent, canActivate: [AuthGuard] },
{ path: 'help', component: HelpComponent, canActivate: [AuthGuard] },
{ path: 'settings', component: SettingsComponent, canActivate: [AuthGuard] },
{ path: 'contact', component: ContactComponent },
{ path: '**', component: NotFoundComponent }
];
projects-routing.module.ts
const routes: Routes = [
{
path: 'projects',
component: ProjectsComponent,
canActivate: [AuthGuard],
children: [
...
]
}
如您所见,当我去我的 Okta 站点输入我的用户名和密码时,我被重定向到我的应用程序的根目录,从 loadDiscoveryDocument() 返回的 Promise 已订阅,但 @987654344 @ 将我重定向回登录页面,在我可以让 OAuthService 为我收集并将其存储在存储之前,从 url 中丢失了 id_token 等。
我的问题
有没有办法让它工作而无需更改我的应用程序的路由结构?根据登录状态,我不需要充当“LoginComponent”和“HomeComponent”的“HomeComponent”。
【问题讨论】:
-
你好杰克。如果您将
this.oauthService.tryLogin({});从构造函数中移到ngOnInit()方法中(并在您的类上实现OnInit),是否有帮助? -
Matt,它没有帮助,因为在解决承诺并调用 tryLogin 之前,是身份验证守卫将我重定向到登录组件。我唯一能想到的就是创建一个回调组件,让 okta 重定向到该组件,保留我的令牌,然后使用角度路由器服务重定向到 root。我只是希望有一个更清洁的解决方案。不过,理论上,在设置我的身份时使用的回调组件会起作用,是吗?
-
我刚刚意识到,这可能有助于表明项目模块是定义项目路由的地方,并且 /projects 受到
auth.guard的保护 -
你能用 Okta 的 Auth SDK 代替吗?这允许您创建自己的登录表单而不是重定向。这在您引用的同一篇博客文章中有所介绍。 developer.okta.com/blog/2017/04/17/…
-
我会试一试。无论如何,这就是我接下来要做的事情。感谢您的建议。
标签: angular authentication redirect okta okta-api