Client Setup
By default, the client composables use the global $fetch instance. You can replace this with a custom instance to inject headers such as Authorization into every request made by the ZenStack composables.
Provide a custom fetch instance
Create a Nuxt plugin that calls provideZenstackFetch with your custom $fetch instance. This plugin runs before any composable tries to make a network request.
ts
// plugins/zenstack.ts
export default defineNuxtPlugin(() => {
const myFetch = $fetch.create({
onRequest({ options }) {
// Add your authorization token to every request
options.headers.set('Authorization', 'Bearer your-token')
},
})
provideZenstackFetch(myFetch)
})How it flows
Nuxt Plugin (provideZenstackFetch)
│
▼
useZenstackCreate / useZenstackRead / ... (composables)
│ (use provided $fetch)
▼
Server endpoint (/api/_zenstack/...)
│ (reads Authorization header)
▼
Nitro plugin (provideZenstackClient)
│ (sets auth context on the ZenStack client)
▼
ZenStack Policy Engine (enforces access rules)