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 URL/route for un-authenticated users #45

Open
alwaisy opened this issue Jan 15, 2024 · 5 comments
Open

Redirect URL/route for un-authenticated users #45

alwaisy opened this issue Jan 15, 2024 · 5 comments
Labels
help wanted Extra attention is needed question Further information is requested

Comments

@alwaisy
Copy link

alwaisy commented Jan 15, 2024

I am having some difficulty handling unauthenticated users. I attempted to use another global middleware, but encountered an infinite redirect error. Is there a redirect URL available for unauthenticated users similar to Clerk auth?

@maige-app maige-app bot added help wanted Extra attention is needed question Further information is requested labels Jan 15, 2024
@DanielRivers
Copy link
Collaborator

Hi @awaisalwaisy,

Currently there is no built in way to redirect users to a specific URL when they are unautnticated accessing routes that you want to be protected.

This can however be achieved by a simple custom middleware.

If you add a file into the ./middleware folder with the following content. I called this protected.ts

export default defineNuxtRouteMiddleware(() => {
    if (!useNuxtApp().$auth.loggedIn) {
        return navigateTo('/') // Update this URL to the where you want to redirect the user too.
    }
})  

Then at the top of your <script> tag on the page you wish to protect add the following

definePageMeta({
  middleware: ["protected"],
});

Let me know if this helps.

@alwaisy
Copy link
Author

alwaisy commented Jan 21, 2024

Thanks for replying...

I have implemented the following code by copying and modifying the middleware from the "kinde auth" module. It successfully worked for my needs.

Here is the code snippet from my global middleware file,

import { defineNuxtRouteMiddleware, useNuxtApp } from "#imports";

export default defineNuxtRouteMiddleware(() => {
  if (!useNuxtApp().$auth.loggedIn) {
    if (import.meta.server) {
      return navigateTo(process.env.NUXT_CLIENT_URL + "/api/login", {
        external: true,
      });
    } else {
      console.log("Not logged in...");
    }
    // return abortNavigation();
  }
});

Although this solution works, I believe there could be a better built-in way to handle this situation.

@DanielRivers
Copy link
Collaborator

@awaisalwaisy Have a nice solution for this incoming.

@alwaisy
Copy link
Author

alwaisy commented Mar 21, 2024

@awaisalwaisy Have a nice solution for this incoming.

I just visited the reference PR. Better solution to protect routes.

@DanielRivers
Copy link
Collaborator

I hope this will be merged very soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants