Coder Social home page Coder Social logo

Comments (4)

DanielRivers avatar DanielRivers commented on August 19, 2024 1

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.

from kinde.

DanielRivers avatar DanielRivers commented on August 19, 2024 1

@awaisalwaisy Have a nice solution for this incoming.

from kinde.

alwaisy avatar alwaisy commented on August 19, 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.

from kinde.

alwaisy avatar alwaisy commented on August 19, 2024

@awaisalwaisy Have a nice solution for this incoming.

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

from kinde.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.