TransWikia.com

nuxt: pass data from server plugin to client

Stack Overflow Asked on December 27, 2021

I have a nuxt behind an auth proxy. A request will get to nuxt (only if) it is authorized, in which case the X-auth-username (and other) headers will be set.

I have found that, using a server plugin, I can read this info on the request. However, the data is not send from the server to the client. How do I get the server to send information headers (in particular the user name) to the client?

My plugin so far:


import { Plugin } from '@nuxt/types'
import { IncomingHttpHeaders } from 'http'

declare module '@nuxt/types' {
  interface NuxtAppOptions {
    $headers: IncomingHttpHeaders
  }
}

const getAuth: Plugin = (context, inject) => {
  const headers = context.req.headers
  inject('headers', headers)
}

export default getAuth

NOTE I am using the nuxt-composition-api, and not vuex.

One Answer

Injected header should be available as $headers in templates or lifecycle methods of components as this.$headers.

Answered by Oluwafemi Sule on December 27, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP