nuxt / example-auth0

A simple example that shows how to use Nuxt.js with Auth0.
https://auth0.nuxtjs.org
MIT License
722 stars 159 forks source link

Context.isServer has been deprecated #30

Closed klaveren closed 4 years ago

klaveren commented 6 years 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.

This question is available on Nuxt.js community (#c18)
dbcbos commented 6 years 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)
}
klaveren commented 6 years ago

@dbcbos Thanks, Works fine.

daniheras commented 5 years 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.

web3devin commented 5 years 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.

Austio commented 4 years ago

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.