Skip to main content

Crate fastauth_plugin_organization

Crate fastauth_plugin_organization 

Source
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 PathParam type: path_param!(pub OrgId = "org_id");.

Structs§

AcceptInvitation
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.
CancelInvitation
POST /organization/cancel-invitation: needs invitation: [cancel] in the invitation’s organization. Answers the cancelled invitation.
CheckPermission
Whether the user’s role in the organization grants the permissions; false for non-members.
CheckSlug
POST /organization/check-slug: { "status": true } when the slug is free, else 400 ORGANIZATION_SLUG_ALREADY_TAKEN.
CheckSlugInput
Body of /organization/check-slug.
CreateOrganization
POST /organization/create: creates an organization with the caller as its owner and makes it the session’s active organization. The server API’s create runs the same endpoint, so hooks on CreateOrganization see both.
CreateOrganizationInput
Body of /organization/create.
DeleteOrganization
POST /organization/delete: needs organization: [delete]. Deletes the organization with its members and invitations, and clears it as any session’s active organization. Answers the deleted organization.
DeleteOrganizationInput
Body of /organization/delete.
EmailQuery
Input of UserInvitations.
FullOrganization
An organization with its members (and their users) and invitations.
GetActiveMember
GET /organization/get-active-member: the caller’s membership in the active organization, with their user.
GetFullOrganization
GET /organization/get-full-organization: the requested organization (by id or slug, else the active one) with members and invitations. null when there is no active organization.
GetFullOrganizationQuery
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.
HasPermissionInput
Body of /organization/has-permission: { "permissions": { "member": ["delete"] } }.
HasPermissionOutput
What /organization/has-permission answers: { "error": null, "success": bool }.
Invitation
A row of invitation. Its id is the secret in the invitation link.
InvitationAccepted
Emitted when an invitee accepts; MemberAdded fires for the new membership too.
InvitationDetails
An invitation with what an invitee needs to decide: /organization/get-invitation.
InvitationIdInput
Body of the endpoints that act on one invitation.
InvitationIdQuery
Query of /organization/get-invitation.
InvitationResponse
What accepting or rejecting answers: the invitation and the new membership (null on rejection).
InvitationWithOrganization
A pending invitation with its organization’s name: /organization/list-user-invitations.
InviteMember
POST /organization/invite-member: needs invitation: [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.
InviteMemberInput
Body of /organization/invite-member.
LeaveOrganization
POST /organization/leave: ends the caller’s own membership, unless they are the last owner. Answers the removed membership.
LeaveOrganizationInput
Body of /organization/leave.
ListInvitations
GET /organization/list-invitations: the organization’s invitations, for members who may create them. Invitations granting owner only show to owners.
ListMembers
GET /organization/list-members: members of the requested (or active) organization, with their users. Members only.
ListMembersOutput
What /organization/list-members answers: a page of members and the total count.
ListMembersQuery
Query of /organization/list-members.
ListOrganizations
GET /organization/list: the organizations the caller is a member of.
ListUserInvitations
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 with auth.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.
MemberAdded
Emitted for every new membership: the creator’s, add_member’s and an accepted invitation’s.
MemberRemoved
Emitted when a member is removed or leaves. Deleting an organization doesn’t emit it per member.
MemberUser
The user part of a member in list responses: { id, name, email, image }.
MemberWithUser
A member with its user, as /organization/list-members and /organization/get-active-member answer.
NewMember
A membership to add without an invitation.
Organization
The organization plugin: .plugin(Organization::default()).
OrganizationApi
Calls the organization endpoints without HTTP.
OrganizationCreated
Emitted when an organization is created, over HTTP or by the server API.
OrganizationIdQuery
Query of /organization/list-invitations.
OrganizationRow
A row of organization. Named OrganizationRow because Organization is the plugin.
OrganizationSession
The column the plugin adds to session. Clients see it as activeOrganizationId; handlers read it with session.session_plugin::<Organization, _>().
OrganizationWithMembers
An organization with its members: what /organization/create answers.
PermissionCheck
A permission question about a user, without a session.
RejectInvitation
POST /organization/reject-invitation: the invitee declines.
RemoveMember
POST /organization/remove-member: needs member: [delete]. Only owners remove owners, and never the last one.
RemoveMemberInput
Body of /organization/remove-member.
RemoveMemberOutput
What /organization/remove-member answers.
SetActiveOrganization
POST /organization/set-active: makes an organization the caller is a member of the session’s active one, or clears it with organizationId: null. Answers the organization, or null.
SetActiveOrganizationInput
Body of /organization/set-active.
UpdateMemberRole
POST /organization/update-member-role: needs member: [update] (owners always may). Only owners grant or change the owner role, and the last owner can’t demote themselves.
UpdateMemberRoleInput
Body of /organization/update-member-role.
UpdateOrganization
POST /organization/update: needs organization: [update].
UpdateOrganizationData
The fields /organization/update changes; absent fields stay as they are.
UpdateOrganizationInput
Body of /organization/update.
UserInvitations
Pending invitations addressed to any email.

Enums§

InvitationStatus
Where an invitation stands. Only Pending invitations can be accepted, rejected or resent.
OrganizationError
Why an organization request failed. Codes, messages and statuses match Better Auth’s organization plugin, so its client’s error.code checks keep working.
RoleInput
A role in a request body: "admin", "admin,billing" or ["admin", "billing"].
SortDirection
sortDirection of /organization/list-members.

Traits§

OrganizationExt
auth.organization(): the server API. Only compiles on an Auth with Organization registered.