Auth Preset
defineAuthMiddleware is a ready-made middleware for createActionClient. You give it a function that turns the request into a user, or into null for an anonymous request. With a user, the handler receives it as ctx.user; without one, the action returns a 401 before the handler runs. In this example a request header stands in for a real session cookie.
Server action
server/actions/account.post.ts
ts
import { getHeader } from 'h3'
interface SessionUser {
id: string
name: string
plan: 'free' | 'pro'
}
export default createActionClient()
.use(defineAuthMiddleware((event): SessionUser | null => {
const session = getHeader(event, 'x-demo-session')
if (!session) return null
return { id: session, name: `User ${session}`, plan: 'pro' }
}))
.action(async ({ ctx }) => {
const user = (ctx as { user: SessionUser }).user
return {
user,
message: `Welcome back, ${user.name}`,
serverTime: new Date().toISOString(),
}
})Nitro v3
The resolver reads the header with h3's getHeader, which works on Nitro v2. On Nitro v3, use getRequestHeader from nuxt/server instead. See Server Runtimes.
Page
pages/auth.vue
vue
<script setup lang="ts">
import { ref } from 'vue'
import { account as accountAction } from '#actions'
const signedIn = ref(false)
const account = useAction(accountAction, {
headers: (): Record<string, string> => (signedIn.value ? { 'x-demo-session': 'u_42' } : {}),
})
</script>
<template>
<div>
<section>
<span class="badge">{{ signedIn ? 'Signed in' : 'Signed out' }}</span>
<button @click="signedIn = !signedIn">
{{ signedIn ? 'Sign out' : 'Sign in' }}
</button>
<button @click="account.execute(undefined as never)">
Load account
</button>
</section>
<section>
<div v-if="account.error.value" class="error">
{{ account.error.value.code }}: {{ account.error.value.message }}
</div>
<div v-if="account.data.value" class="success">
{{ account.data.value.message }} — plan: {{ account.data.value.user.plan }}
<pre>{{ JSON.stringify(account.data.value.user, null, 2) }}</pre>
</div>
</section>
</div>
</template>The markup is trimmed for readability. The full page is linked above.
How it works
- The resolver returns a
SessionUserwhen thex-demo-sessionheader is present, andnullwhen it is not. - A
nulluser rejects the call with codeUNAUTHORIZEDand status 401. The handler never runs, and the page shows the code and the message. headersis a function, so it is evaluated on every call. The header is only sent while the page is in the signed-in state.- On success the handler reads the resolved user from
ctxand returns it with a greeting and the server time. - Load the account signed out, sign in, and load it again to see both paths.