Context.isServer has been deprecated

klaveren
0
klaveren
commented a year ago

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.

0
dbcbos
15
dbcbos
commented a year ago

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)
}
2
klaveren
0
klaveren
commented a year ago

@dbcbos Thanks, Works fine.

0
daniheras
0
daniheras
commented 5 months ago

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.

0
web3devin
0
web3devin
commented 2 months ago

@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.

0
Informations
Question โ€ข Unresolved
#c18 - Created a year ago