Getting FORBIDDEN (403) error when using authClient.admin

Auth config:
export const auth = betterAuth({
database: prismaAdapter(prisma, { provider: "mysql" }),

user: {
additionalFields: {
document: {
type: "string",
},
},
},

plugins: [
admin({
adminRoles: [Role.ADMIN],
defaultRole: Role.AUDITOR,
}),
expo(),
],

trustedOrigins: [process.env.APP_URL ?? raise("APP_URL not set"), "locsystem://"],

emailAndPassword: { enabled: true },
});
export const auth = betterAuth({
database: prismaAdapter(prisma, { provider: "mysql" }),

user: {
additionalFields: {
document: {
type: "string",
},
},
},

plugins: [
admin({
adminRoles: [Role.ADMIN],
defaultRole: Role.AUDITOR,
}),
expo(),
],

trustedOrigins: [process.env.APP_URL ?? raise("APP_URL not set"), "locsystem://"],

emailAndPassword: { enabled: true },
});
Calling this code to change role
authClient.admin.setRole({
userId: user.id,
role: data.role,
fetchOptions: { throw: true },
})
authClient.admin.setRole({
userId: user.id,
role: data.role,
fetchOptions: { throw: true },
})
It also happens when creating a new user. The solution for now is call this functions server side but it would be nice if this works well. I'm logged as an ADMIN and the response is:
No description
14 Replies
Arthur Mendes
Arthur MendesOP3w ago
I'm using @casl to RBAC:
register: privateProcedure
.input(
z.object({
email: z.string().email(),
name: z.string(),
password: z.string(),
document: z.string(),
role: z.nativeEnum(Role).optional(),
})
)
.mutation(async ({ input, ctx }) => {
console.log(ctx.user);
ctx.throw("Você não tem permissão para cadastrar usuário").ifCannot("create", "user");

const data = await auth.api.createUser({
headers: await headers(),
body: {
email: input.email,
name: input.name,
password: input.password,
data: { document: input.document },
},
});

await prisma.user.update({ where: { id: data.user.id }, data: { role: input.role } });

await ctx.log("registerUser", { createdUserId: data.user.id });

return data;
}),
register: privateProcedure
.input(
z.object({
email: z.string().email(),
name: z.string(),
password: z.string(),
document: z.string(),
role: z.nativeEnum(Role).optional(),
})
)
.mutation(async ({ input, ctx }) => {
console.log(ctx.user);
ctx.throw("Você não tem permissão para cadastrar usuário").ifCannot("create", "user");

const data = await auth.api.createUser({
headers: await headers(),
body: {
email: input.email,
name: input.name,
password: input.password,
data: { document: input.document },
},
});

await prisma.user.update({ where: { id: data.user.id }, data: { role: input.role } });

await ctx.log("registerUser", { createdUserId: data.user.id });

return data;
}),
This is my code on TRPC, but keeps getting the same error.
Arthur Mendes
Arthur MendesOP3w ago
The response
No description
Ping
Ping3w ago
Is Role an enum?
Arthur Mendes
Arthur MendesOP3w ago
Is generated from Prisma
Arthur Mendes
Arthur MendesOP3w ago
No description
Ping
Ping3w ago
Hey @Arthur Mendes I found the issue. It's an internal bug. I'll fix it, open a PR, then send the URL here for you to track.
Arthur Mendes
Arthur MendesOP2w ago
Nice, thank you so much! @Ping I tried yarn add better-auth@latest but I doesn't fixed yet. Do i have to do something else?
Ping
Ping2w ago
It might not be in latest release yet.
Arthur Mendes
Arthur MendesOP2w ago
Sorry about my english, I'm Brazilian.
Ping
Ping2w ago
No you're fine.
Arthur Mendes
Arthur MendesOP2w ago
Ok, how can I get notified when it's released?
Ping
Ping2w ago
Keep track of this channel: #releases When we release, a new message will appear with the version number.
Arthur Mendes
Arthur MendesOP2w ago
Great, appreciate it!

Did you find this page helpful?