# identity-sdk

> An authentication sdk for node.js applications using mongodb

Latest version **0.1.0** (published 2021-05-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install identity-sdk
pnpm add identity-sdk
yarn add identity-sdk
bun add identity-sdk
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2021-05-15 |
| First published | 2021-05-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 24.5 KB |
| Known vulnerabilities | 0 (+7 in 3 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Mostapha Rammo |
| Maintainers | rammom |
| Keywords | express, auth, identity, authn, authentication, sdk |

## Links

- npm: https://www.npmjs.com/package/identity-sdk
- Repository: https://github.com/rammom/identity-sdk
- Homepage: https://github.com/rammom/identity-sdk#readme
- Issues: https://github.com/rammom/identity-sdk/issues
- npm.io page: https://npm.io/package/identity-sdk

## Dependencies (4)

- [uuid](https://npm.io/package/uuid.md) ^8.3.2
- [bcryptjs](https://npm.io/package/bcryptjs.md) ^2.4.3
- [mongoose](https://npm.io/package/mongoose.md) ^5.12.9
- [jsonwebtoken](https://npm.io/package/jsonwebtoken.md) ^8.5.1

## 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

- 0.1.0 (latest) — 2021-05-15
- 0.0.12 — 2021-05-15
- 0.0.11 — 2021-05-15
- 0.0.10 — 2021-05-15
- 0.0.9 — 2021-05-15
- 0.0.8 — 2021-05-15
- 0.0.7 — 2021-05-15
- 0.0.6 — 2021-05-15
- 0.0.5 — 2021-05-15
- 0.0.4 — 2021-05-15
- 0.0.3 — 2021-05-15
- 0.0.2 — 2021-05-15
- 0.0.1 — 2021-05-15

## README

# Identity
Identity is an authentication sdk build for [Node.js](https://nodejs.org/en/) apps using [MongoDB](https://www.mongodb.com/).

Identity's purpose is to make authenticating and authorizing user's in your application as easy as possible. Just install the SDK, initialize it, and call `Identity.addUser()`
to add a new user to your application. The users are stored in your local database, giving you full control with maximum security.

## Install
```bash
$ npm install identity-sdk 
```

## Usage

#### Table of Contents

* [Adding Identity to your application](#adding-identity-to-your-application)
* [Log a user in](#log-a-user-in)
* [Log a user out](#log-a-user-out)
* [Create a new user](#create-a-new-user)
* [Get a new Access Token](#get-a-new-access-token)
* [License](#license)

### Adding Identity to your application

To add and initialize Identity to your application, just pass in your database information.

`dbHostname`: MongoDB hostname (ex. localhost)<br>
`dbPort`: MongoDB port (ex. 27017)<br>
`dbName`: MongoDB database name (ex. Identity)<br>
`initAdmin`: **(optional)** if `true` creates an admin user on init<br>
`dbConnectCallback`: **(optional)** function called once connected to the database<br>


```javascript
const Identity = require('identity-sdk').init(dbHostname, dbPort, dbName, initAdmin, dbConnectCallback);

// or

const Identity = require('identity-sdk');
Identity.init(dbHostname, dbPort, dbName, initAdmin, dbConnectCallback);
```

### Log a user in

By default, Identity adds an initial `admin` user to the database:
```javascript
const userCredentials = {
  username: 'admin',
  email: 'admin@example.com', // only a username OR an email is required, not both.
  password: 'admin'
};

const {
  accessToken,  // you would send this to your user (client)
  refreshToken, // for subsequent resource requests
  user
} = await Identity.loginUser(userCredentials);

// accessToken: String (JWT with user as payload)
// refreshToken: String (JWT with user as payload)
// user: Object (admin user document)
```

### Log a user out

```javascript
await Identity.logoutUser(refreshToken);

// or
const userCredentials = {
  username: 'admin',
  email: 'admin@example.com', // only a username OR an email is required, not both.
};

await Identity.logoutUserEverywhere(userCredentials);
```

### Validate and get user from an Access Token (JWT)

```javascript
const {
  user,
  validated
} = Identity.validateAccessToken(accessToken);

// user: Object (user data object), null if invalid jwt
// validated: Boolean (is access token valid)
```

### Create a new user

To create a new user, you just need to give Identity a **username** and **password**. 
The **username** and **email** field must be unique.
All other fields described are optional for your convenience.

```javascript
const newUserObject = {
  username,         // (required) (unique) User's username
  password,         // (required) User's plaintext password, gets hashed for you
  email,            // (unique) User's email
  name,             // User's full name
  given_name,       // User's given name
  family_name,      // User's family name
  nickname,         // User's nickname
  phone_number,     // User's phone number
  picture,          // URL pointing to the user's profile picture
};

newlyCreatedUserDocument = await Identity.createNewUser(newUserObject);

// newlyCreatedUserDocument: Object (new user document)
```

### Get a new Access Token

Access tokens expire, when they do pass in the refresh token to get a new one.

```javascript
const newAccessToken = await Identity.getNewAccessToken(refreshToken);
```

## License

[The MIT License](https://opensource.org/licenses/MIT)

Copyright (c) 2021 Mostapha Rammo <[mrammo.ca](https://mrammo.ca/)>

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