setRole doesn't work

I'm using NextJS@14. Hello, I'm trying to give a user the admin role, but it's not working and I am not getting an error message/code. here is my code (a server action):
export async function setAdmin(id: string) {
const updatedUser = await authClient.admin.setRole(
{
userId: id,
role: "admin",
},
{ headers: headers() }
)

console.log("User is now an admin", updatedUser, id)
}
export async function setAdmin(id: string) {
const updatedUser = await authClient.admin.setRole(
{
userId: id,
role: "admin",
},
{ headers: headers() }
)

console.log("User is now an admin", updatedUser, id)
}
The response i get:
User is now an admin { data: null, error: { status: 0, statusText: '' } } ZFgfmRxHcP8Exszk07b9CZOsteXMiQ62
User is now an admin { data: null, error: { status: 0, statusText: '' } } ZFgfmRxHcP8Exszk07b9CZOsteXMiQ62
If you have an Idea of what went wrong please let me know. Any help is appreciated 🙌
1 Reply
janusanus
janusanusOP•2w ago
I fixed it, after looking at some other posts I forgot i had to use the auth.api instead of the authClient, because it's a server action. here is the working code:
export async function setAdmin(id: string) {
const updatedUser = await auth.api.setRole({
body: {
userId: id,
role: "admin"
},
headers: headers()
})

console.log("User is now an admin", updatedUser, id)
}
export async function setAdmin(id: string) {
const updatedUser = await auth.api.setRole({
body: {
userId: id,
role: "admin"
},
headers: headers()
})

console.log("User is now an admin", updatedUser, id)
}

Did you find this page helpful?