0

I have an Angular app which uses a websocket connection to interact with a server. Now I need to implement OAuth in third party service, however I cannot handle any events on new tab opened via window.open(url).

The OAuth API redirects the window to my domain and returns token in URL query params. I need to catch this token to work with this. How can this be implemented?


const eHealthWind = window.open(msg.data.url);
console.log(eHealthWind.window);
    eHealthWind.onload = function () {
    eHealthWind.onpopstate = function (e) {
        console.log('pop', e);
    };
};
12
  • Please show what you have tried so far. Commented Jan 29, 2019 at 14:41
  • There is nothing to show. Just line which opens new tab. And another line which tries to apply EventListener to this window (as it occurred cannot be applied to tab opened with another domain) Commented Jan 29, 2019 at 14:44
  • @HenslerSoftware added this poor code. Not sure whether it will help somehow. Commented Jan 29, 2019 at 14:46
  • have you tried implementing new Subject and emit change (token) as observable, when you redirect to your domain? i can provide example of code but don't know if this will help you. Commented Jan 29, 2019 at 14:48
  • That's not me who is redirecting. That is a third party API. eHealth if be concrete. I need to open their page so that user authenticates then page is redirected to my domain and passes token in url params Commented Jan 29, 2019 at 14:49

1 Answer 1

1

This would be a possible way:


     const loggedInPromise = new Promise((res) => {
       const eHealthWind = window.open(msg.data.url);

        const intervalId = setInterval(function() {
          try {
            res(eHealthWind.location.href);
            clearInterval(intervalId);
            eHealthWind.close();
          } catch (error) {

          }
        }, 1000);
      })

      loggedInPromise.then((urlWithToken) => { console.log(urlWithToken); })


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

4 Comments

Sorry, where does this urlWithToken go? I don't see any variable declaration. Am I missing it?
Edited the answer. You just resolve the promise with the href that should contain the token
I ran into my own problems. Was solving. Thanks for your answer. It works.
please change cancelIntercal into clearInterval since the first one doesn't exist

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.