Closed andre-silva9975 closed 2 months ago
I realized that the api calls that are performed again after logging in, are those which have the token in the headers and are called using useFetch(). I changed to $fetch() and those calls are no longer perform when doing log in. However, when fetching data in the created() method I have to use useFetch() since I believe it's the correct way to fetch data. This does not cause any severe problems to me, because GET requests do not change the database state.
Just quickly checked your reproduction, useFetch
should also be called top level in your setup only since it's a composable. Incorrect registration might cause duplicate requests due to inproper cleanup
Hi @andre-silva9975 👋
As mentioned by @warflash, useFetch
will re-run anytime that the dependencies passing into it are updated. In this case, you reference const { token } = useAuth()
inside the useFetch
method, which changes from your token to null
after logging out, hence triggering another fetch.
We have discussed adding a composable like useAuthenticatedFetch()
to make requests out of the box with your token
set. You can follow along in the conversation and also see how other solved making authenticated requests here in #688
Hi @andre-silva9975 👋
As mentioned by @warflash,
useFetch
will re-run anytime that the dependencies passing into it are updated. In this case, you referenceconst { token } = useAuth()
inside theuseFetch
method, which changes from your token tonull
after logging out, hence triggering another fetch.We have discussed adding a composable like
useAuthenticatedFetch()
to make requests out of the box with yourtoken
set. You can follow along in the conversation and also see how other solved making authenticated requests here in #688
Hello.
I was able to fix this problem by changing from options API to composition API and by calling the composable inside setup like @warflash told before. Thanks for the explanation!
Environment
Build Modules: -
Reproduction
https://github.com/andre-silva9975/nuxt-auth-playground
Describe the bug
I am experiencing an issue with the authentication module where ,for example, a POST request is unintentionally repeated upon re-login after logging out. Here are the steps to reproduce the issue:
The README file explains the actions necessary to reproduce this problem.
One workaround I found was to reload the page before calling the "signOut method. The if I log in the the previous request is not performed.
Expected Behavior:
After logging in again, no previous POST requests should be automatically repeated.
Actual Behavior:
When re-login, the POST request made in step 2 is repeated without any user action.
Additional context
No response
Logs
No response