- Written in TypeScript
- Published as both JS and TypeScript types
- Handles user authentication with JSON Web Tokens
- Provides middlewares for Express apps
- Easy to integrate
npm install base-auth-handler
Import the middlewares you need:
import { currentUser, requireAuth, validateRequest } from "base-auth-handler";
Populates req.currentUser
if a valid JWT token cookie exists.
app.use(currentUser);
Throws error if current user is not authenticated. Use to protect routes.
app.get("/profile", requireAuth, (req, res) => {
// can access req.currentUser
});
Validates request using express-validator. Useful for validating sign up data.
app.post("/signup", validators, validateRequest, (req, res) => {
// data has been validated
})
import express from "express";
import cookieParser from "cookie-parser";
import { currentUser, requireAuth, validateRequest } from "base-auth-handler";
const app = express();
app.use(cookieParser());
app.get("/profile", currentUser, requireAuth, (req, res) => {
res.send(req.currentUser);
});
app.post("/signup",
[
// validators
],
validateRequest,
(req, res) => {
// create user
}
);
The middlewares handle user authentication and validation - the app code stays clean.
Feel free to contribute to this project by reporting issues or creating pull requests. For major changes, please open an issue first to discuss the proposed changes.
This module is licensed under the MIT License - see the LICENSE file for details.