Reputation: 89
My sign out user function does not seem to work at all. I am calling it from an alert in login, but it doesn't seem to call it. If I try to add "()" at the end it just gives me an error.
loginUser = (email, password) => {
try {
firebase.auth().signInWithEmailAndPassword(email, password).then(function (user){
console.log(user)
console.log('we are logged in boys')
Alert.alert(
'Signed In',
'You have signed in. Well done!',
[
{text: 'Sign Out', onPress: () => this.signOutUser},
],
{ cancelable: false }
)
})
}
catch(error) {
console.log(error.toString())
}
}
signOutUser = () => {
console.log('Do we reach this or not')
firebase.auth().signOut().then(function (user){
// Sign-out successful.
console.log('We are signing out!')
}).catch(function(error) {
// An error happened.
console.log('There is an issue!')
});
}
Upvotes: 2
Views: 2112
Reputation: 5023
this
inside signInWithEmailAndPassword
callback is not what you are expecting. change the callback function to arrow function, like
firebase.auth().signInWithEmailAndPassword(email, password).then((user) => {
...
});
Also, change the assignment onPress function as
{text: 'Sign Out', onPress: this.signOutUser}
Hope this will help!
Upvotes: 1
Reputation: 1514
Update your function with arrow function:
loginUser = (email, password) => {
try {
firebase.auth().signInWithEmailAndPassword(email, password).then((user)=> {
console.log(user)
console.log('we are logged in boys')
Alert.alert(
'Signed In',
'You have signed in. Well done!',
[
{text: 'Sign Out', onPress: () => this.signOutUser()},//Or Write this.signOutUser direct without arrow function
],
{ cancelable: false }
)
})
}
catch(error) {
console.log(error.toString())
}
}
Hope this is helpful.
Upvotes: 1
Reputation: 4199
Update your function like this
loginUser = (email, password) => {
try {
firebase.auth().signInWithEmailAndPassword(email, password).then((user)=> {
console.log(user)
console.log('we are logged in boys')
Alert.alert(
'Signed In',
'You have signed in. Well done!',
[
{text: 'Sign Out', onPress: () => this.signOutUser},
],
{ cancelable: false }
)
})
}
catch(error) {
console.log(error.toString())
}
}
The function inside then() should be pointer function so that it can access this
context
Upvotes: 0
Reputation: 413
test this:
{text: 'Sign Out', onPress: () => { this.signOutUser(); } }
Upvotes: 0