Nuxt/docs/3.api/2.composables/use-request-header.md
2024-02-21 18:09:45 +01:00

1.0 KiB

title description links
useRequestHeader Use useRequestHeader to access a certain incoming request header.
label icon to size
Source i-simple-icons-github https://github.com/nuxt/nuxt/blob/main/packages/nuxt/src/app/composables/ssr.ts xs

You can use the built-in useRequestHeader composable to access any incoming request header within your pages, components, and plugins.

// Get the authorization request header
const authorization = useRequestHeader('authorization')

::tip In the browser, useRequestHeader will return undefined. ::

Example

We can use useRequestHeader to easily figure out if a user is authorized or not.

The example below reads the authorization request header to find out if a person can access a restricted resource.

export default defineNuxtRouteMiddleware((to, from) => {
  if (!useRequestHeader('authorization')) {
    return navigateTo('/not-authorized')
  }
})