ashansurkar / nuxt3-auth

10 stars 2 forks source link

H3Error: document is not defined #1

Open rashidpathiyil opened 2 years ago

rashidpathiyil commented 2 years ago

After following your guide, i am getting this error [h3] [unhandled] H3Error: document is not defined at createError (file:///D:/VisualOut/Projects/Lyrics%20App/lyrics-app-nuxt/node_modules/h3/dist/index.mjs:196:15) at Server.nodeHandler (file:///D:/VisualOut/Projects/Lyrics%20App/lyrics-app-nuxt/node_modules/h3/dist/index.mjs:386:21) { statusCode: 500, fatal: false, unhandled: true,

Robspin commented 2 years ago

Correct it doesn't to work because, it tries to run the code server side and there is no document object server side.

Would like some clarification from @Ashishansurkar

Did the code from the guide even function I'm wondering...

amehat commented 2 years ago

I have the same problem, did you find a solution?

erikknorren commented 2 years ago

I had the same issue as @amehat and @rashidpathiyil After some troubleshooting I found a way to implement Auth0 plugin and use it in Nuxt3 middleware. Create a file called auth0.ts in plugins and add the following code. Replace your domain and clientid.

import { createAuth0Client } from '@auth0/auth0-spa-js'

export default defineNuxtPlugin(async (nuxtApp) => {
  const auth0 = await createAuth0Client({
    domain: 'YOUR_DOMAIN',
    clientId: 'YOUR CLIENT ID',
    useRefreshTokens: true,
  })
  return {
    provide: {
      auth0: auth0,
    },
  }
})

Subsequently, in your middleware/auth.ts for example you can then use this

export default defineNuxtRouteMiddleware(async (to, from) => {
  const { $auth0 } = useNuxtApp()
  $auth0.loginWithRedirect()
})