N
Nuxt5mo ago
Kovi

client side navigation to a server side page will run the authorization middleware in the browser

hello all, I'm facing a problem with forcing SSR for a page all the time. From the documentation I found to achive this I should use the '.server.vue' prependix in the page's file name, which works like a charm, but the middleware that checks authorization is running in the browser only, I have this line in the middleware: 'if (import.meta.client) return ', so I can reach the site even tough the user I'm logged in doesn't have the rights to see it. Could you help me how can I configure the middleware to run on the serverside in this case?
2 Replies
Kovi
KoviOP5mo ago
I'll provide some more info for my question might help to resolve it 🙂 On the picture you can see my folder structure
No description
Kovi
KoviOP5mo ago
the auth,global.ts looks like this (there are some dummy variables)
import { useAuthStore } from "@/stores/auth"
export default defineNuxtRouteMiddleware((to, from) => {
const authStore = useAuthStore()

var role = 'user'
var hasSubscription = true

if (to.path.startsWith('/admin') && !(authStore.isAuthenticated && role === 'admin')) {
return abortNavigation({ statusCode: 401, message: 'Unauthorized', fatal: true })
}

if (to.path.startsWith('/tco') && !(authStore.isAuthenticated && hasSubscription)) {
return abortNavigation({ statusCode: 401, message: 'Unauthorized', fatal: true })
}

})
import { useAuthStore } from "@/stores/auth"
export default defineNuxtRouteMiddleware((to, from) => {
const authStore = useAuthStore()

var role = 'user'
var hasSubscription = true

if (to.path.startsWith('/admin') && !(authStore.isAuthenticated && role === 'admin')) {
return abortNavigation({ statusCode: 401, message: 'Unauthorized', fatal: true })
}

if (to.path.startsWith('/tco') && !(authStore.isAuthenticated && hasSubscription)) {
return abortNavigation({ statusCode: 401, message: 'Unauthorized', fatal: true })
}

})
I made a more detailed description of the issue, but I was unable to close/delete this one: https://discord.com/channels/473401852243869706/1258398982347034757
Want results from more Discord servers?
Add your server