title: Firebase Authentication Endless Loop
body: <p>I wrote a program using Firebase Authentication that I want to redirect to a Google SignIn page if a user isn’t signed in, but not to redirect if a user is signed in. Everything works great except for when it goes back to my page after the Google Sign In, it redirects back to the SignIn after only a couple of seconds. Here’s my code:</p>
  
  <pre><code>$(document).ready(function() {
  var provider = new firebase.auth.GoogleAuthProvider();
  var uid;
  var user = firebase.auth().currentUser;
  var xv = 0;
  var yv = 0;
  var xpos = 200;
  var ypos = 200;
  var changeRef = firebase.database().ref();
  var keys = [];
  
  if (user != null) {
  uid = currentUser.uid;
  firebase.database().ref('users/' + uid).set({
  xpos: xpos,
  ypos: ypos
  });
  } else {
  firebase.auth().signInWithRedirect(provider);
  firebase.auth().getRedirectResult().then(function(result) {
  if (result.credential) {
  // This gives you a Google Access Token. You can use it to access the      Google API.
  var token = result.credential.accessToken;
  // ...
  }
  // The signed-in user info.
  user = result.user;
  }).catch(function(error) {
  // Handle Errors here.
  var errorCode = error.code;
  var errorMessage = error.message;
  // The email of the user's account used.
  var email = error.email;
  // The firebase.auth.AuthCredential type that was used.
  var credential = error.credential;
  // ...
  });
  }
  var canvas = document.getElementById("canvas");
  var processingInstance = new Processing(canvas, sketchProc);
  
  });
  </code></pre>