credguard
v0.0.11
Published
A simple credentials system
Readme
CredGuard
CredGuard is a module to help you handle credentials in your scripts. It provides a simple way to store and retrieve credentials in a secure way, alongside with middlewares and routes that handle the tedious authentication process.
Disclaimer
This project is still in development and should not be used in production environments.
How to use
A very simple usage of CredGuard is shown below:
import express from 'express';
import { CredGuard, UserTypes } from 'credguard';
const app = express();
app.use(express.json());
const credGuard = new CredGuard();
credGuard.isReady.then(() => {
credGuard.SetExpress(app);
});
app.get('/protected', credGuard.MiddlewareIsAuthenticated.bind(credGuard), (req, res) => {
res.send('Protected resource');
});
app.get('/role-user', credGuard.MiddlewareRoleFilter([UserTypes.Role.USER]), (req, res) => {
res.send('User role resource');
});
app.get('/role-admin', credGuard.MiddlewareRoleFilter([UserTypes.Role.ADMIN]), (req, res) => {
res.send('Admin role resource');
});
app.listen(3000, () => console.log(`Listening on port 3000`));With this setup, you can make requests to:
- POST http://localhost:3000/login
- POST http://localhost:3000/refresh-token
- POST http://localhost:3000/register
- GET http://localhost:3000/protected
- GET http://localhost:3000/role-user
- GET http://localhost:3000/role-admin
The payload for the login and register routes should be:
{
"email": "<email>",
"password": "<password>"
}This will generate a JWT token that will be used to authenticate the user in the protected routes. With the expired token, you can use the refresh-token route to generate a new token.
