listUsers returns status 401 UNAUTHORIZED even though the current user is an admin.
I am using the
admin()
and adminClient()
plugins while testing out better-auth. I am trying to list out the users using the auth client, but I am getting a 401 unauthorized. I've registered a user and manually changed the role to admin
in the database. I am using nextjs15
Here's the code:
Here are the console logs:
What am I missing? 😅5 Replies
Okay, so I realized I was using the
authClient
on the server side. I've changed it since to use the server side auth. However, the issue persists. This is what I changed it to:
I added the manual auth check, thinking that it might be the culprit. The session logs correctly and the role is still set to admin
, however I get the same error as before
It turns out you need to pass in the headers the same way you would with any other auth form, so this is the final solution to the conundrum
Unknown User•4w ago
Message Not Public
Sign In & Join Server To View
@belikebee Have you seen my latest message? It turns out you just need to pass in the
headers: await headers()
to the object because it is a server function and can't know if there's a session on the request time without including the headers.Unknown User•4w ago
Message Not Public
Sign In & Join Server To View
@invocation97 Thankyou soo much 🙏 Was facing the same issue. They should include this in the docs