@flexiloans/auth v0.0.4
Auth & Authz middleware
auth_middleware_private
is an npm package designed to specifically authenticate and authorize APIs in Node.js applications. It provides two middleware functions: authenticate
and authorize
that can be easily integrated into your application to secure routes and resources.
We use JWT validations to authenticate routes.
Installation
Install the package using npm:
npm i @flexiloans/auth_middleware_private
Usage
Importing the package
import FlexiAuth from '@flexiloans/auth_middleware_private';
Middleware Functions
Use the authenticate
middleware to ensure that a user is authenticated before accessing a route.
import FlexiAuth from '@flexiloans/auth_middleware_private';
app.get('/secure-route', FlexiAuth.authenticate, (req, res) => {
res.send('You are authenticated!');
});
app.get('/public-route', (req, res) => {
res.send('This is a public route!');
});
NOTE - /public-route
does not have authenticate middleware
Use the authorize
middleware is used to check if a user has the necessary permissions to access a resource
import FlexiAuth from '@flexiloans/auth_middleware_private';
app.get('/secure-route', FlexiAuth.authenticate, FlexiAuth.authorize, (req, res) => {
res.send('You are authenticated!');
});
app.get('/public-route', (req, res) => {
res.send('This is a public route!');
});
Error Handling
If any error occurs during authentication or authorization, the middleware will call `next(error) to let you handle the errors as needed
4 months ago