auth.api.listUsers returns UNAUTHORIZED but I am admin

Hello, I have written this function
export const getUserList = async () => {
return await auth.api.listUsers({
query: {
limit: 10,
},
});
};
export const getUserList = async () => {
return await auth.api.listUsers({
query: {
limit: 10,
},
});
};
But the API returns ⨯ [Error [BetterCallAPIError]: API Error: UNAUTHORIZED ] { status: 'UNAUTHORIZED', headers: Headers {}, body: [Object], digest: '3161027971' } When I use auth.api.getSession I see that my account has role: 'admin'. What is the Problem?
2 Replies
Matt
Matt4mo ago
pass your headers
export const getUserList = async () => {
return await auth.api.listUsers({
query: {
limit: 10,
},
headers: await headers(),
});
};
export const getUserList = async () => {
return await auth.api.listUsers({
query: {
limit: 10,
},
headers: await headers(),
});
};
I'd actually recommend adding react cache for now until that's default in the package
import { cache } from "react";
import { headers } from "next/headers";

export const getUserList = cache(async () => {
return await auth.api.listUsers({
query: {
limit: 10,
},
headers: await headers(),
});
});
import { cache } from "react";
import { headers } from "next/headers";

export const getUserList = cache(async () => {
return await auth.api.listUsers({
query: {
limit: 10,
},
headers: await headers(),
});
});
Nick/Roal
Nick/RoalOP4mo ago
Thank you. The docs only mentioned the headers with the getSession api.

Did you find this page helpful?