'How can I redirect the parent page after OAuth 2 flow is complete?

My current flow is such:

  1. User clicks login to third party
  2. Upon grant, the third party redirects to configured redirect uri with code
  3. Forward code to backend to verify, etc.
  4. Return success/failure

My frontend is coded to close the popup when the login is successful, but the parent page still just remains the same - standard login page. The goal would be to redirect that page.

I've tried to add an event listener on the window object, but it's not working.

...
  const loginWindow = window.open(oAuthUrl,'targetWindow','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,height=450')
  loginWindow.onbeforeunload = function () {
    alert("new window closed");
  }
};

Is this possible?



Solution 1:[1]

Ok, I have something that seems to be working:

        fetch('http://localhost:8080/user/login', requestOptions)
            .then(response => response.json())
            .then(data => window.close())
            .then(data => window.opener.location.replace("/"))
    }

window.opener allows you to access the parent window.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ryan