Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
158
rated 0 times [  164] [ 6]  / answers: 1 / hits: 28029  / 8 Years ago, thu, june 2, 2016, 12:00:00

I'm trying to build a website that uses email auth for users. However, everytime I try to sign up or log in a user, the firebase.auth().onAuthStateChanged function fires, but doesn't recognize that a user has logged in or signed up. This is my current code. I know it works because it will alert me, No user! after every log in or sign up and because I can go into my Firebase console and see that the user has signed up. If anyone knows how to fix this, I would appreciate it!



Thanks!



Code:



 function initApp() {
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
alert(Signed in user!)
} else {
alert(No user!)
}
});
}

window.onload = function() {
initApp();
};


CODE FOR LOGIN & SIGNUP:



function toggleSignIn() {
if (firebase.auth().currentUser) {
alert(Sign out)
firebase.auth().signOut();
// [END signout]
} else {
var email = document.getElementById('email').value;
var password = document.getElementById('pass').value;
if (email.length < 4) {
alert('Please enter an email address.');
return;
}
if (password.length < 4) {
alert('Please enter a password.');
return;
}
// Sign in with email and pass.
// [START authwithemail]
firebase.auth().signInWithEmailAndPassword(email, password).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
// [START_EXCLUDE]
if (errorCode === 'auth/wrong-password') {
alert('Wrong password.');
} else {
console.error(error);
}
// [END_EXCLUDE]
});
// [END authwithemail]
}
}

function handleSignUp() {
var email = document.getElementById('semail').value;
var password = document.getElementById('spass').value;

if (password.length < 6) {
alert('Password must be 6 characters or more!');
return;
}
// Sign in with email and pass.
// [START createwithemail]
firebase.auth().createUserWithEmailAndPassword(email, password).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
// [START_EXCLUDE]
if (errorCode == 'auth/weak-password') {
alert('The password is too weak.');
} else {
console.error(error);
}
// [END_EXCLUDE]
});
// [END createwithemail]
}

More From » html

 Answers
6

It appears that my problem was my domain was not authorized for OAuth operations for that Firebase project... I had forgotten to add my domain to the list of authorized ones.



To fix this, go to your Firebase project > Authentication > Sign-In Method > Add your domain under Authorized Domains.



Thanks to @Ymmanuel for the help.


[#61920] Tuesday, May 31, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
josefn

Total Points: 251
Total Questions: 93
Total Answers: 84

Location: Senegal
Member since Fri, Aug 21, 2020
4 Years ago
;