@jestanislao/brewery-auth
v2.5.0
Published
keycloak authentication package for expressjs
Downloads
4
Readme
brewery-auth
Authentication middleware for Nodejs using keycloak that can be used on any express-based application.
Installation
npm install @jestanislao/brewery-auth
Usage
const express = require('express');
const app = express();
const bodyParser = require('body-parser')
const BreweryAuth = require('../src/index');
const brewery = new BreweryAuth();
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.get('/token', (req, res, next) => {
if (!req.body || !req.body.client_id || !req.body.credentials || !req.body.credentials.secret) {
return res.status(401).json({
status: 401,
error: 'error',
description: 'missing parameters'
});
};
brewery.getToken(req.body.client_id, req.body.credentials.secret)
.then(response => {
return res.status(200).json(response);
}
)
.catch(err => {
return res.status(401).json(err);
});
});
app.get('/all-user', brewery.authenticate(['admin', 'user']),(req,res, next) => {
res.json('Route for admin/user.');
})
app.get('/user', brewery.authenticate(['user']),(req,res, next) => {
res.json('Route for user.');
})
app.get('/admin', brewery.authenticate('admin'),(req,res, next) => {
res.json('Route for admin.');
})
app.listen('3000', () =>{
console.log('LISTENING AT PORT 3000')
})