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.33k stars 164 forks source link

Inside NuxtAuthHandler we cannot use useRequestEvent and useRequestHeader #907

Open agracia-foticos opened 2 months ago

agracia-foticos commented 2 months ago

Environment

Reproduction

we cannot reproduction

Describe the bug

We are trying to use useRequestEvent and useRequestHeaders to access the request cookies but they are not available.

Also we try to access $fetch('/api/session', {method:'GET'}) (this is an access to H3 session) but the cookies not are sending.

There is any option to send cookies to NuxtAuthHandler?

Additional context

No response

Logs

No response

phoenix-ru commented 2 months ago

Could you please elaborate a bit? What are you trying to achieve? Some code examples would be nice to understand the problem, thanks

agracia-foticos commented 2 months ago

We manage a session with https://h3.unjs.io/examples/handle-session outside nuxt-auth.

This session its bassed on cookies, this session contains a field called createdAt, this value we must checked in JWT session, because its expiration session of auth.

This field can be changed, this is the reason because we want read it in JWT function, but NuxtAuthHandler doesnt have access to :

phoenix-ru commented 2 months ago

Can you provide at least a minimal reproduction? I have hard time following your usecase. You can use CredentialsProvider with hardcoded credentials similar to playground-authjs: https://github.com/sidebase/nuxt-auth/blob/main/playground-authjs/server/api/auth/%5B...%5D.ts

You can base it off playground-authjs to save some time or from one of the templates below (from bot comment).

NuxtAuthHandler normally works according to Authjs flow: https://authjs.dev/getting-started/installation#configure

github-actions[bot] commented 2 months ago

Hello 👋

Please provide a reproduction for this issue 🙏

How can I create a reproduction? Please use one of the following links to reproduce your issue. - https://stackblitz.com/github/nuxt/starter/tree/v3-stackblitz - https://codesandbox.io/s/github/nuxt/starter/v3-codesandbox Please ensure that the reproduction is as **minimal** as possible. This will allow us to isolate the issue as best as possible. Here are some more amazing posts about the importance of reproductions: - [The Importance of Reproductions](https://antfu.me/posts/why-reproductions-are-required) - [How to Generate a Minimal, Complete, and Verifiable Example](https://stackoverflow.com/help/minimal-reproducible-example)
agracia-foticos commented 2 months ago

https://stackblitz.com/edit/nuxt-starter-embl4u @phoenix-ru here you are a reproduction, but we cant test it because stackblitz are very slow compiling

image

you can see in plugins/01.session.ts we initialize the H3 cookie session.

In server/routes/api/session.get.ts we retreive this H3 cookie session.

In server/api/auth/[...].ts in JWT validation, we want to check this H3 cookie session, but returns blank, not the session created in plugin, because in NuxtAuthHandler seems that cookies arent accesible

phoenix-ru commented 2 months ago

Tried to check the reproduction but you're right - Stackblitz is slow - or support of Stackblitz in Nuxt is broken. Unfortunately, need to de-prioritize in favor of #883

agracia-foticos commented 2 months ago

@phoenix-ru don't worry, prioritize #883, We can advance our development on the other hand, this issue is not a priority. In a few days I will contact you again