Unable to check permisions
When I do this
with an admin person
its keeping console me this :
there is my permissions.ts
auth.ts
export async function updateUser(id: string, payload: Partial<User>) {
const canCreateProject = await authClient.admin.hasPermission(
{
permissions: {
product: ["create"],
},
},
{
headers: await headers(),
},
);
console.log("canCreateProject", canCreateProject);
}
export async function updateUser(id: string, payload: Partial<User>) {
const canCreateProject = await authClient.admin.hasPermission(
{
permissions: {
product: ["create"],
},
},
{
headers: await headers(),
},
);
console.log("canCreateProject", canCreateProject);
}
canCreateProject { data: null, error: { status: 0, statusText: '' } }
canCreateProject { data: null, error: { status: 0, statusText: '' } }
import { createAccessControl } from "better-auth/plugins/access";
import { adminAc, defaultStatements } from "better-auth/plugins/admin/access";
// Define your statements with all permissions
export const statements = {
...defaultStatements,
product: ["create", "update", "delete", "list"],
users: ["create", "update", "delete"],
} as const;
// Create access control with these statements
export const ac = createAccessControl(statements);
// Admin has all permissions
export const admin = ac.newRole({
...adminAc.statements,
product: ["create", "update", "delete"],
users: ["create", "update", "delete"],
});
import { createAccessControl } from "better-auth/plugins/access";
import { adminAc, defaultStatements } from "better-auth/plugins/admin/access";
// Define your statements with all permissions
export const statements = {
...defaultStatements,
product: ["create", "update", "delete", "list"],
users: ["create", "update", "delete"],
} as const;
// Create access control with these statements
export const ac = createAccessControl(statements);
// Admin has all permissions
export const admin = ac.newRole({
...adminAc.statements,
product: ["create", "update", "delete"],
users: ["create", "update", "delete"],
});
export const auth = betterAuth({
database: drizzleAdapter(db, {
provider: "pg",
}),
emailAndPassword: {
enabled: true,
sendResetPassword: async ({ user, url }) => {
},
},
user: {
changeEmail: {
enabled: true,
sendChangeEmailVerification: async ({ newEmail, url }) => {
// Il faudrait ajouter ceci:
},
},
additionalFields: {
truc_de_con: {
type: "string",
nullable: true,
},
},
},
plugins: [
nextCookies(),
adminPlugin({
ac,
roles: {
admin: adminRole,
},
}),
],
});
export type Session = typeof auth.$Infer.Session;
export type User = (typeof auth.$Infer.Session)["user"];
export const auth = betterAuth({
database: drizzleAdapter(db, {
provider: "pg",
}),
emailAndPassword: {
enabled: true,
sendResetPassword: async ({ user, url }) => {
},
},
user: {
changeEmail: {
enabled: true,
sendChangeEmailVerification: async ({ newEmail, url }) => {
// Il faudrait ajouter ceci:
},
},
additionalFields: {
truc_de_con: {
type: "string",
nullable: true,
},
},
},
plugins: [
nextCookies(),
adminPlugin({
ac,
roles: {
admin: adminRole,
},
}),
],
});
export type Session = typeof auth.$Infer.Session;
export type User = (typeof auth.$Infer.Session)["user"];
6 Replies
there is my auth-client.ts
Someone has an idea ?
?
import { createAuthClient } from "better-auth/react";
import { adminClient } from "better-auth/client/plugins";
import { ac, admin as adminRole } from "@/lib/permissions";
import { nextCookies } from "better-auth/next-js";
export const authClient = createAuthClient({
baseURL: "http://localhost:3000",
plugins: [
adminClient({
ac,
roles: {
admin: adminRole,
},
}),
nextCookies(),
],
});
import { createAuthClient } from "better-auth/react";
import { adminClient } from "better-auth/client/plugins";
import { ac, admin as adminRole } from "@/lib/permissions";
import { nextCookies } from "better-auth/next-js";
export const authClient = createAuthClient({
baseURL: "http://localhost:3000",
plugins: [
adminClient({
ac,
roles: {
admin: adminRole,
},
}),
nextCookies(),
],
});
@Jïns
You cannot use the authClient on the Server, the AuthClient basically calls your backend fromt he client side. If you are on the Server you need to get the Session and use the api.userHasPermission function:
import { auth } from '@/auth';
import { headers } from 'next/headers';
const session = await auth.api.getSession({headers: await headers(),});
const { success: isAuthorized } = await auth.api.userHasPermission({
body: {
userId: session.user.id,
permission: {
product: ['create'],
},
},
});
import { auth } from '@/auth';
import { headers } from 'next/headers';
const session = await auth.api.getSession({headers: await headers(),});
const { success: isAuthorized } = await auth.api.userHasPermission({
body: {
userId: session.user.id,
permission: {
product: ['create'],
},
},
});
API | Better Auth
Better Auth API.
Client | Better Auth
Better Auth client library for authentication.
Worth going through the Concepts pages
thanks a lot for that
@Soheel