Denoder / nuxt-module-alternatives

Alternative modules to use while waiting for Nuxt 3 Compatibility
MIT License
144 stars 14 forks source link

Nuxt Auth fetchUser always redirects to homepage #91

Closed nfunwigabga closed 2 years ago

nfunwigabga commented 2 years ago

Whenever I try to fetch the current user, the user is fetched successfully but there is always a redirect to the homepage.

try:

const {$auth} = useNuxtApp()

onMounted(() => {
    $auth.fetchUser()
})

Regardless of which page I am on, I get redirected to the home page after I get the response.

Using

"@nuxtjs-alt/auth": "^2.0.14",
"@nuxtjs-alt/http": "^1.3.14",
"@nuxtjs-alt/proxy": "^1.3.5",
"@pinia/nuxt": "^0.4.3"
Denoder commented 2 years ago

update to the latest version and try

nfunwigabga commented 2 years ago

This is fixed in the latest version. However, it seems to have introduced another minor issue... the user is not redirected away from the the login screen after login, even when watchLoggedIn: true is set in the configuration.

Denoder commented 2 years ago

I can't reproduce this issue on my end.

nfunwigabga commented 2 years ago

Hmm. weird. I just installed everything on a fresh project and got the same result. No redirect after login. I have to manually do navigateTo("/").

Here is how I set up the fresh install. Maybe I am missing something in the setup then? https://github.com/nfunwigabga/nuxt-auth-test

Everything else seems to be ok, including middleware etc.

Denoder commented 2 years ago

I suggest just manually redirecting after login then. This is low priority for me at the moment.

nfunwigabga commented 2 years ago

Yep, that's what I'm doing. And yes it's low priority really.

For anyone looking, you may want to redirect the user the the intended URL stored int he cookies on login. So this is what I'm doing:

const redirectUrl = useCookie('auth.redirect', { default: () => "/"})
const {$auth}=useNuxtApp()

async function login(){
   try {
    await $auth.loginWith("local", {
      body: {email: "foo@bar.com", password: "password" },
    });
    navigateTo(redirectUrl.value);
    redirectUrl.value = null; // clear the cookie on successful login
  } catch (error) {
    errors.value = error.data?.errors;
  }
}

Cheers!

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.