Simple user middleware for Express
Opens up several api endpoints to post to for handling user login and registration.
No config implemented
const user = require('./middleware/user');
this.server.use(user(this.config.user));
{
"user": {
"enabled": false,
"algorithm": "HS512",
"secret": "p3sp8kCBeAXuby9Syexqsx9tZEGKUVPdW9uZ6tXwHcd8LuWWmryB9DYmKZWzpHgLuFyUnjPBQE8L82rgrCHYAL45vpPa9Ng",
"expiresIn": "24h"
}
}
* (POST) /api/user/register => executes bootstrap.config.user.register(req, success, failed);
* (POST) /api/user/activate => executes bootstrap.config.user.activate(req, success, failed);
* (POST) /api/user/login => executes bootstrap.config.user.login(req, success, failed);
* (POST) /api/user/forgot-password => executes bootstrap.config.user.forgotPassword(req, success, failed);
* (POST) /api/user => executes bootstrap.config.user.validate(valid);
/**
* Login is executed whenever you post to <code>/api/user/login</code>. Pass the required user data in the
* success function, which is then tokenized using JSON Web tokens (JWT) and passed to the frontend.
* req is passed for the developer to get the POST body and validate the login.
*/
bootstrap.config.user.login = (req, success, failed) => {
// Handle login by developer
const user = { username: 'user.name@dpdk.com' };
if (user) return success(1011, { user });
return failed(5012);
};
/**
* Register is executed whenever you post to <code>/api/user/register</code>. The request body can be
* used by the developer to check credentials etc, and eventually return the success() or failed() function.
* Using these function standardizes the result passed to the frontend.
*/
bootstrap.config.user.register = (req, success, failed) => {
// Handle registration by developer...
const registered = registerAnUser();
if (registered) return success(1011, { userDataForTheFrontend: { username: 'user.name@dpdk.com' }});
return failed(5030);
};
/**
* Activate is executed whenever you post to <code>/api/user/activate</code>. The request body can be
* used by the developer to activate an account based on the activation key, and eventually return the success() or failed() function.
* Using these function standarizes the result passed to the frontend.
*/
bootstrap.config.user.activate = (req, success, failed) => {
// Handle activation by developer...
const activated = activateAnUser();
if (activated) return success(1010, { userDataForTheFrontend: { username: 'user.name@dpdk.com' }});
return failed(5030);
};
/**
* Validate is executed whenever you post to <code>/api/user</code>. The token from the headers (req.headers.jwt)
* or the token in the body (req.body.token) will be used to validate the given token.
*/
bootstrap.config.user.validate = valid => {
// Not really a useful function, but the user token can be checked.
// Valid: true/false
};
/**
* Forgot password is executed whenever you post to <code>/api/user/forgot-password</code>.
*/
bootstrap.config.user.forgotPassword = (req, success, failed) => {
// Can be used to send a new activation mail
};
No links available
Last modified | Friday, April 30, 2021, 12:00:21 PM UTC |
Last author | Colin van Eenige |
Commit ID | 4c7a701 |