I'm using angular-oauth2-oidc
package, I using implicit flow, and I can't migrate (at the moment) to code flow.
I would like to navigate to original url when the flow is ended, I have using preserving state as document suggest.
My problem is I can't navigate from onTokenReceived
function, the promise resolved by navitageByUrl
resolves with false
but no errors on console:
export class AppComponent {
url: string;
constructor(
private oauthService: OAuthService,
private router: Router) {
this.url = window.location.pathname; // Get requested url.
}
ngOnInit() {
this.oauthService.loadDiscoveryDocument().then(res => {
console.log(res);
this.oauthService.tryLogin({
onTokenReceived: (info) => {
// Receive state and navitage to.
this.router.navigateByUrl(info.state).then(res => {
if (!res) {
console.error("Navigate to " + info.state + " after get token:" + res);
}
});
}
}).then(res => {
if (!res) { // If not login init implicit flow passing url as state.
this.oauthService.initImplicitFlow(this.url);
}
})
})
}
}
However, It works if run after 100 miliseconds using setTimeout
:
onTokenReceived: (info) => {
// Receive state and navitage to.
setTimeout(() => {
this.router.navigateByUrl(info.state).then(res => {
if (!res) {
console.error("Navigate to " + info.state + " after get token:" + res);
}
});
}, 100); // With less than 100 not works.
}
Someone can help me? Why navigateByUrl dosen't work?