The developers of better auth already added the ability to have roles in their admin plugin, use that addon instead of this one
π‘οΈ @hedystia/better-auth-admin
Enhance your Better Auth projects with powerful admin features! π
- π Role-based Access Control: Define and manage user roles and permissions
- π₯ Advanced User Management: Create, update, list, and remove users with ease
- π« User Banning: Temporarily or permanently restrict user access
- π€ User Impersonation: Securely access user accounts for support purposes
- π Flexible User Search: Find users quickly with powerful search and filter options
- π οΈ Customizable Configuration: Tailor the admin features to your project needs
- Install the package:
npm install @hedystia/better-auth-admin
- Import the plugin in your
auth.ts
file:
import { admin } from "@hedystia/better-auth-admin";
import { betterAuth } from "better-auth";
export const auth = betterAuth({
plugins: [
// Other plugins...
admin({
// Configuration options
}),
],
});
- Add the plugin to your
authClient.ts
file:
import { adminClient } from "@hedystia/better-auth-admin/client";
import { createAuthClient } from "better-auth/client";
export default createAuthClient({
plugins: [
// Other plugins...
adminClient()
],
});
The admin
plugin accepts an optional configuration object with the following properties:
admin({
defaultRole: "user",
adminRole: "admin",
defaultBanReason: "No reason",
defaultBanExpiresIn: 60 * 60 * 24 * 7, // 1 week
impersonationSessionDuration: 60 * 60, // 1 hour
permissions: {
global: "*",
createRole: "create_role",
listPermissions: "list_permissions",
listRoles: "list_roles",
updateRole: "update_role",
getRole: "get_role",
setRole: "set_role",
banUser: "ban_user",
unBanUser: "un_ban_user",
impersonateUser: "impersonate_user",
stopImpersonating: "stop_impersonating",
createUser: "create_user",
updateUser: "update_user",
deleteUser: "delete_user",
listUsers: "list_users",
setUserRole: "set_user_role",
listUserSessions: "list_user_sessions",
revokeUserSession: "revoke_user_session",
revokeUserSessions: "revoke_user_sessions",
linkUser: "link_user",
unlinkUser: "unlink_user",
removeUser: "remove_user",
},
});
This plugin is a fork of the original better-auth βadminβ plugin but enhanced to support permissions for each role.
This project is licensed under the MIT License.