Authentication for Node.js, Simplified.
A flexible authentication and session engine for Express.js with dual-database support for PostgreSQL and SQLite, TOTP 2FA, OAuth social logins, API tokens, and RFC 8628 CLI device flows.
npm install mbkauthe
Dual-DB
PostgreSQL & SQLite
PBKDF2
Cryptographic Hashing
RFC 8628
CLI Device Auth Flow
100%
Open-Source & MIT
Built for developers, engineered for security
Everything your Node.js application needs for authentication, session verification, and access delegation.
Dual-Database Architecture
First-class support for both PostgreSQL (connection pooled) and SQLite (via better-sqlite3 with WAL mode and FIFO mutex). Switch effortlessly with zero code changes.
Robust Session Engine
Encrypted session cookies, automatic database validation, session fixation prevention, and configurable multi-session limits per user.
Granular Role-Based Access (RBAC)
Drop-in middleware helpers (sessVal, roleChk, sessRole, strictSessRole) for superadmin, normaluser, member, and guest role enforcement.
Two-Factor Auth (TOTP)
Time-based One-Time Passwords compatible with Google Authenticator, Authy, and 1Password, featuring trusted device remember periods.
GitHub & Google OAuth
Native GitHub App and Google OAuth2 integration with unified user account linking and shared credential support for microservices.
API Tokens & RFC 8628 CLI Auth
SHA-256 hashed API tokens with read-only/write scopes, plus a browser-based CLI device flow for seamless command-line logins.
PostgreSQL or SQLite.
Zero code changes.
Whether you need high-concurrency connection-pooled PostgreSQL for cloud production or zero-config SQLite with Write-Ahead Logging (WAL) for local dev and embedded apps, MBKAuthe provides a unified driver abstraction layer.
-
Runtime SQL Translation: Converts Postgres
$1,ANY(), and typecasts to SQLite dynamically. -
BaseRepository: Extensible query builder with atomic
withTransactionsupport. - FIFO Transaction Mutex: Eliminates write contention deadlocks in SQLite WAL mode.
PostgreSQL
Ideal for multi-instance clusters, Neon Postgres, Supabase, and AWS RDS. Powered by pg.Pool.
LOGIN_DB=postgresql://...
SQLite
Zero database server needed. High performance Write-Ahead Logging (WAL) via better-sqlite3.
SQLITE_PATH=./data/app.db
Simple, declarative API integration
From Express middleware to custom database repositories, MBKAuthe fits naturally into modern Node.js applications.
import { Router } from "express";
import { sessRole, strictSessRole } from "mbkauthe";
const router = Router();
// 1. Standard role check (accepts active session cookie or API token)
router.get("/reports", sessRole("normaluser"), (req, res) => {
res.json({ reports: [] });
});
// 2. Strict cookie-only check for sensitive superadmin actions (rejects API tokens)
router.post("/wipe-database", strictSessRole("superadmin"), async (req, res) => {
res.json({ success: true, message: "Action authorized" });
});
export default router;
import { BaseRepository, defaultAdapter } from "mbkauthe";
export class ProjectRepository extends BaseRepository {
constructor(adapter = defaultAdapter) {
super(adapter);
}
async transferOwnership(projectId, newOwnerId) {
return await this.withTransaction(async (txRepo) => {
await txRepo.query(
"UPDATE projects SET owner_id = $1 WHERE id = $2",
[newOwnerId, projectId]
);
await txRepo.query(
"INSERT INTO audit_logs (project_id, action) VALUES ($1, $2)",
[projectId, "TRANSFERRED_OWNERSHIP"]
);
});
}
}
const init = await fetch("https://portal.mbktech.org/api/cli/device", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ client_name: "my-cli", profile_key: "1362403658a3" })
}).then(r => r.json());
console.log(`Open to approve:\n ${init.verification_url}\n`);
// Poll for issued token
for (;;) {
await sleep(init.interval * 1000);
const res = await fetch("https://portal.mbktech.org/api/cli/device/token", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ device_code: init.device_code })
}).then(r => r.json());
if (res.status === "approved") {
console.log(`Logged in as ${res.username}. Token: ${res.token}`);
break;
}
}
import fetch from "node-fetch";
async function queryProtectedApi(token) {
const res = await fetch("https://portal.mbktech.org/api/tokens/verify", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ token })
});
const data = await res.json();
if (data.tokenValid) {
console.log(`Valid token for user: ${data.username}`);
console.log(`Allowed scopes: ${data.permissions.scope}`);
}
}
Explore MBKAuthe documentation
Comprehensive guides, architecture deep dives, and full API endpoint documentation.
Installation & Setup
Prerequisites, package installation, environment templates, and table creation.
Start GuideEnvironment Config
mbkautheVar vs mbkauthShared, case-insensitivity, proxy mappings, and defaults.
View ConfigPostgreSQL & SQLite
Dual database architecture, BaseRepository, and translatePgToSqlite engine.
Read ArchitectureAPI Reference
Full catalog of REST endpoints, authentication methods, payloads, and rate limits.
Browse APIReady to secure your Node.js application?
Get started with MBKAuthe in minutes. Install via npm, run the database schema generator, and protect your routes.