# auth-api

> generate auth api for you

Latest version **1.0.10** (published 2016-01-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install auth-api
pnpm add auth-api
yarn add auth-api
bun add auth-api
```

## 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.10 |
| Published | 2016-01-05 |
| First published | 2015-12-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 5 |
| Known vulnerabilities | 0 (+17 in 3 direct dependencies) |
| Install scripts | no |
| GitHub stars | 52 |
| Author | timqian |
| Maintainers | timqian |
| Keywords | jwt, express |

## Links

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

## Dependencies (5)

- [bcrypt](https://npm.io/package/bcrypt.md) ^0.8.5
- [nodemailer](https://npm.io/package/nodemailer.md) ^1.10.0
- [jsonwebtoken](https://npm.io/package/jsonwebtoken.md) ^5.4.1
- [es6-promisify](https://npm.io/package/es6-promisify.md) ^3.0.0
- [babel-polyfill](https://npm.io/package/babel-polyfill.md) ^6.3.14

## Alternatives

- [@clerk/clerk-expo](https://npm.io/package/@clerk/clerk-expo.md) — 133.6K weekly downloads
- [@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

## Recent versions

- 1.0.10 (latest) — 2016-01-05
- 1.0.9 — 2016-01-05
- 1.0.8 — 2015-12-31
- 1.0.7 — 2015-12-30
- 1.0.6 — 2015-12-30
- 1.0.5 — 2015-12-30
- 1.0.4 — 2015-12-30
- 1.0.2 — 2015-12-30
- 1.0.1 — 2015-12-30
- 1.0.0 — 2015-12-30

## README

## Purpose

Reuse authentication part code of REST server, easily and flexibly.
Thanks to `express.Router`.

## Features

- [jwt](https://github.com/auth0/node-jsonwebtoken) to verify user;
- [nodemailer](https://github.com/nodemailer/nodemailer) to send verification emails;
- [mongoose](https://github.com/Automattic/mongoose) to drive mongodb (user model: https://github.com/timqian/auth-api/blob/master/src/models/User.js);
- [axios](https://github.com/mzabriskie/axios) to test RESTful api(axios can be used both on browser and node, that means the test code can be reused in your web app);

## Sample usage:

1. Install `auth-api` and his peerDependencies:

  `npm install auth-api express body-parser mongoose --save`

2. Run the sample code below and boom~~ the auth server will be listening at `http://localhost:3000`

```javascript
var authApi        = require('auth-api');
var express        = require('express');
var bodyParser     = require('body-parser');
var mongoose       = require('mongoose');

mongoose.connect('mongodb://localhost/database'); // connect to database

var userConfig = {
  APP_NAME: 'STOCK APP',
  SECRET: 'ilovetim',                             // jwt secret
  CLIENT_TOKEN_EXPIRES_IN: 60 * 24 * 60 * 60,     // client token expires time(60day)
  EMAIL_TOKEN_EXPIRES_IN: 24 * 60 * 60,           // email token expires time(24h)

  EMAIL_SENDER: {                                 // used to send mail by nodemailer
    service: 'Gmail',
    auth: {
      user: 'qianlijiang123@gmail.com',
      pass: '321qianqian',
    }
  },

  USER_MESSAGE: {                                 // message sent to client
    MAIL_SENT: 'mail sent',
    NAME_TAKEN: 'Name or email has been taken',
    USER_NOT_FOUND: 'User not found',
    WRONG_PASSWORD: 'wrong password',
    LOGIN_SUCCESS: 'Enjoy your token!',
    NEED_EMAIL_VERIFICATION: 'You need to verify your email first',
  },

  API_URL: 'http://localhost:3000'              // to be used in the mail
};

authApi.init(userConfig);

var app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use('/', authApi.authRouter);

// protecting api
app.get('/needingToken', authApi.verifyToken, (req, res) => {

  // send back the jwt claim directly
  var claim = req.decoded;
  res.status(200).json(claim);
});

app.get('/needingTokenAndEmailVerified', authApi.verifyToken, (req, res) => {
  if (req.decoded.verified) {
    res.status(200).json(req.decoded);
  } else {
    res.status(400).json({
      success: false,
      message: 'Please verify your email before doing this!'
    });
  }
});


app.listen(3000);
console.log('API magic happens at http://localhost:3000');

// handle unhandled promise rejection
// https://nodejs.org/api/process.html#process_event_unhandledrejection
process.on('unhandledRejection', function(reason, p) {
    console.log('Unhandled Rejection at: Promise ', p, ' reason: ', reason);
    // application specific logging, throwing an error, or other logic here
});
```

(es6 sample: https://github.com/timqian/auth-api/blob/master/testServer.js)

## What does the above code do for you

1. Generate the following auth api for you at `http://localhost:3000`


|Method| url                 | data(if needed)                              | server action(if request is good) |
| ---- |---------------------| ---------------------------------------------| -------------|
| POST | /signup             | {name: ..., email: ..., password: ...}       |create a user in mongodb and send verification email |
| POST | /login              | {name/email: ..., password: ...}             |check user and return jwt token|
| POST | /password_reset     | {email: ..., password(the new password): ...}| send verification link to email |
| GET  | /email_verification |                                              | verify token and change password |

(more details in the code)


## Module api

- `authApi.init(config)`: configure the module
- `authApi.authRouter`: an express router I wrote for you
- `authApi.verifyToken`: an express middleware used to verify token sent by client

## TODOS

- [ ] better http status code
- [ ] better config params
- [ ] docs
- [ ] new feature

## license

  MIT

### As a starter see the [starter branch](https://github.com/timqian/auth-api/tree/jwtAuth-RESTful-server-starter-2.0)

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