1.0.0 • Published 7 months ago
mern-auth-service v1.0.0
A simple and efficient authentication package for MERN and Next.js applications.
Features
- JWT-based authentication
- Easy user registration and login
- Middleware to protect routes
- Supports environment variable configuration for
JWT_SECRET_KEY
andJWT_EXPIRATION
Installation
To install the package:
npm install mern-auth
## To use the package
-- Register a User
const { registerUser } = require('mern-auth');
const newUser = await registerUser(UserModel, 'username', 'password');
-- Login User
const { loginUser } = require('mern-auth');
const token = await loginUser(UserModel, 'username', 'password');
console.log('Token:', token);
-- Protect Routes with JWT Middleware
const { authenticateJWT } = require('mern-auth');
app.use('/protected-route', authenticateJWT, (req, res) => {
res.send('This is a protected route');
});
-- Logout
const { logout } = require('mern-auth');
app.post('/logout', (req, res) => {
logout(res);
});
1.0.0
7 months ago