Cannot create organizations

Hello, I am trying to generate mock data with authClient.organization.create() but it doesn't work. Has someone any idea ? I am using SvelteKit
No description
No description
No description
33 Replies
bekacru
bekacru2mo ago
don't use the authClient use auth instance and call auth.api.createOrganization
roze789
roze789OP2mo ago
i am getting this error
No description
No description
Kazz
Kazz2mo ago
Try adding headers in the createOrganization as well
roze789
roze789OP2mo ago
ok but what do i need to make into it ?
Kazz
Kazz2mo ago
What are you using? Nextjs or something else?
roze789
roze789OP2mo ago
SvelteKit
Kazz
Kazz2mo ago
Let me check the docs
roze789
roze789OP2mo ago
Thank you
Kazz
Kazz2mo ago
This is how they are adding headers in svelte
roze789
roze789OP2mo ago
i am into a seed.ts file i want to make data its not running through this file
Kazz
Kazz2mo ago
Oh you are in a seeding file, I've never tried seeding data with better-auth yet. But you cant get headers in a seeding file since there are no headers, and if there are no headers there is no session. createOrganization needs to know who the owner is, you can pass userId in the body but I have no idea if it will return an unauthorized response or not
roze789
roze789OP2mo ago
ok so i need to make it work doing the normal flow thank you I have another question you must make a dashboard to make it possible to create organisation ?
Kazz
Kazz2mo ago
I dont know what you mean, but you need a user in order to create an organization. Organization has 2 tables, Organization table and Member table. Member table links to the organization and the user and the role of the user in the organization When you create an organization with a user logged in, the user will be added as the owner of the organization
Kazz
Kazz2mo ago
Organization | Better Auth
The organization plugin allows you to manage your organization's members and teams.
roze789
roze789OP2mo ago
and is admin library worth to use along organisation library ?
Kazz
Kazz2mo ago
I havent used the admin library yet, but the admin has roles in the user table, while the roles in Organization is in Members table, so they are completely seperate things Organization already has 3 roles "Owner", "Admin", "Member", but you can add more if you want with their permissions
Kazz
Kazz2mo ago
Organization | Better Auth
The organization plugin allows you to manage your organization's members and teams.
roze789
roze789OP2mo ago
Thank you
Kazz
Kazz2mo ago
Also for your seeding, you can create the seed manually instead of using better-auth For example you can create an organization by adding an organization with all its fields to the database as well as adding the member part for it.
Kazz
Kazz2mo ago
Example for what i used before, this is a function i use to create an organization when someone pays successfuly, i wont have their session but i would have their userId so i do the rest manually
No description
roze789
roze789OP2mo ago
interesting. So you only need userId to make a new organisation ?
Kazz
Kazz2mo ago
Yes, you can create a user and take his id and use it for creating the organization as well, all asynchronously so that you get the id of each one. But one more note, you need to create the ID as you can see above 1. Create userId, then create user with the generated userId 2. Create organizationId, then create an organization with generated organizationId and userId before 3. Create the Member connection with the userId and organizationId and the role you want "owner", "admin", or "member" I think this is the fastest way for a seed file
roze789
roze789OP2mo ago
Thank you a lot @Kazz It works thx. I have another question. sessions and members tables are empties. They are used while logging in ?
Kazz
Kazz2mo ago
Session table is for the login session Member table is for users in organization, so if user1 created or joined an organization, they will be added to the member table with their role in the organization (userId, orgId, role)
roze789
roze789OP2mo ago
Ah yes, my members table is filled Thank you @Kazz I would like to log in with an account so I need to add data into account table ? in my drizzle studio, i have no record into this table
Kazz
Kazz2mo ago
Accounts are different providers you are logging in with that links to the user. So if a user logs in with google and github with the same email, they will both be linked to the same user. You just beed providers
roze789
roze789OP2mo ago
I will use Email/password first i would like to find a way to hash my password but what to use to hash a password
Kazz
Kazz2mo ago
Bcrypt
roze789
roze789OP2mo ago
in the doc, it says scrypt
Kazz
Kazz2mo ago
You can use whatever you want
roze789
roze789OP2mo ago
My thought is: I want to create a account manually, then tried to log in with my frontend app. So i need to sync hash and comparehash algorithme so it can work I will need to change the hash and verify
roze789
roze789OP2mo ago

Did you find this page helpful?