How can I use roles and permissions from the database to manage authorisation using the admin plugin

I have written server functions to fetch roles and permissions from the database and attempted to use them in auth.ts and authClient.ts. But it's not working. Please, anyone can help to fine how I use roles and permissions from database
4 Replies
Hiếu Trần
Hiếu Trần4w ago
Share your code.
Dhruvi joshi
Dhruvi joshiOP4w ago
auth.ts export const auth = betterAuth({ plugins: [ admin({ ac: await getAccessControl(), roles: await initializeRoles(), }), ] }) authClient.ts export const authClient = createAuthClient( plugins: [ adminClient({ ac: await getAccessControl(), roles: await initializeRoles() }) ] }); dbQuery.ts export const getPermissionList = async () => { const permissions = await prisma.permission.findMany({ select:{ page: true, access: true } }); // Transform array to object with page as key and access as value const data = permissions.reduce((acc, { page, access }) => { acc[page] = access; return acc; }, {} as Record<string, string[]>); // console.log(data); return data; }
Hiếu Trần
Hiếu Trần4w ago
I need to understand a bit more. You said "it's not working", what exactly do you mean?
Dhruvi joshi
Dhruvi joshiOP4w ago
I am getting an error in fetching roles and accessControl as below Server Functions cannot be called during initial render. This would create a fetch waterfall. Try to use a Server Component to pass data to Client Components instead. undefined ⨯ Error: Server Functions cannot be called during initial render. This would create a fetch waterfall. Try to use a Server Component to pass data to Client Components instead. at eval (src/lib/auth-client.ts:15:35) at [project]/src/lib/auth-client.ts [app-ssr] (ecmascript) (.next/server/chunks/ssr/[root-of-the-server]d9b18883._.js:1593:1) at eval (src/lib/authorization/protectedRouteProvider.tsx:4:0) at [project]/src/lib/authorization/protectedRouteProvider.tsx [app-ssr] (ecmascript) (.next/server/chunks/ssr/[root-of-the-server]d9b18883._.js:1631:1) 13 | emailOTPClient(), 14 | adminClient({
15 | ac: await getAccessControl(),
| ^ 16 | roles: await initializeRoles() 17 | }) 18 | ] { digest: '4076722315' }

Did you find this page helpful?