@flexiloans/auth_public v0.0.20
Auth & Authz middleware
auth
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_public
Usage
Importing and initialising the package
import FlexiAuth from '@flexiloans/auth_public';
<!-- Base URL is where the authentication server is hosted -->
<!-- SERVICE_NAME is the name of service installing this package -->
const AUTH_URL = "http://AUTH_URL";
const SERVICE_NAME = "FLKYC";
FlexiAuth.init(AUTH_URL);
Authenticaiton and Authorization will not work if the library is not initialised.
Middleware Functions
Use the authenticate
middleware to ensure that a user is authenticated before accessing a route.
import FlexiAuth from '@flexiloans/auth_public';
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_public';
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