This is a simple authentication middleware package for Node.js applications.
You can install this package via npm:
npm install authentication-middleware-sec
const express = require('express');
const AuthenticationMiddleware = require('authentication-middleware-sec');
// Create an instance of AuthenticationMiddleware
const authMiddleware = new AuthenticationMiddleware();
// Register a new user
authMiddleware.register('john', 'password123');
// Create an Express application
const app = express();
// Use the authentication middleware for protected routes
app.use('/protected-route', authMiddleware.middleware);
// Define protected routes
app.get('/protected-route', (req, res) => {
res.send('You have accessed a protected route.');
});
// Start the server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
Registers a new user with the provided username and password.
Authenticates the user with the provided username and password. Returns true
if authentication succeeds, false
otherwise.
Middleware function to authenticate incoming requests. Checks for username and password in the request headers and validates them using the authenticate
method. If authentication succeeds, calls the next
function to proceed with the request; otherwise, sends a 401 Unauthorized response.
This package is open source and available under the MIT License.