Auth
Easily implement reliable authentication strategies.
Authentication is essential for securing applications, and bknd provides a straightforward approach to implementing robust strategies.
Core Features
- Automatically creates a
userentity, with support for customizable fields. - Authenticates users based on configurable strategies.
- Generates JWTs according to specified configurations.
- Provides session management for maintaining user authentication state.
Supported Authentication Strategies
- Email/Password: Supports plain and SHA-256 password hashing (bcrypt planned for future releases).
- OAuth/OIDC: Works with providers like Google and GitHub.
- Compatible with any specification-compliant provider.
With a focus on flexibility and ease of integration, bknd's authentication system offers the essentials for managing secure user access in your applications.
Server Module: module.auth
auth.changePassword([userId], [password])
To change a user's password, use the changePassword method:
await app.module.auth.changePassword(user.id, password);This method updates the password for the specified user.
userId: The ID of the user whose password should be changed.password: The new password value.
This method throws an error if:
- The user with the given
userIdis not found. - The user's authentication strategy is not
"password"(e.g. the user registered via an OAuth provider).
Securing Your Admin Portal
Do not enable the Guard without an admin user
Enabling the Guard without first creating a user with an admin role will lock you out of the admin portal entirely. There is no login screen that can save you — you'll need to manually edit the database to recover. Follow the checklist below before enabling the Guard.
The Guard protects your admin portal and API endpoints by requiring authentication and proper permissions. Before enabling it, you must set up at least one user with full admin access.
Checklist Before Enabling Guard
Complete these steps in order before turning on the Guard:
-
Create an admin role with
implicit_allow: true- This grants full access to all permissions
- Go to Auth → Roles → Create a new role
- Enable the "Implicit Allow" toggle
-
Create a user
- Go to Auth → Users → Create a new user
- Set up their email and password
-
Attach the admin role to the user
- Edit the user you just created
- Assign the admin role to them
-
Verify you can sign in
- Open an incognito/private browser window
- Navigate to your app and sign in with the admin user
- Confirm you have access
-
Now enable the Guard
- Go to Auth → Settings
- Enable the Guard
Recovery: If You're Locked Out
If you enabled the Guard without setting up an admin user, you'll need to access your database directly:
- Connect to your database using a database client or CLI tool
- Find the
__bkndtable - Locate the row where
type = 'config' - In the
jsoncolumn, setauth.guard.enabledtofalse - Restart your bknd instance
- Complete the checklist above, then re-enable the Guard
Setup row based authentication
Follow these steps if you want to create a "role" that allows users to only edit their own user data:
- Create an user role
- Go to Auth → Roles → Create a new role
- In this case we name it "user"
- In section "Data Permissions" switch ON the toggles data.entity.update and data.entity.read
- For both click on the "settings" logo and past the following code in Condition
{
"entity": "users"
}- For Effect choose "filter" and past in the following code
{
"id": "@user.id"
}- Leave both the sections "Auth Permissions" and "Media Permissions" untouched
- In the section "System Permissions" switch ON all toggles except for system.build, system.info and system.config.write
-
Create a user (or use an existing)
- Go to Auth → Users → Create a new user
- Set up their email and password
-
Attach the user role to the created user
- Edit the user you just created
- Assign the "user" role to it
-
Sign in as a user
- Open an incognito/private browser window
- Navigate to your app and sign in with the user
- Confirm you have access to edit your own user data