sidebase / nuxt-auth

Authentication built for Nuxt 3! Easily add authentication via OAuth providers, credentials or Email Magic URLs!
https://auth.sidebase.io
MIT License
1.31k stars 164 forks source link

API call is repeated upon re-login after logout without user action #812

Closed andre-silva9975 closed 2 months ago

andre-silva9975 commented 4 months ago

Environment


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. network

Additional context

No response

Logs

No response

andre-silva9975 commented 4 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.

warflash commented 4 months ago

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

zoey-kaiser commented 2 months ago

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

andre-silva9975 commented 2 months ago

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

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!