1
votes

I redirect my Angular application to the Spotify Login (https://accounts.spotify.com/authorize) Once they login they are redirected back to http://localhost:4200/callback. This URL has a token attached to it. But it immediately redirects to (I want it to redirect here):

const appRoutes: Routes = [{ path: 'callback', redirectTo: '/host', pathMatch: 'full' }]

How/where can I preserve the auth token without having it sit in the users URL?

I realize I will eventually need AuthGuards as well, but first I want to retrieve the token.

2
hey Larson. I'm trying to achieve something of this sort (for keycloak) but I've got this useHash: true. Any idea how can I achieve the same with useHash set to true since keycloak doesn't allow # in the redirect_uri?Junaid

2 Answers

2
votes

I suggest you don't directly set "redirectTo" in the routing configuration. Instead, add a callbackComponent to handle the token and redirection. Like following:

const appRoutes: Routes = [
    { path: 'callback', component: CallbackComponent }
]

In CallbackComponent

export class CallbackComponent implements OnInit {
    constructor(private route: ActivatedRoute, private router: Router) {}

    public ngOnInit():void {
        const token = this.route.snapshot.queryParamMap.get('token');
        // Handle token
        // ...
        this.router.navigate(['./host']);
    }
}
1
votes

Verify for auto redirection should not be handled in app.component.ts and if yes then handle it via guards because it is responsible for redirection.

Then

constructor(private route: ActivatedRoute){}
  public ngOnInit():void {
        const token = this.route.snapshot.queryParamMap.get('token');
        // Handle token
        // ...
        this.router.navigate(['./host']);
    }