Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2k
Need some advice for better auth setup#6399
-
I am working on a application which requires 2 main roles consultant and client (maybe will have admin for overseeing). I want the user to be able to select roles from client/ consultant when they sign up and go to the onboarding screen. For the first phase i am working on the consultant part, and how it works is that the consultant can manage appointments with the client(book/view/cancel), view all his current clients, view all documents issued to a client. The client will be able to manage appointment with the consultant, view all the different consultants he is going to, view documents issued by those consultants etc. How do i setup better auth for such a app. I have never worked with RBAC before and it is driving me crazy. Any suggestions for such a setup would be appreciated! |
BetaWas this translation helpful?Give feedback.
All reactions
Replies: 1 comment 6 replies
-
Hi@TANISHQBEDI! I'mDosu and I’m helping the better-auth team. Here’s a practical way to set up RBAC and role selection for your app using better-auth:
Example (simplified): // permissions.tsimport{createAccessControl}from"better-auth/plugins/access";conststatement={appointment:["book","view","cancel"],client:["list","view"],document:["view","issue"],}asconst;constac=createAccessControl(statement);exportconstconsultant=ac.newRole({appointment:["book","view","cancel"],client:["list","view"],document:["view","issue"],});exportconstclient=ac.newRole({appointment:["book","view","cancel"],consultant:["list","view"],document:["view"],});exportconstadmin=ac.newRole({// all permissions ...statement,});// auth.tsimport{betterAuth}from"better-auth";import{adminasadminPlugin}from"better-auth/plugins";import{ac,consultant,client,admin}from"./permissions";exportconstauth=betterAuth({user:{additionalFields:{role:{type:"string",input:false},},},plugins:[adminPlugin({ ac,roles:{ consultant, client, admin},defaultRole:"client",// fallback}),],databaseHooks:{"user.create.before":async(ctx)=>{// Assign role based on signup context (e.g., from OAuth2 state or custom field)ctx.data.role=ctx.signupIntentRole||"client";},},}); On the client, after signup, check the user’s role and route to the correct onboarding screen. If you want more details or a deeper dive into any step, let me know! To reply, just mention@dosu. How did I do?Good |Irrelevant |Incorrect |Verbose |Hallucination |Report 🐛 |Other |
BetaWas this translation helpful?Give feedback.
All reactions
-
When you set a field like role to input: false in better-auth, users can't set it directly from the client—this is for security. Here’s how you can still assign roles (or other protected fields) during signup for both email and social providers: Email/password signup: // In your better-auth configdatabaseHooks:{user:{create:{before:async(user,ctx)=>({data:{ ...user,role:ctx.body.role||"client",// assign from request body},}),},},}, OTP/email signup: Social/OAuth2 providers: // When starting OAuth2 loginconststate=JSON.stringify({role:"consultant"});// Pass this state to the provider's authorization URL// In your user.create.before hookconst{ role}=ctx.state;// Extracted from OAuth2 state param// Assign to user as above If you want the role to be available in the session object on the client, use the customSession plugin to merge user data into the session, since additional fields are not included by default in session responses: customSession(async({ user, session})=>({ ...session,role:user.role,})) Key points:
Let me know if you want a more detailed code example for your specific flow! To reply, just mention@dosu. How did I do?Good |Irrelevant |Incorrect |Verbose |Hallucination |Report 🐛 |Other |
BetaWas this translation helpful?Give feedback.
All reactions
-
@dosu Thats the issue how do i send the request body with input false? export default function Home() { return ( Sign Up with Email Sign Up with Google ); } Thats what i have so far |
BetaWas this translation helpful?Give feedback.
All reactions
-
You can include the role in your signup request body (like you're doing), but because you set How it works:
Key points:
Let me know if you want a full working example for your stack! To reply, just mention@dosu. How did I do?Good |Irrelevant |Incorrect |Verbose |Hallucination |Report 🐛 |Other |
BetaWas this translation helpful?Give feedback.
All reactions
-
I tried it but i get the error saying |
BetaWas this translation helpful?Give feedback.
All reactions
-
Finally I am going with this kind of approach |
BetaWas this translation helpful?Give feedback.