Created
July 19, 2016 21:18
-
-
Save GibranPolonsky/d1516214af2942c97b7dca4ff2989e3a to your computer and use it in GitHub Desktop.
REACT Login Services with Firebase
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
import Rebase from 're-base'; | |
const ref = new Firebase('https://aiolostest.firebaseio.com'); | |
class AuthServices { | |
constructor(){ | |
} | |
getCurrentUser(callback){ | |
let users = ref.child('users'); | |
users.orderByChild('uid').equalTo(ref.getAuth().uid).on("value", callback); | |
} | |
updateUser(id, value){ | |
let user = ref.child('users').child(id); | |
user.set(value); | |
} | |
createUser(user){ | |
ref.createUser({ | |
email : user.email, | |
password : user.password | |
}, function(error, userData) { | |
if (error) { | |
console.log("Error creating user:", error); | |
} else { | |
console.log("Successfully created user account with uid:", userData.uid); | |
let users = ref.child('users'); | |
let newUser = users.push(); | |
newUser.set({ | |
'uid' : userData.uid, | |
'name' : user.name, | |
'email' : user.email | |
}); | |
ref.authWithPassword({ | |
email : user.email, | |
password : user.password | |
}, function(error, authData) { | |
if (error) { | |
console.log("Login Failed!", error); | |
} else { | |
console.log("Authenticated successfully with payload:", authData); | |
localStorage.setItem('token', authData.token); | |
window.location.href = "./#/dashboard/true" | |
} | |
}); | |
} | |
}); | |
} | |
loggingWithEmail(user, callback){ | |
ref.authWithPassword({ | |
email : user.email, | |
password : user.password | |
}, callback); | |
} | |
} | |
export default AuthServices; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment