Express JWT Authentication Router

2.0.3 · active · verified Wed Apr 22

edaten-auth is a plug-and-play JWT authentication router designed for Express applications that utilize MongoDB for data persistence via Mongoose. It provides out-of-the-box functionality for user registration, login, token refreshing, and logout, simplifying the implementation of common authentication flows. The current stable version is 2.0.3. While a specific release cadence isn't explicitly stated, the project appears actively maintained given its current versioning. Its primary differentiator is its "plug-and-play" nature, offering a complete, pre-built solution for JWT authentication with Express and Mongoose, requiring minimal configuration beyond environment variables for secrets and ensuring a MongoDB connection. It manages access tokens and refresh tokens, storing the latter securely in HTTP-only cookies.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates the basic setup of the `edaten-auth` router within an Express application, including essential middleware and a critical Mongoose connection step.

import express from "express";
import cookieParser from "cookie-parser";
import mongoose from "mongoose";
import createAuth from "edaten-auth";

const app = express();

app.use(express.json());
app.use(cookieParser());

// IMPORTANT: connect MongoDB BEFORE using auth routes
// In a real application, ensure process.env.MONGO_URI is set.
await mongoose.connect(process.env.MONGO_URI ?? 'mongodb://localhost:27017/myauthdb');

app.use("/auth", createAuth({
  jwtSecret: process.env.JWT_SECRET ?? 'supersecretjwtkey',
  jwtRefreshSecret: process.env.JWT_REFRESH_SECRET ?? 'anothersupersecretrefreshkey',
  requiredFields: ["email"],
  loginField: "email"
}));

app.get('/', (req, res) => res.send('Welcome! Auth routes available at /auth'));

app.listen(3000, () => console.log('Server running on port 3000'));

view raw JSON →