Admin Plugin: API Error Unauthorized

When calling auth.api.listUsers on the server. I'm getting an API error:
Failed to fetch users: [Error [BetterCallAPIError]: API Error: UNAUTHORIZED ] {
status: 'UNAUTHORIZED',
headers: Headers {},
body: [Object]
}
GET /admin 200 in 621ms
GET /admin 404 in 700ms
Failed to fetch users: [Error [BetterCallAPIError]: API Error: UNAUTHORIZED ] {
status: 'UNAUTHORIZED',
headers: Headers {},
body: [Object]
}
GET /admin 200 in 621ms
GET /admin 404 in 700ms
oddly enough if i just fetch the api route directly:
await betterFetch("/api/auth/admin/list-users")
await betterFetch("/api/auth/admin/list-users")
This works just fine. Granted I'm populating the Cookies on the headers explicitly. But to the best of my knowledge that isn't the issue the cookies seem to be getting set properly in either case, because I'm able to make every other type of request. Just not sure if its my config or something else. Here's a link to the branch on my repo if anyone wants to check this out for me: https://github.com/innovations-dev/boiler15/blob/wip-admin/lib/auth.ts Happy to answer any additional questions or provide other context if needed. Thanks in advance.
GitHub
boiler15/lib/auth.ts at wip-admin · innovations-dev/boiler15
Contribute to innovations-dev/boiler15 development by creating an account on GitHub.
4 Replies
bekacru
bekacru2mo ago
are you passing headers
await auth.api.listUsers({
headers: //pass headers
})
await auth.api.listUsers({
headers: //pass headers
})
Gaurang
GaurangOP2mo ago
I tried to add cookies bit the header object for listUsers doesn't take a Cookie or cookie property -- i tried both.
bekacru
bekacru2mo ago
you need to pass the whole header object not cookies
await auth.api.listUsers({
headers: new Headers({
cookie: ""
})
})
await auth.api.listUsers({
headers: new Headers({
cookie: ""
})
})
Gaurang
GaurangOP2mo ago
That makes more sense let me try i'll get back to you. Thank you for clarifying that That did the trick! You're a life saver! TYSM

Did you find this page helpful?