Production-Ready Authentication v5.7.1

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.

PostgreSQL & SQLite Dual DB PBKDF2 & TOTP 2FA RFC 8628 Device Flow
server.js Node.js ES6
import express from "express";
import mbkauthe, { sessVal, roleChk } from "mbkauthe";
const app = express();
// 1. Mount authentication core router
app.use(mbkauthe);
// 2. Protect session routes
app.get("/dashboard", sessVal, (req, res) => {
res.send(`Welcome ${req.session.user.username}!`);
});
// 3. Enforce role-based access
app.get("/admin", sessVal, roleChk("superadmin"), (req, res) => {
res.send("Admin Control Panel");
});
app.listen(3000);
Ready in <10 LOC View More Examples →

Dual-DB

PostgreSQL & SQLite

PBKDF2

Cryptographic Hashing

RFC 8628

CLI Device Auth Flow

100%

Open-Source & MIT

Core Features

Built for developers, engineered for security

Everything your Node.js application needs for authentication, session verification, and access delegation.

PostgreSQL + SQLite

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.

AES Encrypted

Robust Session Engine

Encrypted session cookies, automatic database validation, session fixation prevention, and configurable multi-session limits per user.

Express Middleware

Granular Role-Based Access (RBAC)

Drop-in middleware helpers (sessVal, roleChk, sessRole, strictSessRole) for superadmin, normaluser, member, and guest role enforcement.

RFC 6238

Two-Factor Auth (TOTP)

Time-based One-Time Passwords compatible with Google Authenticator, Authy, and 1Password, featuring trusted device remember periods.

Social Login

GitHub & Google OAuth

Native GitHub App and Google OAuth2 integration with unified user account linking and shared credential support for microservices.

Bearer mbk_*

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.

Dual-Database Layer

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 withTransaction support.
  • FIFO Transaction Mutex: Eliminates write contention deadlocks in SQLite WAL mode.

PostgreSQL

Production & Serverless

Ideal for multi-instance clusters, Neon Postgres, Supabase, and AWS RDS. Powered by pg.Pool.

DB_TYPE=postgres
LOGIN_DB=postgresql://...

SQLite

Dev, Test & Embedded

Zero database server needed. High performance Write-Ahead Logging (WAL) via better-sqlite3.

DB_TYPE=sqlite
SQLITE_PATH=./data/app.db
Integration Patterns

Simple, declarative API integration

From Express middleware to custom database repositories, MBKAuthe fits naturally into modern Node.js applications.

routes/admin.js Role-based protection
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;
repositories/ProjectRepository.js BaseRepository & Transactions
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"]
      );
    });
  }
}
cli/auth.js RFC 8628 Device Flow Client
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;
  }
}
scripts/deploy.js Bearer Token Verification
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}`);
  }
}

Ready to secure your Node.js application?

Get started with MBKAuthe in minutes. Install via npm, run the database schema generator, and protect your routes.