I'm interested in how to implement OAuth in React using popup (window.open).
For example I have:
mysite.com — this is where I open the popup. passport.mysite.com/oauth/authorize — popup.The main question is how to create connection between window.open (popup) and window.opener (as it's known the window.opener is null due to cross-domain security therefore we can't use it anymore).
⇑
window.openeris removed whenever you navigate to a different host (for security reasons), there is no way around it. The only option should be doing the payment in a frame if it is possible. The top document needs to stay on the same host.
Scheme:

Possible solutions:
setInterval described here.So what's the best recommended approach in 2019?
Wrapper for React - https://github.com/Ramshackle-Jamathon/react-oauth-popup
Suggested by Khanh TO. OAuth popup with localStorage. Based on react-oauth-popup.
Scheme:

Code:
oauth-popup.tsx:
import React, {PureComponent, ReactChild} from 'react'
type Props = {
  width: number,
  height: number,
  url: string,
  title: string,
  onClose: () => any,
  onCode: (params: any) => any,
  children?: ReactChild,
}
export default class OauthPopup extends PureComponent<Props> {
  static defaultProps = {
    onClose: () => {},
    width: 500,
    height: 500,
    url: "",
    title: ""
  };
  externalWindow: any;
  codeCheck: any;
  componentWillUnmount() {
    if (this.externalWindow) {
      this.externalWindow.close();
    }
  }
  createPopup = () => {
    const {url, title, width, height, onCode} = this.props;
    const left = window.screenX + (window.outerWidth - width) / 2;
    const top = window.screenY + (window.outerHeight - height) / 2.5;
    const windowFeatures = `toolbar=0,scrollbars=1,status=1,resizable=0,location=1,menuBar=0,width=${width},height=${height},top=${top},left=${left}`;
    this.externalWindow = window.open(
        url,
        title,
        windowFeatures
    );
    const storageListener = () => {
      try {
        if (localStorage.getItem('code')) {
          onCode(localStorage.getItem('code'));
          this.externalWindow.close();
          window.removeEventListener('storage', storageListener);
        }
      } catch (e) {
        window.removeEventListener('storage', storageListener);
      }
    }
    window.addEventListener('storage', storageListener);
    this.externalWindow.addEventListener('beforeunload', () => {
      this.props.onClose()
    }, false);
  };
  render() {
    return (
      <div onClick={this.createPopup)}>
        {this.props.children}
      </div>
    );
  }
}
app.tsx
import React, {FC} from 'react'
const onCode = async (): Promise<undefined> => {
  try {
    const res = await <your_fetch>
  } catch (e) {
    console.error(e);
  } finally {
    window.localStorage.removeItem('code'); //remove code from localStorage
  }
}
const App: FC = () => (
  <OAuthPopup
    url={<your_url>}
    onCode={onCode}
    onClose={() => console.log('closed')}
    title="<your_title>">
    <button type="button">Enter</button>
  </OAuthPopup>
);
export default App;
                        I once encounter an issue on my oauth login flow with window.open/window.opener bug on ms-edge
My flow before this issue was
My flow after this issue was
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With