# nodeauthapi

> This is an authapi lib. which can work as middleware for authentication. You can use it directly without creating model for database and controllers and session manager for this. You just config the database and forget.

Latest version **1.0.7** (published 2019-02-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install nodeauthapi
pnpm add nodeauthapi
yarn add nodeauthapi
bun add nodeauthapi
```

## 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 | 1.0.7 |
| Published | 2019-02-02 |
| First published | 2018-05-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 14.3 KB |
| Known vulnerabilities | 0 (+10 in 2 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Shubham Srivastava |
| Maintainers | anmolgi1401, shubham9170 |
| Keywords | express, auth, authentication |

## Links

- npm: https://www.npmjs.com/package/nodeauthapi
- Repository: https://github.com/Shubham9918/authApi
- Homepage: https://github.com/Shubham9918/authApi#readme
- Issues: https://github.com/Shubham9918/authApi/issues
- npm.io page: https://npm.io/package/nodeauthapi

## Dependencies (3)

- [bcrypt](https://npm.io/package/bcrypt.md) >=3
- [mongoose](https://npm.io/package/mongoose.md) ^5.0.15
- [jsonwebtoken](https://npm.io/package/jsonwebtoken.md) ^8.2.1

## Alternatives

- [@pothos/plugin-authz](https://npm.io/package/@pothos/plugin-authz.md) — 12.4K weekly downloads
- [@bounded-sh/client](https://npm.io/package/@bounded-sh/client.md) — 3.2K weekly downloads
- [@luigi-project/plugin-auth-oauth2](https://npm.io/package/@luigi-project/plugin-auth-oauth2.md) — 2.3K weekly downloads
- [@nocobase/plugin-verification](https://npm.io/package/@nocobase/plugin-verification.md) — 2.0K weekly downloads
- [escher-request](https://npm.io/package/escher-request.md) — 545 weekly downloads

## Recent versions

- 1.0.7 (latest) — 2019-02-02
- 1.0.6 — 2018-05-11
- 1.0.5 — 2018-05-11
- 1.0.4 — 2018-05-08
- 1.0.3 — 2018-05-08
- 1.0.2 — 2018-05-07
- 1.0.1 — 2018-05-06
- 1.0.0 — 2018-05-06

## README

# nodeAuthApi
This library use for authentication work as middleware. As of now you can add login feature to your app. 
Some feature like authenticate, manage authenticated URL and unauthenticated URL etc.. 
This API is totally based on local strategy (i.e. you can authenticate using only your database not 3rd party authentication).

## Install
```
    $ npm i nodeauthapi 
```

## Usage
##### Create application *(config)*
Before using this API you have to initialize any app. We are using the 
[Express](http://expressjs.com/en/starter/installing.html) framework for explaining this API. 
To initialize the Express app follow the following commands:
```
    $ express --version
    $ cd ~/Desktop
    $ express --view=ejs myapp
    $ cd myapp
    $ npm install
    $ npm install nodeauthapi
```

##### Configure database and authentication
For configuring the database and authentication API have a function [config()](./lib/manager.js) in 
[manager.js](./lib/manager.js). To configure the database pass fist parameter in as `config('db',options)` 4
and for authentication config pass `config('auth',options)`, `options` are different for both configuration.<br> 
Follow the following code:
```javascript
const nodeAuthApi = require('nodeauthapi');
//for database config
nodeAuthApi.config('db',{
    dbURI:'mongodb://127.0.0.1/test', //Your hosted mongodb URI,
    modelName:'users',//Name of model you want to create
    Schema : {
        //username, password and email are required
        username:{
            type : String,
            required:true
        },
        password:{
            type:String,
            required:true,
            bcrypt:true
        },
        email:{
            type:String,
            required:true
        },
        name:{
            type:String
        }
    } // Schema object is for database schema
});
    
//for authentication config
nodeAuthApi.config('auth',{
    sessionSecret : 'secret', // secret for json web token
    sessionExpirein : '1m' // session expire time
});
```
`Configure the API before configuring any routers.`
##### Authenticate Requests
Use `nodeAuthApi.authenticate()`, for authnticating the request. This function contain one parameter `options`
and it is object containing `successRediect, failureRedirect`.<br>
For example, as route middleware in an [Express](http://expressjs.com/en/starter/installing.html) application:
```javascript
app.post('/login', nodeAuthApi.authenticate({
    successRedirect : '/dashboard', // redirect to this route if successfully logged in
    failureRedirect : '/login' // redirect when fail to authenticate
}));
```
##### Other
Here we are discuss about other API like `logout()`,`isAuthenticated()`,`isNotAuthenticated()`:<br>
* Use of `logout()`
```javascript
//For logout the session
app.get('/logout',nodeAuthApi.logout({
    redirectURI:'/login' // URI for redirecting after logout
}));
```
* Use of `isAuthenticated()`<br>
```javascript
//For verifying that user are authenticated are not
app.get('/dashboard',nodeAuthApi.isAuthenticated({
    failureRedirect : '/login' // redirect user when not authenticated
}),(req, res, next)=>{
    res.send('Protected page'); //User request jump to next middleware if user is authenticated
});
```
* Use of `isNotAuthenticated()`<br>
```javascript
//For verifying that user are already logged in OR not
app.get('/dashboard',nodeAuthApi.isNotAuthenticated({
    failureRedirect : '/dashboard' // redirect user when authenticated
}),(req, res, next)=>{
    res.send('Login Page'); //User request jump to next middleware if user is not authenticated
});
```
## License
[The MIT License](https://opensource.org/licenses/MIT)<br>
Copyright (c) 2018 Shubham Srivastava

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