Comments (4)
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.
@awaisalwaisy Have a nice solution for this incoming.
from kinde.
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.
@awaisalwaisy Have a nice solution for this incoming.
I just visited the reference PR. Better solution to protect routes.
from kinde.
Related Issues (20)
- [500] Authentication flow state not found in Safari HOT 1
- Error 500 when logging in: user must be a string. HOT 5
- Can I use this module by logging in with my username and password? I didn't find any relevant documentation for this
- Protect Nuxt (Nitro) API routes HOT 2
- `useRuntimeConfig is not defined` HOT 4
- Error: Could not load HOT 3
- The package doesn't work with disabled SSR HOT 3
- useKindeClient incorrect type
- api callback in nuxt HOT 1
- useAuth in pinia store HOT 2
- Huge amount of rewriting during build HOT 2
- NUXT_KINDE_LOGOUT_REDIRECT_URL doesn't seem to be taken into account HOT 1
- Are the internal routes configurable
- Internal Server Error with /api/login and /api/register Links in Production HOT 1
- Setting an audience for authentication tokens HOT 1
- useKindeClient can't import on server side HOT 2
- [ERROR] [nuxt] [request error] [unhandled] [500] Invalid URL string. HOT 1
- User information available on server HOT 1
- getUser is blowing up in server handler HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from kinde.