@jestanislao/brewery-auth

2.5.0 • Public • Published

brewery-auth

npm version

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')
})

License

ISC

Readme

Keywords

none

Package Sidebar

Install

npm i @jestanislao/brewery-auth

Weekly Downloads

2

Version

2.5.0

License

ISC

Unpacked Size

8.35 kB

Total Files

8

Last publish

Collaborators

  • jestanislao