How to sign up as admin

How to add a admin, from the form in short That is my question Can't find anything on the docs Thanks in advance!
11 Replies
D3vision
D3vision9mo ago
You should create a user as normal, by logging in, then in your db you can change the role from user to admin
D3vision
D3vision9mo ago
Admin | Better Auth
Admin plugin for Better Auth
sundaram krishnan
sundaram krishnanOP9mo ago
smthg like below is not possible ? @D3vision
await authClient.signUp.email(
{
email: values.email,
password: values.password,
name: values.name,
role: "admin",
},
await authClient.signUp.email(
{
email: values.email,
password: values.password,
name: values.name,
role: "admin",
},
this doesn't work
D3vision
D3vision9mo ago
I'm unsure but you can always give it a try For my own project I just changed the role in the database
sundaram krishnan
sundaram krishnanOP9mo ago
i tried to do that can u pls share ur repo i can take it for reference
Kazz
Kazz9mo ago
Add the role field to the database schema and to the additional fields in better-auth for your case auth.ts
export const auth = betterAuth({
database: drizzleAdapter(db, { provider: 'pg' }),
user: {
additionalFields: {
role: { type: 'string', default: 'user' }
},
},
})
export const auth = betterAuth({
database: drizzleAdapter(db, { provider: 'pg' }),
user: {
additionalFields: {
role: { type: 'string', default: 'user' }
},
},
})
in auth client we add the inferAdditionalFields so that better-auth knows about the additional fields you added auth-client.ts
import { auth } from './auth'
import { createAuthClient } from 'better-auth/react'
import { inferAdditionalFields } from 'better-auth/client/plugins'
export const authClient = createAuthClient({
baseURL: env.NEXT_PUBLIC_BASE_URL,
plugins: [inferAdditionalFields<typeof auth>()],
})
import { auth } from './auth'
import { createAuthClient } from 'better-auth/react'
import { inferAdditionalFields } from 'better-auth/client/plugins'
export const authClient = createAuthClient({
baseURL: env.NEXT_PUBLIC_BASE_URL,
plugins: [inferAdditionalFields<typeof auth>()],
})
this way you will be able to add role to the authClient.signUp and authClient.updateUser functions
sundaram krishnan
sundaram krishnanOP9mo ago
Ohh I see Really thx for the help Afk right now, will try it out tmrw
Kazz
Kazz9mo ago
Alright ^, tell me if it works for you when you implement it!
sundaram krishnan
sundaram krishnanOP9mo ago
Sure, thx again the role is still not changing @Kazz in db it's default to user only
export const auth = betterAuth({
database: prismaAdapter(prisma, {
provider: "mongodb",
}),

user: {
additionalFields: {
role: { type: "string", default: "user" },
},
},

socialProviders: {
google: {
clientId: process.env.GOOGLE_CLIENT_ID as string,
clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,
},
},
plugins: [openAPI(), admin()],
emailAndPassword: {
enabled: true,
},
} satisfies BetterAuthOptions);
export const auth = betterAuth({
database: prismaAdapter(prisma, {
provider: "mongodb",
}),

user: {
additionalFields: {
role: { type: "string", default: "user" },
},
},

socialProviders: {
google: {
clientId: process.env.GOOGLE_CLIENT_ID as string,
clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,
},
},
plugins: [openAPI(), admin()],
emailAndPassword: {
enabled: true,
},
} satisfies BetterAuthOptions);
my auth.ts file
export const authClient = createAuthClient({
baseURL: process.env.BETTER_AUTH_URL,
plugins: [inferAdditionalFields<typeof auth>()],
});
export const authClient = createAuthClient({
baseURL: process.env.BETTER_AUTH_URL,
plugins: [inferAdditionalFields<typeof auth>()],
});
auth-client.ts
await authClient.signUp.email(
{
email: values.email,
password: values.password,
name: values.name,
role: "student",
},
await authClient.signUp.email(
{
email: values.email,
password: values.password,
name: values.name,
role: "student",
},
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
sundaram krishnan
sundaram krishnanOP9mo ago
doesn't work @rizvan it doesn't get updated in the mongo db but i can see the role changes in prisma studio @Kazz
await authClient.signUp.email(
{
email: values.email,
password: values.password,
name: values.name,
role: "student",
},
await authClient.signUp.email(
{
email: values.email,
password: values.password,
name: values.name,
role: "student",
},
the role doesn't change, when i try to do smthg teacher, it doesn't update it works fine, i have added another attirbute called UserRole and it works fine thx a lot

Did you find this page helpful?