Expand description
The admin plugin for fastauth: roles and
statement-based permissions, bans, impersonation and user management. The routes and
JSON match Better Auth’s admin plugin, so its adminClient works unchanged.
use fastauth_core::prelude::*;
use fastauth_plugin_admin::{Admin, AdminExt, Permissions};
let auth: Auth<MemoryAdapter, (), plugins![Admin]> = FastAuth::builder()
.secret(Secret::from_bytes(vec![1u8; 32]))
.base_url("http://localhost:3000")
.database(MemoryAdapter::new())
.email_password(EmailPassword::default())
.plugin(
Admin::new()
.role("support", Permissions::new().allow("user", ["list", "get"]))
.impersonation_session_duration(Duration::minutes(30)),
)
.build()?;
let _admin = auth.admin();It adds role, banned, ban_reason and ban_expires to user and
impersonated_by to session, none of them client-settable, and serves
/admin/create-user, list-users, get-user, set-role, ban-user, unban-user,
update-user, set-user-password, remove-user, list-user-sessions,
revoke-user-session, revoke-user-sessions, impersonate-user, stop-impersonating
and has-permission. Each checks the caller’s roles against the action it performs
and answers 403 with Better Auth’s code when they don’t allow it; auth.admin() runs
the same operations from server code without a session.
A sign-in gate refuses banned users with 403 BANNED_USER for every sign-in method,
and lifts expired bans.
With the axum feature, RequireRole guards your own routes by role.
Modules§
Macros§
- role
- Declares a role marker for
RequireRole:
Structs§
- Admin
- The admin plugin:
.plugin(Admin::new()). - Admin
Api - Runs admin operations from server code. Calls are trusted: they need no session and skip the permission checks, but run the same hooks and events as the HTTP routes. Impersonation acts for a signed-in admin, so it takes the request’s headers.
- Admin
Session - The column admin adds to
session: who is impersonating through it. - Admin
User - The columns admin adds to
user. None of them is client-settable; only admin endpoints and the server API change them. Apps read them withsession.user.plugin::<Admin, _>(). - BanUser
POST /admin/ban-user: needsuser:ban. Revokes every session of the user.- BanUser
Input - Body of
/admin/ban-user. - Create
User POST /admin/create-user: needsuser:create, plususer:set-roleto pick a role anduser:banto set ban columns.- Create
User Input - Body of
/admin/create-user. - GetUser
GET /admin/get-user?id=..: needsuser:get. Answers the user itself, unwrapped.- GetUser
Query - Query of
/admin/get-user. - HasPermission
POST /admin/has-permission: whether the signed-in user (any user, not only admins) may do everything inpermissions.- HasPermission
Input - Body of
/admin/has-permission. Over HTTP the caller’s session decides; server calls name a user or a role. - Impersonate
User POST /admin/impersonate-user: needsuser:impersonate, plususer:impersonate-adminswhen the target is an admin. Signs the browser in as the user with a short session markedimpersonatedBy, and keeps the admin’s session token in a signed{prefix}.admin_sessioncookie forstop-impersonating. Impersonation sessions can’t impersonate, and banned users can’t be impersonated.- List
User Sessions POST /admin/list-user-sessions: needssession:list. The user’s unexpired sessions.- List
Users GET /admin/list-users: needsuser:list. Search, filters, sorting and paging all run in the database;totalcounts every match.- List
Users Query - Query of
/admin/list-users. - Permission
Output { "error": null, "success": true }.- Permissions
- Actions per resource:
{ "user": ["list", "ban"] }. A role’s grants, or the actions a permission check asks for. - Remove
User POST /admin/remove-user: needsuser:delete. Deletes the user’s sessions and accounts, then the user, and emitsSessionRevoked(when they had sessions) andUserDeleted.- Require
Role - The signed-in session of a user who holds role
R. Rejects with401 UNAUTHORIZEDwithout a session and403 FORBIDDENwithout the role. A user holds every role in their comma-separateduser.role(the default role while it is empty); admins hold every configured admin role, soRequireRole<A, role::Admin>also letsadmin_user_idsin. - Revoke
User Session POST /admin/revoke-user-session: needssession:revoke. Succeeds when no session with that token is left, even if there never was one.- Revoke
User Session Input - Body of
/admin/revoke-user-session. - Revoke
User Sessions POST /admin/revoke-user-sessions: needssession:revoke.- Session
List { "sessions": [..] }.- Session
Output { "session": {..}, "user": {..} }.- SetRole
POST /admin/set-role: needsuser:set-role. Every role must be defined.- SetRole
Input - Body of
/admin/set-role. - SetUser
Password POST /admin/set-user-password: needsuser:set-password. Checks the password policy, creates the credential account when the user has none, and emitsPasswordChanged.- SetUser
Password Input - Body of
/admin/set-user-password. - Stop
Impersonating POST /admin/stop-impersonating: ends the impersonation session and signs the browser back in as the admin from theadmin_sessioncookie.- Success
{ "success": true }.- Unban
User POST /admin/unban-user: needsuser:ban.- Update
User POST /admin/update-user: needsuser:update, plususer:set-roleforrole,user:banfor the ban columns anduser:set-emailforemailandemailVerified. Answers the user itself, unwrapped. Settingbanned: truerevokes the user’s sessions.- Update
User Input - Body of
/admin/update-user. - User
IdInput - Body of the endpoints that name one user.
- User
List { "users": [..], "total": 42, "limit": 10, "offset": 0 }.totalcounts every match.- User
Output { "user": {..} }.
Enums§
- Admin
Error - Why an admin request failed. Codes, statuses and messages match Better Auth’s admin
plugin;
USER_NOT_FOUND,INVALID_EMAILandUSER_ALREADY_EXISTS_USE_ANOTHER_EMAILcome from core. - Roles
- One role or several:
"admin"or["admin", "support"]. Stored comma-separated. - Search
Field - Which column
searchValuematches. - Search
Operator - How
searchValuematches. Case-sensitive. - Sort
Direction ascordesc.- Where
Operator - How
filterValuecompares withfilterField.inandnot_intake a comma-separated list; the text operators are case-sensitive.
Constants§
- STATEMENTS
- Every resource and action the admin endpoints check, as Better Auth defines them.