instead of the welcome + user.uid message, why does this happen if the users have already been inserted? Please help me. I appreciate those who take the time and trouble to tell me where my code is failing. Thank you very much in advance. the description as long as possible because it tells me that my post is almost purely code.
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s.
This is the Auten.js component
import React from 'react';
const firebase = require('firebase');
// Your web app's Firebase configuration
var firebaseConfig = {
apiKey: "XXXXXX",
authDomain: "fir-login-2d466.firebaseapp.com",
databaseURL: "https://fir-login-2d466.firebaseio.com",
projectId: "fir-login-2d466",
storageBucket: "fir-login-2d466.appspot.com",
messagingSenderId: "303960039772",
appId: "1:303960039772:web:909c622cd19a61f2"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
class Auten extends React.Component {
constructor(props)
{
super(props);
this.state = {
err: ''
};
this.login = this.login.bind(this);
this.signup = this.signup.bind(this);
}
login(event)
{
const email = this.refs.email.value;
const password = this.refs.password.value;
console.log(email, password);
const auth = firebase.auth();
const promise = auth.signInWithEmailAndPassword(email, password);
promise.catch(e => {
let err = e.message;
console.log(err);
this.setState({err: err});
});
}
signup()
{
const email = this.refs.email.value;
const password = this.refs.password.value;
console.log(email, password);
const auth = firebase.auth();
const promise = auth.createUserWithEmailAndPassword(email, password);
promise.then(user => {
let err = "Welcome " + user.email;
firebase.database().ref('/users' + user.uid).set({
email: user.email
});
console.log(user);
this.setState({err: err});
});
promise.catch(e => {
let err = e.message;
console.log(err);
this.setState(({err: err}));
});
}
render(){
return(
<div>
<input id = "email" ref = "email" type = "email" placeholder = "Enter your email." /><br />
<input id = "password" ref = "password" type = "password" placeholder = "Enter your password." /><br />
<p>{this.state.err}</p>
<button onClick = {this.login}>Log In</button>
<button onClick = {this.signup}>Sign Up</button>
<button>Log Out</button>
</div>
);
}
}
export default Auten;
Here is an image showing that if users are inserted.
The problem is that you are not logging the user, but you are registering it
With
createUserWithEmailAndPassword(email,password)
you create the user, withsignInUserWithEmailAndPassword(email,password)
it you log inIn the promise of success
createUserWithEmailAndPassword()
you can take it to another screen and give it the welcome message, but that will be the first time since that user is not registered in the database, but later, if you do it again you will not power because that user is already created, and you will get the error in the promise with the legend that there is already a user with that email in the databaseTo fix it you will have to use
You can read a little more HERE
I was able to solve the error, you have to change both methods for this: