title: How to get Google authentication to work with ReactJs?
body: <p>I have been trying the whole day to get Google authentication to work with React, but after searching for hours trying everything i found i only get a buggy, sometimes-working, mess. Can anyone see what i am doing wrong? (This is for a school project so i'm learning!)</p>
  
  <p>The project will be an online schedule creator.</p>
  
  <p>My main Index.js has the following render-function:</p>
  
  <pre><code>  function renderApp() {
      ReactDOM.render(
        &lt;Router history={browserHistory}&gt;
          &lt;Route path="/" component={Login}&gt;&lt;/Route&gt;
          &lt;Route component={AuthUserContainer}&gt;
            &lt;Route path="/auth" component={Layout}&gt;
              &lt;IndexRoute component={Overview}&gt;&lt;/IndexRoute&gt;
              &lt;Route path="schedule(/:scheduleid)" component={Schedule}&gt;&lt;/Route&gt;
            &lt;/Route&gt;
          &lt;/Route&gt;
        &lt;/Router&gt;,
      app);
    }
  </code></pre>
  
  <p>The home-page is simply a login page, handled by the Login component which will redirect the user to /auth if the authentication is successful. Authentication is continually handled by AuthUserContainer everytime a protected route is accessed.</p>
  
  <p>The Login component has the Google Sign In button, and all protected routes has (via Layout component) a Google Sign Out button.</p>
  
  <p>My Header.js contains the "Google Sign Out button" (signOutHandler):</p>
  
  <pre><code>export default class Header extends React.Component {
    constructor(props) {
      super(props);
      this.state = {};
      this.SignOutHandler = this.SignOutHandler.bind(this);
    }
  
    SignOutHandler() {
      var auth2 = gapi.auth2.getAuthInstance();
      auth2.signOut().then(function () {
        console.log('User signed out.');
        Authenticate.signOutUser();
      });
    }
  
    render() {
      return (
        &lt;div class="header-content"&gt;
          &lt;h2 class="header-title"&gt;Schedule&lt;/h2&gt;
          &lt;h3 class="header-user-button"&gt;
              &lt;a href="/" onClick={this.SignOutHandler}&gt;User First Name&lt;/a&gt;
          &lt;/h3&gt;
        &lt;/div&gt;
      );
    }
  }
  </code></pre>
  
  <p>I used Googles own example code and tried to tweak it for React. Problems i get with this code is "Uncaught TypeError: Cannot read property 'getAuthInstance' of undefined". Since i split the Sign In and Sign Out buttons to separate pages auth2 doesn't get initialised properly, but clicking the button twice can make it work. Trying to initialise it manually here often gives me the error that Gapi is undefined...</p>
  
  <p>Finally the Login.js:</p>
  
  <pre><code>export default class Login extends React.Component {
    constructor(props) {
      super(props);
      this.state = {};
      this.onSignIn = this.onSignIn.bind(this);
      this.renderGoogleLoginButton = this.renderGoogleLoginButton.bind(this);
    }
  
    renderGoogleLoginButton() {
        console.log('rendering google signin button')
        gapi.signin2.render('my-signin2', {
          'scope': 'https://www.googleapis.com/auth/plus.login',
          'width': 200,
          'height': 50,
          'longtitle': true,
          'theme': 'light',
          'onsuccess': this.onSignIn
        });
  
        gapi.load('auth2', function() {
          gapi.auth2.init();
        });
      }
  
    componentDidMount() {
        window.addEventListener('google-loaded', this.renderGoogleLoginButton);
    }
  
    onSignIn(googleUser) {
      Authenticate.signInUser(googleUser.getBasicProfile());
      this.props.router.push("/auth");
    }
  
    render() {
      return (
        &lt;div class="container"&gt;
          &lt;h2 class="form-signin-heading"&gt;
            Sign-in with Google account required
          &lt;/h2&gt;
          &lt;div id="my-signin2"&gt;&lt;/div&gt;
        &lt;/div&gt;
      );
    }
  }
  </code></pre>
  
  <p>I based this code also on Googles own, but that didn't work at all (either the button didn't load, simply nothing showed, or it never called onSignIn). I also found and tried <a href="https://stackoverflow.com/questions/31640234/using-google-sign-in-button-with-react-2">this</a>, which made it kinda work (sometimes). I also tried to initialise auth2 in renderGoogleLoginButton(), but that only works sometimes aswell...</p>
  
  <p>renderGoogleLoginButton() is called by the even-handler of a custom event (google-loaded) which fires when the Google API loads (exactly like in the link). Authenticate is my own class which bundles together my creation of token, getUserName functions etc.</p>
  
  <p>I also get the following error from gapi: "Uncaught TypeError: Cannot read property 'style' of null", i think it tries to reference the Sign In/ Sign Out buttons but since they aren't loaded at the time they are null.</p>
  
  <p>I cannot find any good solutions that doesn't result in glitches or bugs. <a href="https://developers.google.com/identity/sign-in/web/sign-in" rel="nofollow noreferrer">Googles own code</a> places everything in the same HTML-document, but since i want to split it up into components then it all breaks. Am i doing this completly wrong perhaps? I'm very new to React but hopefully it's understandable!</p>
  
  <p>Sorry for the long post, thank you so much for your time, and if you have any questions just ask!</p>