BA
Better Auth•3mo ago
In&Out

admin createUser doesnt work

Hey folks, im trying to make user and i get this error,
POST /api/auth/admin/create-user 401 in 6457ms
{ data: null, error: { status: 401, statusText: 'UNAUTHORIZED' } }
POST /api/auth/admin/create-user 401 in 6457ms
{ data: null, error: { status: 401, statusText: 'UNAUTHORIZED' } }
21 Replies
In&Out
In&OutOP•3mo ago
export async function insertEditor(
_prevState: AuthFormState,
formData: FormData
): Promise<AuthFormState> {
const session = await auth.api.getSession({
headers: await headers(),
});

if (!session || !session.user) {
return { error: "Could not verify superadmin." };
}

const email = String(formData.get("email") ?? "").trim();
const password = String(formData.get("password") ?? "");
const name = String(formData.get("name") ?? "");

if (!email || !password || !name) {
return { error: "Fill all fields." };
}

if (password.length < 6) {
return { error: "Password must be at least 6 characters." };
}

try {
const newUser = await authClient.admin.createUser({
name,
email,
password,
data: {
role: "editor",
ibk_slug: session.user.ibk_slug,
},
});
console.log(newUser)

if (!newUser) {
return { error: "User creation failed." };
}

return { success: true };
} catch (error: any) {
console.error("User creation error:", error);

if (error.message?.includes("already") || error.message?.includes("duplicate")) {
return { error: "Email already exists." };
}

return { error: "Something went wrong. Try again." };
}
}
export async function insertEditor(
_prevState: AuthFormState,
formData: FormData
): Promise<AuthFormState> {
const session = await auth.api.getSession({
headers: await headers(),
});

if (!session || !session.user) {
return { error: "Could not verify superadmin." };
}

const email = String(formData.get("email") ?? "").trim();
const password = String(formData.get("password") ?? "");
const name = String(formData.get("name") ?? "");

if (!email || !password || !name) {
return { error: "Fill all fields." };
}

if (password.length < 6) {
return { error: "Password must be at least 6 characters." };
}

try {
const newUser = await authClient.admin.createUser({
name,
email,
password,
data: {
role: "editor",
ibk_slug: session.user.ibk_slug,
},
});
console.log(newUser)

if (!newUser) {
return { error: "User creation failed." };
}

return { success: true };
} catch (error: any) {
console.error("User creation error:", error);

if (error.message?.includes("already") || error.message?.includes("duplicate")) {
return { error: "Email already exists." };
}

return { error: "Something went wrong. Try again." };
}
}
even tried this but it doesnt work
const result = await auth.api.signUpEmail({
body: {
email,
password,
name: userName,
ibk_slug,
category,
role:"admin",
},
});
const result = await auth.api.signUpEmail({
body: {
email,
password,
name: userName,
ibk_slug,
category,
role:"admin",
},
});
import { betterAuth } from "better-auth";
import { nextCookies } from "better-auth/next-js";
import { Pool } from "pg";
import { admin } from "better-auth/plugins";

export const auth = betterAuth({
database: new Pool({
connectionString: process.env.DATABASE_URL,
}),
emailAndPassword: {
enabled: true,
},
user: {
additionalFields: {
role: {
type: "string",
required: false,
defaultValue: "admin",
input: true,
},
ibk_slug: {
type: "string",
required: false,
},
category: {
type: "string",
required: true,
},
},
},
plugins: [nextCookies(), admin()],
})
import { betterAuth } from "better-auth";
import { nextCookies } from "better-auth/next-js";
import { Pool } from "pg";
import { admin } from "better-auth/plugins";

export const auth = betterAuth({
database: new Pool({
connectionString: process.env.DATABASE_URL,
}),
emailAndPassword: {
enabled: true,
},
user: {
additionalFields: {
role: {
type: "string",
required: false,
defaultValue: "admin",
input: true,
},
ibk_slug: {
type: "string",
required: false,
},
category: {
type: "string",
required: true,
},
},
},
plugins: [nextCookies(), admin()],
})
@nikatune
nikatune
nikatune•3mo ago
are you sure ur role is admin ?
In&Out
In&OutOP•3mo ago
now it is but i still get
POST /api/auth/admin/create-user 401 in 6457ms
{ data: null, error: { status: 401, statusText: 'UNAUTHORIZED' } }
POST /api/auth/admin/create-user 401 in 6457ms
{ data: null, error: { status: 401, statusText: 'UNAUTHORIZED' } }
{
session: {
expiresAt: 2025-07-09T16:57:38.824Z,
token: 'z0biiY014CWSHkod7axp4LZGks5sBNqS',
createdAt: 2025-07-02T16:57:38.824Z,
updatedAt: 2025-07-02T16:57:38.824Z,
ipAddress: '',
userAgent: '',
userId: '39een2KSjuZBYY7hnf1Ej6XZ1R3m671E',
impersonatedBy: undefined,
id: 'frljIsBqFUDUhodLuLdi2PSSdCtfuuhA'
},
user: {
name: 'NoviUstanova',
email: 'bratovi123@gmail.com',
emailVerified: false,
image: null,
createdAt: 2025-07-02T16:57:38.669Z,
updatedAt: 2025-07-02T16:57:38.669Z,
role: 'admin',
banned: undefined,
banReason: undefined,
banExpires: undefined,
ibk_slug: 'slug',
category: 'Kindergarden',
id: '39een2KSjuZBYY7hnf1Ej6XZ1R3m671E'
}
}
{
session: {
expiresAt: 2025-07-09T16:57:38.824Z,
token: 'z0biiY014CWSHkod7axp4LZGks5sBNqS',
createdAt: 2025-07-02T16:57:38.824Z,
updatedAt: 2025-07-02T16:57:38.824Z,
ipAddress: '',
userAgent: '',
userId: '39een2KSjuZBYY7hnf1Ej6XZ1R3m671E',
impersonatedBy: undefined,
id: 'frljIsBqFUDUhodLuLdi2PSSdCtfuuhA'
},
user: {
name: 'NoviUstanova',
email: 'bratovi123@gmail.com',
emailVerified: false,
image: null,
createdAt: 2025-07-02T16:57:38.669Z,
updatedAt: 2025-07-02T16:57:38.669Z,
role: 'admin',
banned: undefined,
banReason: undefined,
banExpires: undefined,
ibk_slug: 'slug',
category: 'Kindergarden',
id: '39een2KSjuZBYY7hnf1Ej6XZ1R3m671E'
}
}
nikatune
nikatune•3mo ago
can i see your auth-client file?
In&Out
In&OutOP•3mo ago
import { createAuthClient } from "better-auth/react";
import { adminClient } from "better-auth/client/plugins";

export const authClient = createAuthClient({
/** The base URL of the server (optional if you're using the same domain) */
baseURL: "http://localhost:3000",
plugins: [adminClient()],
});

// If you're using a different base path other than /api/auth make sure to pass the whole URL including the path. (e.g. http://localhost:3000/custom-path/auth)

export const { signIn, signOut, signUp, useSession } = authClient;
import { createAuthClient } from "better-auth/react";
import { adminClient } from "better-auth/client/plugins";

export const authClient = createAuthClient({
/** The base URL of the server (optional if you're using the same domain) */
baseURL: "http://localhost:3000",
plugins: [adminClient()],
});

// If you're using a different base path other than /api/auth make sure to pass the whole URL including the path. (e.g. http://localhost:3000/custom-path/auth)

export const { signIn, signOut, signUp, useSession } = authClient;
nikatune
nikatune•3mo ago
const newUser = await authClient.admin.createUser({
name,
email,
password,
role:"user",
data: {
role: "editor",
ibk_slug: session.user.ibk_slug,
},
});
const newUser = await authClient.admin.createUser({
name,
email,
password,
role:"user",
data: {
role: "editor",
ibk_slug: session.user.ibk_slug,
},
});
can you try it like this? it should work
In&Out
In&OutOP•3mo ago
POST /api/auth/admin/create-user 401 in 493ms
{ data: null, error: { status: 401, statusText: 'UNAUTHORIZED' } }
POST /api/auth/admin/create-user 401 in 493ms
{ data: null, error: { status: 401, statusText: 'UNAUTHORIZED' } }
nikatune
nikatune•3mo ago
@Ping any idea?
In&Out
In&OutOP•3mo ago
i literally gotta finish my whole site by tomorrow and this is causing me issues lmao
nikatune
nikatune•3mo ago
maybe can you try deleting your cookies and login again?
In&Out
In&OutOP•3mo ago
i did that 20 times
Calling admin.createUser as: admin
{ data: null, error: { status: 401, statusText: 'UNAUTHORIZED' } }
Calling admin.createUser as: admin
{ data: null, error: { status: 401, statusText: 'UNAUTHORIZED' } }
if it matters by any chance, im using authClient inside server component
nikatune
nikatune•3mo ago
wait what??? can u try it with auth.api ?
In&Out
In&OutOP•3mo ago
what?
nikatune
nikatune•3mo ago
can u try it on server side
In&Out
In&OutOP•3mo ago
wouldnt it be the same? i cant createUser with { auth } if you mean that well, it works when putting it into client component
nikatune
nikatune•3mo ago
🙂
In&Out
In&OutOP•3mo ago
thanks much for help tho
nikatune
nikatune•3mo ago
np sir
d3adb0y
d3adb0y•4w ago
im having the same issue. i want to create test users for cypress but the server side auth.api.createUser returns a type error
Ping
Ping•4w ago
Can you show me what the type error looks like?
d3adb0y
d3adb0y•4w ago
I figure it out lol I was using the client plug-in Not the server plug-in 🤠

Did you find this page helpful?