angular2 router.navigate inside auth0 callback

∥☆過路亽.° 提交于 2019-12-04 09:19:42
Ray Luxembourg

This Should fix your problem.

import { Component, NgZone } from '@angular/core';

constructor(public router: Router, public _zone: NgZone) {}

then inside your callback , call this

this._zone.run(()=>{
  this.router.navigate(['uploader']);
});
Mr.wiseguy

Thanks to this auth0 article I found a solution to the problem, since NgZone wasn't working for me.

Wrapping the request in bindNodeCallback did the trick. This function wraps the Auth0 request with Zone.js and thus doing the request in the Angular zone.

My code was:

this._auth0.parseHash((err, authResult: auth0.Auth0DecodedHash) => {
    if (!err && authResult && authResult.accessToken && authResult.idToken) {
       window.location.hash = ''; // Remove the Auth0 trailing hash
    }

    this.ngZone.run(() => {
       this.router.navigate(['<some-url>']);
    });
});

But that resulted in not navigating to any page due to being out of Angular scope. I replaced that code with a Zone.js wrapped version to make it work:

this.parseHash$ = bindNodeCallback(this._auth0.parseHash.bind(this._auth0));
this.parseHash$().subscribe(() => this.router.navigate(['<some-url>']));

And editing the request result can be done in the subscribe or via the map functions.


Versions

For the code above I am using the following versions:

Angular 6.1.7

auth0-js 9.8.0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!