Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redirect back to requested page after authentication #861

Closed
3 of 5 tasks
andychukse opened this issue Aug 13, 2024 · 0 comments
Closed
3 of 5 tasks

Redirect back to requested page after authentication #861

andychukse opened this issue Aug 13, 2024 · 0 comments
Labels
enhancement An improvement that needs to be added pending An issue waiting for triage

Comments

@andychukse
Copy link
Contributor

andychukse commented Aug 13, 2024

Describe the feature

Redirect user back to requested page after authentication
Add redirect query parameter to middleware navigateTo when a guest visits authenticated page.
This will help users redirect back to previous page after sign in.

// nuxt.config.ts

provider: {
  type: 'local',
  refresh: { 
        isEnabled: true, 
        endpoint: { path: '/refresh', method: 'POST' }, 
        refreshOnlyToken: true
   },
  pages: {
    login: '/login',
  },
},
 sessionRefresh: {
   enablePeriodically: 90000,
   enableOnWindowFocus: true,
 },
globalAppMiddleware: {
   isEnabled: true,
   addDefaultCallbackUrl: '/account'
},
// login.vue

definePageMeta({
   auth: {
     unauthenticatedOnly: true,
   },
})

Currently when guest tries to access a protected page they are redirected to /login page and after login they are redirect to default callbackUrl unless they specify callbackUrl in the signIn function.

This feature will help automatically redirect a user back to the page they requested after login. For example, if they requested to access '/profile' and they are redirected to login page, after successful login, they will be redirected back to '/profile'

How would you implement this?

Current code
Current implementation redirects to login page

} else {
return navigateTo(authConfig.provider.pages.login)
}
})

Modified code
Proposed implementation gets the current url page and append as redirect query to the login page

return navigateTo({
      path: authConfig.provider.pages.login,
      query: {
        redirect: to.fullPath,
      },
    })
  }

https://github.com/andychukse/nuxt-auth/blob/f3ccf913a9121ac874b30c8b4a9a6e534a036d36/src/runtime/middleware/auth.ts#L108C5-L113C7

Current signIn function or local and refresh providers checks if callbackUrl is set, if not get the default callbackUrl

if (typeof callbackUrl === 'undefined') {
callbackUrl = await determineCallbackUrl(runtimeConfig.public.auth, () => getRequestURLWN(nuxt))
}
if (redirect) {
return navigateTo(callbackUrl)
}
}

To handle the redirect on refresh Provider and local provider.
Check for the presence of redirect query parameter on the url and navigate to that if no callbackUrl is set. If no redirect query parameter is detected, then fallback to the default callbackUrl.

implementation in local and refresh provider

if (typeof callbackUrl === 'undefined') {
    if (useRoute()?.query?.redirect){
      callbackUrl = useRoute().query.redirect?.toString()
    } else {
      callbackUrl = await determineCallbackUrl(runtimeConfig.public.auth, () => getRequestURLWN(nuxt))
    }
  }
  if (redirect) {
    return navigateTo(callbackUrl)
  }
}

https://github.com/andychukse/nuxt-auth/blob/f3ccf913a9121ac874b30c8b4a9a6e534a036d36/src/runtime/composables/local/useAuth.ts#L62C3-L69C4

Additional information

  • Would you be willing to help implement this feature?

Provider

  • AuthJS
  • Local
  • Refresh
  • New Provider
@andychukse andychukse added enhancement An improvement that needs to be added pending An issue waiting for triage labels Aug 13, 2024
phoenix-ru added a commit that referenced this issue Sep 19, 2024
* merge with main

* added check for presence of addDefaultCallbackUrl in globalAppMiddleware

* Update src/runtime/composables/local/useAuth.ts

* Update useAuth.ts

---------

Co-authored-by: Zoey <zoeykaiser8@gmail.com>
Co-authored-by: Marsel Shayhin <18054980+phoenix-ru@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement An improvement that needs to be added pending An issue waiting for triage
Projects
None yet
Development

No branches or pull requests

2 participants