简体   繁体   中英

Capturing query string before Angular Constructor is called

I am in a situation where I am authenticating with MSAL using a third party tool (Strapi).

  1. Open Angular site
  2. Redirect to Microsoft login
  3. Redirect back to Angular app with valid token.

In between step 2 and 3, there is a separate access token coming from Strapi as a query string in the url, which I need to send back to Strapi in order to get a token from it. This token is meant to give access to the CMS. The intention is to use Strapi as a SSO provider for Microsoft in order to get access to MSAL and Strapi at once.

The access token I am getting from Strapi is valid, but the issue is that the query string in the url is lost (only appears for a second, but can be verified in the Network tab) once the Angular app is initialized. So normally where you could capture the query string in the constructor, I cannot capture it because it is gone before the constructor is called.

I'm not sure how to go about saving this value so I can send it where I need to.

Below are two examples of things I've tried that haven't worked because of what I described above.

constructor(
    public mediaObserver: MediaObserver,
    private router: Router,
    private authService: MsalService,
    private broadcastService: BroadcastService,
    private apibundleService: ApibundleService,
    private permissionsService: NgxPermissionsService,
    private http: HttpClient,
    private route: ActivatedRoute
  ) {
    this.route.queryParams.subscribe((params) => {
      this.queryAccessToken = params['access_token'];
      console.log(this.queryAccessToken);
    });
  }
constructor(
    public mediaObserver: MediaObserver,
    private router: Router,
    private authService: MsalService,
    private broadcastService: BroadcastService,
    private apibundleService: ApibundleService,
    private permissionsService: NgxPermissionsService,
    private http: HttpClient,
    private route: ActivatedRoute
  ) {
   this.route.fragment
  .pipe(map(fragment => fragment))
  .subscribe(fragment => {
    let f = fragment.match(/^(.*?)&/);
    if(f) {
      let token: string = f[1].replace('access_token=', '');     
    }
  })
  }

Created an issue here. https://github.com/AzureAD/microsoft-authentication-library-for-js/issues/3242

It is already resolved in 1.4.2. Please update the library.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM