-
Notifications
You must be signed in to change notification settings - Fork 486
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Access token doesn't handled in router configured with "useHash: true" #211
Comments
BTW there is my router's config: import { NgModule } from '@angular/core'
import { RouterModule, Route, ExtraOptions } from '@angular/router'
import { HomeComponent } from './home/home.component'
import { PingComponent } from './ping/ping.component'
import { PageNotFoundComponent } from './page-not-found/page-not-found.component'
@NgModule({
imports: [
RouterModule.forRoot([
<Route>{ path: '', pathMatch: 'full', redirectTo: 'home' },
<Route>{ path: 'home', component: HomeComponent },
<Route>{ path: 'ping', component: PingComponent },
<Route>{ path: '**', component: PageNotFoundComponent },
], <ExtraOptions>{
enableTracing: true,
// useHash: true,
})
],
exports: [
RouterModule
]
})
export class AppRoutingModule {
} |
With
|
+1 (I confirm this issue) |
1 similar comment
+1 (I confirm this issue) |
Can one of you make a plunk that reproduces the issue? |
To reproduce, use the auth0 angular2 demo repo and add { useHash: true } as a second parameter to RouterModule.forRoot() |
This issue happens because Angular's router doesn't play nicely with the redirect flow and authentication result that comes in the hash. For Lock versions between 10.0.0 and 10.9.2, this can be solved by listening for router events, parsing the hash if one is found, and using the authentication result. See this workaround for more. For Lock > 10.9.2, a ...
import 'rxjs/add/operator/take';
import 'rxjs/add/operator/filter';
public handleAuthenticationWithHash(): void {
this
.router
.events
.filter(event => event.constructor.name === 'NavigationStart')
.filter(event => (/access_token|id_token|error/).test(event.url))
.subscribe(event => {
this.lock.resumeAuth(window.location.hash, (error, authResult) => {
if (error) return console.log(error);
this.setUser(authResult);
this.router.navigateByUrl('/');
});
});
}
private setUser(authResult): void {
localStorage.setItem('access_token', authResult.accessToken);
localStorage.setItem('id_token', authResult.idToken);
} This can be called in the import { AuthService } from './auth/auth.service';
...
export class AppComponent {
constructor(public auth: AuthService) {
auth.handleAuthenticationWithHash();
}
} This issue isn't specific to angular2-jwt but rather Auth0 Lock. I'll close this here, but stay tuned to the Lock issue tracker for more. |
Whereas for router configured with "useHash: false" all is working fine.
Solution?
The text was updated successfully, but these errors were encountered: