Closed rudokemper closed 2 months ago
The issue was that I had middleware applied to the api/
directory, which was interfering with the route used by Nuxt Auth Utils api/_auth/session
. Filing a PR to expose the usage of this route in the readme, which would have saved me (and possibly others) some time.
Hey 👋,
What was doing the middleware that was interfering with your route? I think that the usage of global middleware (within Nitro) should be clarified because their are using a lot of trouble.
Hi, yes that makes sense. I really didn't expect my middleware to be impacting the functionality of this module.
My middleware is forbidding access to API routes unless an API key is included in the header - I use this to protect my data API routes. And you can see how I solved my problem by adding a bypass for the auth-nuxt-utils route.
import { API_KEY } from "../../config";
export default defineEventHandler((event: H3Event) => {
// Only apply middleware to API routes
if (!event.node.req.url?.startsWith("/api/")) {
return;
}
// Bypass middleware for auth-nuxt-utils
if (
event.node.req.url?.startsWith("/api/_auth/")
) {
return;
}
const apiKey = event.node.req.headers["x-api-key"];
if (apiKey !== API_KEY) {
event.node.res.statusCode = 403;
event.node.res.end("Forbidden");
return;
}
});
Yeah, I totally understand the issue.
You can create a utils from this middleware and apply it only to want event handler by leveraging the object syntax or as a simple function like the requireUserSession
utils.
And instead of using
if (apiKey !== API_KEY) {
event.node.res.statusCode = 403;
event.node.res.end("Forbidden");
return;
}
You could simply throw an error using createError
utility.
if (apiKey !== API_KEY) {
throw createError({
status: 403,
message: 'Forbidden'
})
}
:+1: thanks for these helpful tips!
I am having an issue that others seem to have encountered before (cf. this Discord thread or perhaps this issue), but have not been able to figure out a solution:
Sessions are not being set after successfully authenticating, and
loggedIn
remains false across the application. This is regardless of the auth provider (I have tried auth0 and Github and am experiencing the same issue).On the client side, the flow is as follows: Upon accessing the login API route, I am redirected to the auth provider, able to authenticate, and then redirected back after successfully logging in. The API route then does show that a session being created successfully, through
getUserSession
, but there is no session stored elsewhere in cookies or local storage, andloggedIn
stays false, so it is as if I never logged in.Per the documentation, this is my API route:
I have tried to follow the example in atidone as closely as possible, with similar redirect middleware, but I seem to be missing something, and I am not sure why; it could be because I am not using
@nuxthub/core
or Nuxt 4, but I haven't tested that.Here is my login page:
Upon authentication, I am supposed to be returned back to
/
, but my middleware automatically redirects me here sinceloggedIn
is false across the app:Lastly, just to clarify, I am using SSR, so that's not the problem. There is nothing else about my Nuxt use case that would seem to interfere with the auth flow. I am using nuxt 3.12.2 and nuxt-auth-utils 0.3.6.
Any ideas?
Since others seem to have had this issue, I would love to contribute to this project by documenting a solution for when this issue is encountered.