How to use adminRoles?

I tried to use custom adminRoles like this
import { prismaAdapter } from "better-auth/adapters/prisma"
import { PrismaClient } from "@prisma/client"
import { username, admin } from "better-auth/plugins"
import { betterAuth } from "better-auth"
import bcrypt from "bcrypt"

const prisma = new PrismaClient()

export const auth = betterAuth({
database: prismaAdapter(prisma, {
provider: "postgresql",
}),
appName: "nuxt-app",
plugins: [
admin({
adminRoles: ["admin", "owner"],
// adminUserIds: ["hOeyOhG7mCwsIUbtI05yHcLSJBId6ckM"],
}),
username(),
],
socialProviders: {
google: {
clientId: process.env.GOOGLE_CLIENT_ID as string,
clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,
prompt: "select_account",
},
},
emailAndPassword: {
enabled: true,
password: {
hash: async (password: string): Promise<string> => {
const saltRounds = 12
return await bcrypt.hash(password, saltRounds)
},
verify: async (data: {
password: string
hash: string
}): Promise<boolean> => {
const { password, hash } = data
return await bcrypt.compare(password, hash)
},
},
},
})
import { prismaAdapter } from "better-auth/adapters/prisma"
import { PrismaClient } from "@prisma/client"
import { username, admin } from "better-auth/plugins"
import { betterAuth } from "better-auth"
import bcrypt from "bcrypt"

const prisma = new PrismaClient()

export const auth = betterAuth({
database: prismaAdapter(prisma, {
provider: "postgresql",
}),
appName: "nuxt-app",
plugins: [
admin({
adminRoles: ["admin", "owner"],
// adminUserIds: ["hOeyOhG7mCwsIUbtI05yHcLSJBId6ckM"],
}),
username(),
],
socialProviders: {
google: {
clientId: process.env.GOOGLE_CLIENT_ID as string,
clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,
prompt: "select_account",
},
},
emailAndPassword: {
enabled: true,
password: {
hash: async (password: string): Promise<string> => {
const saltRounds = 12
return await bcrypt.hash(password, saltRounds)
},
verify: async (data: {
password: string
hash: string
}): Promise<boolean> => {
const { password, hash } = data
return await bcrypt.compare(password, hash)
},
},
},
})
But when I set my role to admin in my database. better-auth don't think I'm admin. Did I do anything wrong?
4 Replies
星野みずき
星野みずきOP2w ago
Also want to ask about custom roles. I tried to use set-role. But that only allow admin and user
Ping
Ping2w ago
What endpoints are you hitting which says you're not an admin? Can you show me that code?
星野みずき
星野みずきOP2w ago
I got 403 when my role is owner
Ping
Ping2w ago
yeah I would like to see that code the code of you using authClient or auth.api?

Did you find this page helpful?