# node-auth-guard

> Module for authorization

Latest version **2.4.0** (published 2019-02-13) · ISC license · 0 weekly downloads

## Install

```sh
npm install node-auth-guard
pnpm add node-auth-guard
yarn add node-auth-guard
bun add node-auth-guard
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.4.0 |
| Published | 2019-02-13 |
| First published | 2018-09-18 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 16.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Nazar Lutsyk |
| Maintainers | nazar_lutsyk |
| Keywords | auth, authorization, guard, node-auth-guard, express-authorization |

## Links

- npm: https://www.npmjs.com/package/node-auth-guard
- Repository: https://github.com/NazarLutsyk/node-auth-guard
- Homepage: https://github.com/NazarLutsyk/node-auth-guard#readme
- Issues: https://github.com/NazarLutsyk/node-auth-guard/issues
- npm.io page: https://npm.io/package/node-auth-guard

## Recent versions

- 2.4.0 (latest) — 2019-02-13
- 2.3.0 — 2019-01-15
- 2.2.1 — 2018-09-20
- 2.2.0 — 2018-09-20
- 2.0.0 — 2018-09-20
- 1.0.0 — 2018-09-18

## README

#Node Auth Guard
###Small and flexible library for authorization

````javascript
let express = require('express');
let authGuard = require('node-auth-guard');
let app = express();

app.use((req, res, next) => {

    //add manually some principal
    req.some = {
        path: {
            to: {
                principal: {
                    name: 'some name',
                    surname: 'some surname',

                    //field with roles of principal
                    roles: ['USER', 'ADMIN']
                    //or you can specify only one => role: 'ADMIN'
                }
            }
        }
    };
    next();
});

//initialize authorization
app.use(authGuard.initialize({

    //path in the req object which will be indicate principal field
    //default value "user"
    principalPath: 'some.path.to.principal',

    //name of field which will be indicate principal roles field
    //default value "roles"
    rolesField: 'roles'
    
}));

//route opened for users that have "ADMIN" or "USER" roles
app.get('/', authGuard.roles('USER', 'ADMIN'), (req, res, next) => {
    res.end('Hello!');
});

//route opened for users that have a name "Rick"
app.get('/',
    authGuard.rule(
        (req, res, next) => {
        
            //do some checks
            //if all ok you may call next() otherwise send error
            if (req.some.path.to.principal.name === 'Rick') {
                return next()
            }else {
                return next(new Error('Forbidden!'));
            }

        }
    ),
    (req, res, next) => {
        res.end('Hello!');
    }
);

//route opened for users that have "ADMIN" or "SUPER_ADMIN" roles
//or
//they performing the rule
app.get('/',
    authGuard.rule(
        (req, res, next) => {/*do some checks...*/},
       
        //you can specify roles for which rule will not be executed
        'ADMIN',
        'SUPER_ADMIN'
    ),
    (req, res, next) => {
        res.end('Hello!');
    }
);

app.listen(3000);
````
#### Standart rules
````javascript
    //only for authenticated users
    app.get(
        '/', 
        authGuard.rules.isAuthenticated,     
        (req, res, next) => {
            res.end('Hello!');
        }
    );

    //only for NOT authenticated users
    app.get(
        '/', 
        authGuard.rules.isNotAuthenticated,     
        (req, res, next) => {
            res.end('Hello!');
        }
    );
````

---
_Source: https://npm.io/package/node-auth-guard · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
