N
Nuxtβ€’3d 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
Koviβ€’3d 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
Koviβ€’3d 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 })
}

})