Created
October 19, 2017 08:20
-
-
Save sharon-rozinsky/2676a82069f596859193b10ee54504bb to your computer and use it in GitHub Desktop.
firebase-by-example: Authenticaiton
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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