Expand description
Organizations for fastauth: organizations, members,
invitations, roles and permissions. The /organization/* routes, JSON and error codes
match Better Auth’s organization plugin, so its organizationClient works unchanged.
use fastauth_core::prelude::*;
use fastauth_plugin_organization::{Organization, OrganizationExt, Roles};
let auth: Auth<MemoryAdapter, (), plugins![Organization]> = FastAuth::builder()
.secret(Secret::from_bytes(vec![1u8; 32]))
.base_url("http://localhost:3000")
.database(MemoryAdapter::new())
.email_password(EmailPassword::default())
.plugin(
Organization::default()
.organization_limit(5)
.invitation_ttl(Duration::days(7))
.roles(Roles::new().role("billing", [("invoice", &["read", "pay"])])),
)
.build()?;
assert!(auth.organization().roles().allows("billing", &[("invoice", &["pay"])]));The plugin adds the organization, member and invitation tables and an
active_organization_id column on session. A creator becomes the organization’s
owner, and the last owner can neither leave nor be demoted. What each role may do is
decided in one place, Roles::allows, which the endpoints, the server API
(OrganizationExt) and the axum extractors (feature axum) all call.
Without the plugin registered, auth.organization() doesn’t compile:
ⓘ
use fastauth_core::prelude::*;
use fastauth_plugin_organization::OrganizationExt;
let auth: Auth<MemoryAdapter> = FastAuth::builder()
.secret(Secret::from_bytes(vec![1u8; 32]))
.base_url("http://localhost:3000")
.database(MemoryAdapter::new())
.build()
.unwrap();
let _ = auth.organization();Re-exports§
pub use access::Permission;pub use access::Roles;
Modules§
- access
- Roles, the statements they grant, and the one permission check every endpoint and extractor goes through.
- extract
- axum extractors for organization membership and permissions (feature
axum).
Macros§
- path_
param - Declares a
PathParamtype:path_param!(pub OrgId = "org_id");.
Structs§
- Accept
Invitation POST /organization/accept-invitation: the invitee joins with the invited role, and the organization becomes the session’s active one.- AddMember
- Adds a user to an organization directly, Better Auth’s server-only
addMember. - Cancel
Invitation POST /organization/cancel-invitation: needsinvitation: [cancel]in the invitation’s organization. Answers the cancelled invitation.- Check
Permission - Whether the user’s role in the organization grants the permissions;
falsefor non-members. - Check
Slug POST /organization/check-slug:{ "status": true }when the slug is free, else 400ORGANIZATION_SLUG_ALREADY_TAKEN.- Check
Slug Input - Body of
/organization/check-slug. - Create
Organization POST /organization/create: creates an organization with the caller as its owner and makes it the session’s active organization. The server API’screateruns the same endpoint, so hooks onCreateOrganizationsee both.- Create
Organization Input - Body of
/organization/create. - Delete
Organization POST /organization/delete: needsorganization: [delete]. Deletes the organization with its members and invitations, and clears it as any session’s active organization. Answers the deleted organization.- Delete
Organization Input - Body of
/organization/delete. - Email
Query - Input of
UserInvitations. - Full
Organization - An organization with its members (and their users) and invitations.
- GetActive
Member GET /organization/get-active-member: the caller’s membership in the active organization, with their user.- GetFull
Organization GET /organization/get-full-organization: the requested organization (by id or slug, else the active one) with members and invitations.nullwhen there is no active organization.- GetFull
Organization Query - Query of
/organization/get-full-organization. - GetInvitation
GET /organization/get-invitation?id=..: a live invitation addressed to the caller.- HasPermission
POST /organization/has-permission: whether the caller’s role in the organization grants the permissions.- HasPermission
Input - Body of
/organization/has-permission:{ "permissions": { "member": ["delete"] } }. - HasPermission
Output - What
/organization/has-permissionanswers:{ "error": null, "success": bool }. - Invitation
- A row of
invitation. Its id is the secret in the invitation link. - Invitation
Accepted - Emitted when an invitee accepts;
MemberAddedfires for the new membership too. - Invitation
Details - An invitation with what an invitee needs to decide:
/organization/get-invitation. - Invitation
IdInput - Body of the endpoints that act on one invitation.
- Invitation
IdQuery - Query of
/organization/get-invitation. - Invitation
Response - What accepting or rejecting answers: the invitation and the new membership (
nullon rejection). - Invitation
With Organization - A pending invitation with its organization’s name:
/organization/list-user-invitations. - Invite
Member POST /organization/invite-member: needsinvitation: [create]; only owners invite owners. Stores a pending invitation that expires after the configured TTL and, when a mailer is configured, emails the invitee a link (AuthEmail::Plugin, kind"organization-invitation"). Answers the invitation.- Invite
Member Input - Body of
/organization/invite-member. - Leave
Organization POST /organization/leave: ends the caller’s own membership, unless they are the last owner. Answers the removed membership.- Leave
Organization Input - Body of
/organization/leave. - List
Invitations GET /organization/list-invitations: the organization’s invitations, for members who may create them. Invitations grantingowneronly show to owners.- List
Members GET /organization/list-members: members of the requested (or active) organization, with their users. Members only.- List
Members Output - What
/organization/list-membersanswers: a page of members and the total count. - List
Members Query - Query of
/organization/list-members. - List
Organizations GET /organization/list: the organizations the caller is a member of.- List
User Invitations GET /organization/list-user-invitations: the pending invitations addressed to the caller’s email, which must be verified. The server API lists any address’s withauth.organization().invitations_for_email(email).- Member
- A row of
member: one user’s membership and role in one organization. A user is a member of an organization at most once; the plugin checks that before every insert. - Member
Added - Emitted for every new membership: the creator’s,
add_member’s and an accepted invitation’s. - Member
Removed - Emitted when a member is removed or leaves. Deleting an organization doesn’t emit it per member.
- Member
User - The user part of a member in list responses:
{ id, name, email, image }. - Member
With User - A member with its user, as
/organization/list-membersand/organization/get-active-memberanswer. - NewMember
- A membership to add without an invitation.
- Organization
- The organization plugin:
.plugin(Organization::default()). - Organization
Api - Calls the organization endpoints without HTTP.
- Organization
Created - Emitted when an organization is created, over HTTP or by the server API.
- Organization
IdQuery - Query of
/organization/list-invitations. - Organization
Row - A row of
organization. NamedOrganizationRowbecauseOrganizationis the plugin. - Organization
Session - The column the plugin adds to
session. Clients see it asactiveOrganizationId; handlers read it withsession.session_plugin::<Organization, _>(). - Organization
With Members - An organization with its members: what
/organization/createanswers. - Permission
Check - A permission question about a user, without a session.
- Reject
Invitation POST /organization/reject-invitation: the invitee declines.- Remove
Member POST /organization/remove-member: needsmember: [delete]. Only owners remove owners, and never the last one.- Remove
Member Input - Body of
/organization/remove-member. - Remove
Member Output - What
/organization/remove-memberanswers. - SetActive
Organization POST /organization/set-active: makes an organization the caller is a member of the session’s active one, or clears it withorganizationId: null. Answers the organization, ornull.- SetActive
Organization Input - Body of
/organization/set-active. - Update
Member Role POST /organization/update-member-role: needsmember: [update](owners always may). Only owners grant or change the owner role, and the last owner can’t demote themselves.- Update
Member Role Input - Body of
/organization/update-member-role. - Update
Organization POST /organization/update: needsorganization: [update].- Update
Organization Data - The fields
/organization/updatechanges; absent fields stay as they are. - Update
Organization Input - Body of
/organization/update. - User
Invitations - Pending invitations addressed to any email.
Enums§
- Invitation
Status - Where an invitation stands. Only
Pendinginvitations can be accepted, rejected or resent. - Organization
Error - Why an organization request failed. Codes, messages and statuses match Better Auth’s
organization plugin, so its client’s
error.codechecks keep working. - Role
Input - A role in a request body:
"admin","admin,billing"or["admin", "billing"]. - Sort
Direction sortDirectionof/organization/list-members.
Traits§
- Organization
Ext auth.organization(): the server API. Only compiles on anAuthwithOrganizationregistered.