Replies: 3 comments 3 replies
-
I'm not sure if this is actually your issue, but when you're returning the unsubscribe, it's expecting to be in a useEffect. updateAuthState: flow(function* updateAuthState() {
const unsubscribe = onAuthStateChanged(auth, async (user) => {
self.setAuthenticated(user)
})
// Don't forget to unsubscribe when the component unmounts
return () => unsubscribe()
}), This would work more like this: function App(): JSX.Element {
// by returning the unsubscribe here, it'll be properly called on unmount
useEffect(() => rootStore.authentication.updateAuthState(), [])
return (
<SafeAreaView style={backgroundStyle}>
// ...
</SafeAreaView>
)
} Instead, if you want it to unsubscribe inside the MST model itself, you'd want to use the beforeDestroy hook. export const AuthenticationStoreModel = types
.model("AuthenticationStore")
.props({
user: types.optional(types.frozen(), null),
})
.actions(withSetPropAction)
.actions((self) => ({
setAuthenticated: flow(function* (user: User) {
...
self.user = user
...
}),
}))
.actions((self) => ({
updateAuthState: flow(function* updateAuthState() {
// typescript may not like this, but it'll work
self.unsubscribe = onAuthStateChanged(auth, async (user) => {
self.setAuthenticated(user)
})
// Don't forget to unsubscribe when the component unmounts
return () => unsubscribe()
}),
}))
.actions((self) => ({
// This is a lifecycle method that gets called after the model is created.
// It's a good place to do any setup. Especially if you're listening to events.
// In our case, we're listening to the auth state of a user.
afterCreate() {
self.updateAuthState()
},
beforeDestroy() {
// typescript may not like this, but it'll work
self?.unsubscribe()
}
})) This may not be your actual issue, but it's like a real issue. Moving on toward your actual problem, I think I'd need more information about how the page is using the token and what error you're seeing. |
Beta Was this translation helpful? Give feedback.
-
Hey, running into a similar issue in my side - any resolutions? |
Beta Was this translation helpful? Give feedback.
-
Hey 👋 Its been a little while but I have since solved this problem. The issue was that we were attempting to run
I don't immediately see why the same function, as outlined by yourself Jamon, wouldn't run in MST when the app starts, but eh, something to read into. Thank you for your help :))) |
Beta Was this translation helpful? Give feedback.
-
Hey 👋
I am developing a mobile app with firebase email/password authentication, and followed the firebase docs to get it working.
Here is an
auth listener
in the MST:But when I go to a page which makes an API request, then reload the app on Expo Go, the API request fails. The request needs the user object for authentication (using
user.getIdToken()
), and the user object is fetched from the store withuseStores
hook.I attempted to see if the entire app is built (therefore the API request is made) before the
RootStore
is rehydrated inapp.tsx
by console logging stuff in the request and in the MST model above, and the API request logs first, implying that this is true.When I retry the request in the app, it worked perfectly fine.
Is this an issue with how MST is initiated and rehydrated? Am I missing something?
Beta Was this translation helpful? Give feedback.
All reactions