In Nuxt Auth plugin, how to pass localstorage prefix parameter ? Prefix must be window.location.hostname

155 Views Asked by At

In nuxt.config.js

  auth: {
    localStorage: {
      prefix: '[' + window.location.hostname + ']'
    },

In output:

 FATAL  window is not defined                                                                               10:33:30

  at nuxt.config.js:237:21
  at jiti (node_modules/jiti/dist/jiti.js:1:196506)
  at Object.loadNuxtConfig (node_modules/@nuxt/config/dist/config.js:1082:15)
  at loadNuxtConfig (node_modules/@nuxt/cli/dist/cli-index.js:338:32)
  at NuxtCommand.getNuxtConfig (node_modules/@nuxt/cli/dist/cli-index.js:463:26)
  at Object._listenDev (node_modules/@nuxt/cli/dist/cli-dev.js:75:30)
  at Object.startDev (node_modules/@nuxt/cli/dist/cli-dev.js:58:25)
  at Object.run (node_modules/@nuxt/cli/dist/cli-dev.js:52:16)
  at NuxtCommand.run (node_modules/@nuxt/cli/dist/cli-index.js:413:22)


   ╭───────────────────────────────────────────╮
   │                                           │
   │   ✖ Nuxt Fatal Error                      │
   │                                           │
   │   ReferenceError: window is not defined   │
   │                                           │

Prefix must be window.location.hostname:

In localstorage:

auth._token.local : Bearer 707d7bfd03e63694c3b3b6a5fe3eabc1

Must be:

[mysite.local]auth._token.local : Bearer 707d7bfd03e63694c3b3b6a5fe3eabc1

1

There are 1 best solutions below

0
On

window is undefined for pretty much the same reason as explained here.
TLDR: window only exist in the browser, not in a Node.js environment. Nuxt is running both, hence the error.

You could use this approach for Node. It might work with only the Node version because the Nuxt file is mainly used during the build step.
If it doesn't work in the browser because it is using a Node API, you can always have a generic env variable for that specific case.