Skip to content

Instantly share code, notes, and snippets.

@azamsharp
Created October 18, 2018 13:48
Show Gist options
  • Save azamsharp/380f9d13558a1ff8a66f6465f85326da to your computer and use it in GitHub Desktop.
Save azamsharp/380f9d13558a1ff8a66f6465f85326da to your computer and use it in GitHub Desktop.
// sign up
let emailTextBox = document.getElementById("emailTextBox")
let passwordTextBox = document.getElementById("passwordTextBox")
let btnSignUp = document.getElementById("btnSignUp")
// login
let loginEmailTextBox = document.getElementById("loginEmailTextBox")
let loginPasswordTextBox = document.getElementById("loginPasswordTextBox")
let btnLogin = document.getElementById("btnLogin")
// signout
let btnSignOut = document.getElementById("btnSignOut")
btnSignOut.addEventListener('click',function(){
firebase.auth().signOut()
})
// when the user auth status changed
firebase.auth().onAuthStateChanged(function(user){
if(user) {
console.log("onAuthStateChanged")
//window.location = "my-tasks.html"
}
})
// sign in user
btnLogin.addEventListener('click',function(){
let email = loginEmailTextBox.value
let password = loginPasswordTextBox.value
firebase.auth().signInWithEmailAndPassword(email,password)
.then(function(response){
let user = response.user
if(user) {
// go to a different page
// home page
// window.location = "my-tasks.html"
}
console.log(response)
// get the current user
// let currentUser = firebase.auth().currentUser
// currentUser.uid // unique identifier for the user
}).catch(function(error){
console.log(error)
})
})
// creates a new user
btnSignUp.addEventListener('click',function(){
let email = emailTextBox.value
let password = passwordTextBox.value
firebase.auth().createUserWithEmailAndPassword(email,password)
.then(function(response){
console.log(response)
}).catch(function(error){
console.log(error)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment