Admin
The Admin plugin provides a set of administrative functions for user management in your application. It allows administrators to perform various operations such as creating users, managing user roles, banning/unbanning users, impersonating users, and more.
Installation
Add the plugin to your auth config
To use the Admin plugin, add it to your auth config.
Migrate the database
Run the migration or generate the schema to add the necessary fields and tables to the database.
See the Schema section to add the fields manually.
Add the client plugin
Next, include the admin client plugin in your authentication client instance.
Usage
Before performing any admin operations, the user must be authenticated with an admin account. An admin is any user assigned the admin
role. For the first admin user, you'll need to manually assign the admin
role to their account in your database.
Create User
Allows an admin to create a new user.
List Users
Allows an admin to list all users in the database.
By default, 100 users are returned. You can adjust the limit and offset using the following query parameters:
search
: The search query to apply to the users. It can be an object with the following properties:field
: The field to search on, which can beemail
orname
.operator
: The operator to use for the search. It can becontains
,starts_with
, orends_with
.value
: The value to search for.
limit
: The number of users to return.offset
: The number of users to skip.sortBy
: The field to sort the users by.sortDirection
: The direction to sort the users by. Defaults toasc
.filter
: The filter to apply to the users. It can be an array of objects.
Set User Role
Changes the role of a user.
Ban User
Bans a user, preventing them from signing in and revokes all of their existing sessions.
Unban User
Removes the ban from a user, allowing them to sign in again.
List User Sessions
Lists all sessions for a user.
Revoke User Session
Revokes a specific session for a user.
Revoke All Sessions for a User
Revokes all sessions for a user.
Impersonate User
This feature allows an admin to create a session that mimics the specified user. The session will remain active until either the browser session ends or it reaches 1 hour. You can change this duration by setting the impersonationSessionDuration
option.
Stop Impersonating User
To stop impersonating a user and continue with the admin account, you can use stopImpersonating
Remove User
Hard deletes a user from the database.
Schema
This plugin adds the following fields to the user
table:
Field Name | Type | Key | Description |
---|---|---|---|
role | string | The user's role. Defaults to `user`. Admins will have the `admin` role. | |
banned | boolean | Indicates whether the user is banned. | |
banReason | string | The reason for the user's ban. | |
banExpires | number | The Unix timestamp when the user's ban will expire. |
And adds one field in the session
table:
Field Name | Type | Key | Description |
---|---|---|---|
impersonatedBy | string | The ID of the admin that is impersonating this session. |
Options
Default Role
The default role for a user. Defaults to user
.
you can pass false
to disable assigning default role
Admin role
You can specify what role should be consider admin. Default to admin
impersonationSessionDuration
The duration of the impersonation session in seconds. Defaults to 1 hour.
Default Ban Reason
The default ban reason for a user created by the admin. Defaults to No reason
.
Default Ban Expires In
The default ban expires in for a user created by the admin in seconds. Defaults to undefined
(meaning the ban never expires).