nuxt/example-auth0

Context.isServer has been deprecated

klaveren opened this issue ยท 5 comments

I have get this error when I try access auth routes:

on app: Invalid token specified
An error occurred while rendering the page. Check developer tools console for details.

on terminal:
nuxt:render Rendering url /auth/signed-in +0ms
context.isServer has been deprecated, please use process.server instead.
nuxt:render Data fetching /auth/signed-in: 30ms +0ms
nuxt:render Rendering url / +10s
context.isServer has been deprecated, please use process.server instead.
nuxt:render Data fetching /: 1ms +9s
nuxt:render Rendering url /auth/signed-in +19s
context.isServer has been deprecated, please use process.server instead.
nuxt:render Data fetching /auth/signed-in: 1ms +19s

Someone have any idea?
Thanks.

This question is available on Nuxt.js community (#c18)

I have solved this by replacing isServer by process.server in ./middelware/check-auth.js

Below is the code, how I have rewritten it

import { getUserFromCookie, getUserFromLocalStorage } from '~/utils/auth'

export default function ({ store, req }) {
   // If nuxt generate, pass this middleware
  if (process.server && !req) return
  const loggedUser = process.server ? getUserFromCookie(req) : getUserFromLocalStorage()
  store.commit('SET_USER', loggedUser)
}

@dbcbos Thanks, Works fine.

I think that this is more correct

import { getUserFromCookie, getUserFromLocalStorage } from '~/utils/auth'

export default function ({ isServer, store, req }) {
  if (process.client) {
    const loggedUser = isServer ? getUserFromCookie(req) : getUserFromLocalStorage()
    store.commit('SET_USER', loggedUser)
  }
  
}

with "process.client" the code only execute in the client side.

@daniheras That is less correct. You want that code to execute on the server. You don't want it to execute specifically on the server and when request is not set. Which is specifically when you are running nuxt generate. That way you can pre-populate your vuex store with SET_USER before the response is sent to you.

One thing that the solution to this issue misses is how to handle testing. When you rely on process.server you cannot test switching logic on that if you pass things through a webpack config that uses the DefinePlugin for in lining environmental variables. On our end we work around this by adding a middlware at the top of our chain that injects isServer. Then when we are able to use DI to set that as needed for testing anything that relies on nuxtContext.