Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save spac3unit/05f83ae2cba907fb7f4523d672746cde to your computer and use it in GitHub Desktop.

Select an option

Save spac3unit/05f83ae2cba907fb7f4523d672746cde to your computer and use it in GitHub Desktop.
firebase-by-example: Authenticaiton
var provider = new firebase.auth.GoogleAuthProvider();
document.getElementById('signIn').addEventListener('click', function (e) {
firebase.auth().signInWithRedirect(provider);
});
document.getElementById('signOut').addEventListener('click', function (e) {
firebase.auth().signOut()
.then(function () {
console.log('User signed out');
}).catch(function (error) {
console.error('Error while signing out: ', error);
})
});
firebase.auth().getRedirectResult()
.then(function (result) {
if (result.credential) {
console.log('Got Google Token');
}
var user = result.user;
console.log(user);
}).catch(function (error) {
console.error('Error(code: ' + error.code + ', message: ' + error.message + ')');
});
firebase.auth().onAuthStateChanged(function (user) {
if (user) {
document.getElementById('signedUserName').textContent = 'Signed in user: ' + user.displayName + '[email: ' + user.email + ']';
document.getElementById('userImage').src = user.photoURL;
} else {
document.getElementById('signedUserName').textContent = 'Signed in user: NONE';
document.getElementById('userImage').src = '';
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment