4

How to get access_token from keycloak server to my authorization component. The authorization url is looking as following:

http://localhost:8081/auth/realms/external/protocol/openid-connect/auth?client_id=myapp&redirect_uri=http%3A%2F%2Flocalhost%3A4200%2Fauth&response_type=token&scope=openid&nonce=test123

The callback url is in http://localhost:4200/auth , here is the component:

import { Component, OnInit } from '@angular/core';
import {ActivatedRoute} from '@angular/router';

@Component({
selector: 'app-auth',
templateUrl: './auth.component.html',
styleUrls: ['./auth.component.scss']
})
export class AuthComponent implements OnInit {

constructor(private route: ActivatedRoute) { }

ngOnInit() {
    console.log(this.route.snapshot);
    console.log(this.route.snapshot);
}

}

After a successful login, access_token will be returned from keycloak, something like this:

http://localhost:4200/auth#access_token=qwerty123&session_time=123456789

My question is, how to get that to work, and getting the access_token.

1
  • 1
    simply read it from location.hash Commented Feb 7, 2020 at 10:41

1 Answer 1

5

The following should work

 this.route.queryParams.subscribe(params => {
      const accesToken = this.route.snapshot.fragment
        .split("&")[0]
        .replace("/", "")
        .split("=")[1];
    });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.