I'm trying to make a login process with react native / redux / firebase and i got some issues...
I try to implement onAuthStateChanged to dispatch an action, but it's not working as i want.
It's working for two cases :
1 - I implement directly my onAuthStateChanged in my component like below :
componentDidMount() {
firebaseAuth.onAuthStateChanged()
.then((user) => {
if (user) {
Actions.home();
} else {
Actions.login();
}
});
}
2 - I implement it in as an action with redux-thunk but without dispatch (but then i cant dispatch an action and redirect to my correct route)
export const isAuthenticated = () => {
firebaseAuth.onAuthStateChanged()
.then((user) => {
if (user) {
console.log("launch.action#isAuthenticated - user already connected");
} else {
console.log("launch.action#isAuthenticated - user not connected");
}
});
};
And what i want to do is this (but doesn't work) :
export const isAuthenticated = () => {
return (dispatch) => {
firebaseAuth.onAuthStateChanged()
.then((user) => {
console.log('user', user);
if (user) {
console.log("launch.action#isAuthenticated - user already connected");
dispatch(isUserConnected(true));
} else {
console.log("launch.action#isAuthenticated - user not connected");
dispatch(isUserNotConnected(true));
}
});
};
};
Can someone explain me why it doesn't work with the dispatch ?
Thanks!