使用 ui-router-ng2 的通配符路由?
Wildcard route with ui-router-ng2?
我正在尝试将 ui-router-ng2 与身份验证即服务提供商 Auth0 一起使用。问题是登录后 Auth0 将用户重定向到 url,如下所示:
http://localhost:3000/#access_token=alCUyTW2WKLkV8Fq&{等等...}
我试过更改重定向 url 以便它命中一个组件,但使用 auth0 似乎不可能。
使用 ui-router 1,可以添加一个 http 拦截来处理这个问题,但此时 angular 2 中没有 http 拦截功能。
将 auth0 与 angular 2 默认路由器一起使用时,重定向处理如下:
this
.router
.events
.filter(event => event.constructor.name === 'NavigationStart')
.filter(event => (/access_token|id_token|error/).test(event.url))
.subscribe(() => {
this.lock.resumeAuth(window.location.hash, (error, authResult) => {
if (error) return console.log(error);
localStorage.setItem('id_token', authResult.idToken);
this.router.navigate(['/']);
});
我已经浏览了 ui-router-ng2 文档,但没有找到做类似事情的方法。
有什么想法吗?
我通过在我的应用程序的根组件中执行以下操作来修复此问题:
export class AppComponent {
constructor() {
console.log("App root component created");
// Auth0 doesn't redirect properly to /#/ URLs so we have to capture token here
var regex = /(id_token=)([^&]*)/;
var vals = regex.exec(window.location.hash);
if (vals != null) {
localStorage.setItem('id_token', vals[2]);
if(localStorage.getItem('preAuthURL') != null) { window.location.href = localStorage.getItem('preAuthURL');}
}
}
}
我正在尝试将 ui-router-ng2 与身份验证即服务提供商 Auth0 一起使用。问题是登录后 Auth0 将用户重定向到 url,如下所示:
http://localhost:3000/#access_token=alCUyTW2WKLkV8Fq&{等等...}
我试过更改重定向 url 以便它命中一个组件,但使用 auth0 似乎不可能。
使用 ui-router 1,可以添加一个 http 拦截来处理这个问题,但此时 angular 2 中没有 http 拦截功能。
将 auth0 与 angular 2 默认路由器一起使用时,重定向处理如下:
this
.router
.events
.filter(event => event.constructor.name === 'NavigationStart')
.filter(event => (/access_token|id_token|error/).test(event.url))
.subscribe(() => {
this.lock.resumeAuth(window.location.hash, (error, authResult) => {
if (error) return console.log(error);
localStorage.setItem('id_token', authResult.idToken);
this.router.navigate(['/']);
});
我已经浏览了 ui-router-ng2 文档,但没有找到做类似事情的方法。
有什么想法吗?
我通过在我的应用程序的根组件中执行以下操作来修复此问题:
export class AppComponent {
constructor() {
console.log("App root component created");
// Auth0 doesn't redirect properly to /#/ URLs so we have to capture token here
var regex = /(id_token=)([^&]*)/;
var vals = regex.exec(window.location.hash);
if (vals != null) {
localStorage.setItem('id_token', vals[2]);
if(localStorage.getItem('preAuthURL') != null) { window.location.href = localStorage.getItem('preAuthURL');}
}
}
}