8

I use oauth authentication to login user. After the user has logged in, get taken from URL. URL like this: http://xxx/callback#access_token=xxx. then redirect to other page. I use this.router.navigateByUrl to redirect, the hash tag is removed but when I click other link,the hash fragment will show again. How can I remove the hash token fragment permanently?

Example:

after use login in //xxx/callback#access_token=xxx

redirect to //xxx/home

then click linkA, the url is //xxx/linkA#access_token=xxx

expected url is //xxx/linkA

5 Answers 5

5

Looks like this has been fixed in the Angular 2 final release (testing it with 2.1.0 right now).

The following works for me:

router.navigate([]); 

Redirects to the "index" page without the hash and it doesn't reappear on subsequent navigation.

Sign up to request clarification or add additional context in comments.

Comments

3

You should put useHash equal to false at the imports of your app.module

@NgModule({ bootstrap: [ App ], declarations: [App], imports: [ .... RouterModule.forRoot(ROUTES, { useHash: false }) ], providers: [] }) 

putting it to false will stop it from using HashLocationStrategy which is the default in RouterModule.forRoot function

Comments

3

I have same issue, my solution is after retrieved access_token, you can set: window.location.hash = ''

It works for me

Comments

3

I don't know how you redirect but I assume it's caused by the method you use.

One way to reset the fragment is

<a routerLink="path" fragment=""> 

(not tested myself yet).

From code

let tree = this.router.navigate(['path'], {relativeTo: this.route, queryParams: ''}); 

See also https://angular.io/api/router/Router#createurltree

6 Comments

Thanks, just after login redirect, the first url is a OAuth callback url. I redirect by code not by html tag
this works ,<a routerLink="path" [fragment]="['']" /> . but still have the tag #,like xxx/x#
Sorry, I somehow mixed it with query parameters. Have you tried [fragment]="[]"?
[fragment]="[]" is the same as [fragment]="['']", you are right. Do you know how can remove the fragment in the router without redirecting and navigating? Is there a method?
I guess not. . . .
|
0

If you are for example using auth0 in their doc they do :

this.auth0.parseHash({ _idTokenVerification: false }, (err, authResult) => { if (authResult && authResult.accessToken && authResult.idToken) { window.location.hash = ""; } }); 

Instead do:

this.auth0.parseHash({ _idTokenVerification: false }, (err, authResult) => { if (authResult && authResult.accessToken && authResult.idToken) { this.router.navigate["/home"] } }); 

This will remove the whole hash

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.