Created
May 10, 2017 14:13
-
-
Save netgfx/a8d6a8d88e077bf0a8e5a79b452e3ceb to your computer and use it in GitHub Desktop.
firebase api
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 FirebaseAPI = FirebaseAPI || {}; | |
| FirebaseAPI = { | |
| currentDB: {}, | |
| signInUser: function(loginMail, password) { | |
| window.console.log("trying to login"); | |
| //Show User Name on Main menu screen | |
| ////////////////////////////////////////////////////////////// | |
| firebase.auth().signOut().then(function() { | |
| // Sign-out successful. | |
| reg.auth = firebase.auth().onAuthStateChanged(function(user) { | |
| window.console.log(">>>> ", user); | |
| if (user) { | |
| $("#loginContainer, .modal").addClass("hidden"); | |
| //FirebaseAPI.addDB(); | |
| console.log(user); | |
| FirebaseAPI.currentDB = firebase.database(); | |
| PersistenceManager.createManagers(); | |
| GAME.Menu.prototype.showUserName(GAME.Menu.prototype._this); | |
| if (firebase.auth().currentUser.emailVerified === false) { | |
| GAME.Menu.prototype.unverifiedAccount(GAME.Menu.prototype._this); | |
| } | |
| reg.auth = undefined; | |
| } else { | |
| // No user is signed in. | |
| } | |
| }); | |
| }, function(error) { | |
| // An error happened. | |
| }); | |
| firebase.auth().signInWithEmailAndPassword(loginMail, password).catch(function(error) { | |
| // Handle Errors here. | |
| showLoginError(); | |
| var errorCode = error.code; | |
| var errorMessage = error.message; | |
| window.console.log(errorCode, errorMessage); | |
| // ... | |
| }); | |
| }, | |
| signOutUser: function() { | |
| firebase.auth().signOut().then(function() { | |
| console.log('Signed Out'); | |
| }, function(error) { | |
| console.error('Sign Out Error', error); | |
| }); | |
| }, | |
| registerUser: function(username, password, email) { | |
| var callback = firebase.auth().createUserWithEmailAndPassword(email, password).catch(function(error) { | |
| // Handle Errors here. | |
| showRegisterError(); | |
| var errorCode = error.code; | |
| var errorMessage = error.message; | |
| window.console.log(errorCode, errorMessage); | |
| // ... | |
| }); | |
| callback.then(function(user) { | |
| window.console.log(user); | |
| FirebaseAPI.pushUserData(user.uid, username, user.email); | |
| $(".register").addClass("hidden"); | |
| $("#email").val(user.email); | |
| firebase.auth().currentUser.sendEmailVerification(); | |
| }); | |
| }, | |
| pushUserData: function(userId, name, email) { | |
| //var user = firebase.auth().currentUser | |
| //var name = user.displayName; | |
| //var uid = user.uid; | |
| //var email = user.email; | |
| /*var callback = firebase.database().ref('users/' + userId).set({ | |
| username: name, | |
| email: email, | |
| hero: "", | |
| progress: "", | |
| team: "", | |
| selectedType: "" | |
| }).catch(function(error) { | |
| // Handle Errors here. | |
| var errorCode = error.code; | |
| var errorMessage = error.message; | |
| // ... | |
| }); | |
| */ | |
| callback.then(function() { | |
| window.console.log("done"); | |
| }); | |
| }, | |
| loginUserSuccess: function() { | |
| }, | |
| showLogin: function() { | |
| }, | |
| addDB: function(fn) { | |
| fn = fn || function() {}; | |
| var user = firebase.auth().currentUser; | |
| var id; | |
| if (user !== null) { | |
| // User is signed in. | |
| id = user.uid | |
| window.console.log(user,"FN IS: ", fn); | |
| firebase.database().ref('users/' + id).on('value', function(snapshot) { | |
| window.console.log(snapshot.val()); | |
| }); | |
| } else { | |
| } | |
| }, | |
| getDB: function() { | |
| return FirebaseAPI.currentDB; | |
| }, | |
| getUsername: function(fn) { | |
| var user = firebase.auth().currentUser; | |
| if (user) { | |
| var id = user.uid; | |
| var username; | |
| firebase.database().ref('users/' + id).once('value').then(function(snapshot) { | |
| name = snapshot.val().name; | |
| fn(name); | |
| }); | |
| return name; | |
| } | |
| }, | |
| saveData: function(type, data) { | |
| type = type || "error"; | |
| var _data = {}; | |
| _data[type] = data; | |
| var user = firebase.auth().currentUser; | |
| var id = firebase.auth().currentUser.uid; | |
| firebase.database().ref('users/' + id).update(_data); | |
| }, | |
| getTeamData: function(fn) { | |
| fn = fn || function() {}; | |
| }, | |
| getData: function(fn) { | |
| fn = fn || function() {}; | |
| FirebaseAPI.addDB(fn); | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment