sidebase / nuxt-auth

Authentication built for Nuxt 3! Easily add authentication via OAuth providers, credentials or Email Magic URLs!
https://auth.sidebase.io
MIT License
1.25k stars 162 forks source link

Cannot access 'renderer$1' before initialization #420

Closed nikkorejz closed 11 months ago

nikkorejz commented 1 year ago

Environment

Reproduction

No response

Describe the bug

I want to add base cookies auth to my nuxt3 project

  1. Install nuxt-auth yarn add @sidebase/nuxt-auth

  2. Manually append to package.json in dependencies section "next-auth": "^4.18.8",

  3. Add to nuxt.config.ts

    
    ...
    modules: ['@sidebase/nuxt-auth'],

auth: { provider: { type: 'local', }, }, ...


4. Copy content of `server/api/auth/[...].ts` from https://sidebase.io/nuxt-auth/v0.6/recipes/nuxt-auth-example to `server/api/auth/[...].ts`

5. `yarn dev`

6. Get a error

### Additional context

_No response_

### Logs

```shell
✔ Nitro built in 400 ms                                                                                                                                                                         nitro 15:09:25
[nitro] [dev] [uncaughtException] Error: RuntimeError: Type must match at this point                                                                                                                  15:09:25
    at useTypedBackendConfig (file:///home/nikita/Documents/Domains/istp-stagestable/.nuxt/dev/index.mjs:668:9)
    at useConfig (file:///home/nikita/Documents/Domains/istp-stagestable/.nuxt/dev/index.mjs:712:25)
    at NuxtAuthHandler (file:///home/nikita/Documents/Domains/istp-stagestable/.nuxt/dev/index.mjs:744:16)                                                                                            15:09:21
    at file:///home/nikita/Documents/Domains/istp-stagestable/.nuxt/dev/index.mjs:808:15
    at ModuleJob.run (node:internal/modules/esm/module_job:194:25)
[nuxt] [request error] [unhandled] [500] Cannot access '_____$1' before initialization                                                                                                                15:09:33
  at ./.nuxt/dev/index.mjs:519:65  
  at process.processTicksAndRejections (node:internal/process/task_queues:95:5)  
  at async Object.handler (./node_modules/h3/dist/index.mjs:1247:19)  
  at async Server.toNodeHandle (./node_modules/h3/dist/index.mjs:1322:7)
[nuxt] [request error] [unhandled] [500] Cannot access 'renderer$1' before initialization                                                                                                             15:09:38
  at ./.nuxt/dev/index.mjs:523:65  
  at process.processTicksAndRejections (node:internal/process/task_queues:95:5)  
  at async Object.handler (./node_modules/h3/dist/index.mjs:1247:19)  
  at async Server.toNodeHandle (./node_modules/h3/dist/index.mjs:1322:7)
LucasLeandroBR commented 1 year ago

same issue.

stylessh commented 1 year ago

Same issue here, have no idea what it might be

Son-Isaki commented 1 year ago

Same issue here with version 0.6.0-beta.3 and local provider only and following log :

[nuxt] [request error] [unhandled] [500] Cannot access 'renderer$1' before initialization                                                                                                       23:29:40
  at /S:/Projects/Web/naruto-leaf/frontend/.nuxt/dev/index.mjs:713:65  
  at process.processTicksAndRejections (node:internal/process/task_queues:95:5)  
  at async Object.handler (/S:/Projects/Web/naruto-leaf/frontend/node_modules/h3/dist/index.mjs:1284:19)  
  at async toNodeHandle (/S:/Projects/Web/naruto-leaf/frontend/node_modules/h3/dist/index.mjs:1359:7)  
  at async Object.ufetch [as localFetch] (/S:/Projects/Web/naruto-leaf/frontend/node_modules/unenv/runtime/fetch/index.mjs:9:17)  
  at async Object.errorhandler [as onError] (/S:/Projects/Web/naruto-leaf/frontend/.nuxt/dev/index.mjs:685:30)  
  at async Server.toNodeHandle (/S:/Projects/Web/naruto-leaf/frontend/node_modules/h3/dist/index.mjs:1366:9)
Eternal-Grace commented 1 year ago

I've had a similar issue. And had to take it unto myself to find the error, and fix it. So yeah! I think I've fixed the issue now. Here is the xxx.patch file code to use. Tell me if it takes care of your problem.

From 7e5c40a3171bc7b010dbda434d242aae16710cf4 Mon Sep 17 00:00:00 2001
From: GJM <contact@genitus.tech>
Date: Thu, 20 Jul 2023 16:59:06 +0200
Subject: [PATCH] fix: Cannot access 'renderer' before initialization. (#420)

---
 .../dist/runtime/server/services/authjs/nuxtAuthHandler.mjs     | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/node_modules/@sidebase/nuxt-auth/dist/runtime/server/services/authjs/nuxtAuthHandler.mjs b/node_modules/@sidebase/nuxt-auth/dist/runtime/server/services/authjs/nuxtAuthHandler.mjs
index 09e2bf9..a150949 100644
--- a/node_modules/@sidebase/nuxt-auth/dist/runtime/server/services/authjs/nuxtAuthHandler.mjs
+++ b/node_modules/@sidebase/nuxt-auth/dist/runtime/server/services/authjs/nuxtAuthHandler.mjs
@@ -11,7 +11,7 @@ import { useRuntimeConfig } from "#imports";
 let preparedAuthHandler;
 let usedSecret;
 const SUPPORTED_ACTIONS = ["providers", "session", "csrf", "signin", "signout", "callback", "verify-request", "error", "_log"];
-const useConfig = () => useTypedBackendConfig(useRuntimeConfig(), "authjs");
+const useConfig = () => useTypedBackendConfig(useRuntimeConfig(), useRuntimeConfig().public.auth.provider.type ?? 'authjs');
 const readBodyForNext = async (event) => {
   let body;
   if (isMethod(event, "PATCH") || isMethod(event, "POST") || isMethod(event, "PUT") || isMethod(event, "DELETE")) {
--
2.34.1
mfreeman451 commented 1 year ago

happening to me as well, using the Github provider

Eternal-Grace commented 1 year ago

@mfreeman451 Did applying the patch fix your issues?

pedroresende commented 1 year ago

any news about this ?

Eternal-Grace commented 1 year ago

any news about this ?

The topic got moved to https://github.com/sidebase/nuxt-auth/pull/490

No updates came from @zoey-kaiser after we last spoke. And it seems tyepescript restrictions prevent her from fixing the issue with minimal effort.

I've tried rectifying the problem on my own, but it requires an important restructuring of a few typed interfaces.

My suggestion, since this issue is taking way more time than needed to be fixed, is to patch it on your own. If you are on github, and know how to (or ask someone with better experience). It's a very easy fix, if you ignore auth.provider.type = 'authjs' and any potential future updates.

zoey-kaiser commented 11 months ago

Hi everyone! I had some more time to look into this and I feel like we may be dealing with three separate issues here!

1.) The original issue posted by @nikkorejz, had the issue that it was using the local provider, but still created a NuxtAuthHandler, when the local provider does not need, even is incompatible with it. In order to use the setup with the NuxtAuthHandler, the authjs provider has to be selected.

2.) @Son-Isaki, you had another completely different error (Cannot access 'renderer$1' before initialization) and your error log, does not reference any code used by NuxtAuth. If you are still facing this issue, please open a new issue to separate it from the other issues in here!

3.) @Eternal-Grace, we looked into your PR before, but after some further investigation, your patch in theory should not work. This is due to you, allowing the local provider to also be compatible with the NuxtAuthHandler, by removing the hard coded authjs value in the type declaration. However, if the local provider is selected, this file would never be used/accessed, as it is purely for instances running authjs. Could you please open a new issue with the error you were getting and then outline how your fix, resolved it.

I will be closing this issue, as it became a bit messy. I outlined the three separate issues, that were combined in this one. Please open new issues for your problems and then we can discuss them there! I hope this will allow us to make some progress on your problems! 🥳