# vue-jwt-mongo

> A simple authentication system for Vue.js

Latest version **1.7.3** (published 2023-07-31) · MIT license · 0 weekly downloads

## Install

```sh
npm install vue-jwt-mongo
pnpm add vue-jwt-mongo
yarn add vue-jwt-mongo
bun add vue-jwt-mongo
```

## 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.7.3 |
| Published | 2023-07-31 |
| First published | 2016-08-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 9 |
| Unpacked size | 14.9 KB |
| Known vulnerabilities | 0 (+13 in 3 direct dependencies) |
| Install scripts | no |
| GitHub stars | 14 |
| Author | Pavel Dubov |
| Maintainers | dubov94 |
| Keywords | vue, jwt, jsonwebtoken, express, mongo, mongodb, auth, authentication |

## Links

- npm: https://www.npmjs.com/package/vue-jwt-mongo
- Repository: https://github.com/dubov94/vue-jwt-mongo
- Homepage: https://github.com/dubov94/vue-jwt-mongo#readme
- Issues: https://github.com/dubov94/vue-jwt-mongo/issues
- npm.io page: https://npm.io/package/vue-jwt-mongo

## Dependencies (9)

- [merge](https://npm.io/package/merge.md) 2.1.1
- [mongoose](https://npm.io/package/mongoose.md) 6.11.3
- [passport](https://npm.io/package/passport.md) 0.4.1
- [jwt-decode](https://npm.io/package/jwt-decode.md) 2.2.0
- [body-parser](https://npm.io/package/body-parser.md) 1.20.1
- [express-jwt](https://npm.io/package/express-jwt.md) 8.2.1
- [jsonwebtoken](https://npm.io/package/jsonwebtoken.md) 9.0.0
- [passport-local](https://npm.io/package/passport-local.md) 1.0.0
- [passport-local-mongoose](https://npm.io/package/passport-local-mongoose.md) 6.0.1

## Alternatives

- [angular-pipes](https://npm.io/package/angular-pipes.md) — 5.6K weekly downloads
- [@ng-web-apis/midi](https://npm.io/package/@ng-web-apis/midi.md) — 2.6K weekly downloads
- [happn-3](https://npm.io/package/happn-3.md) — 1.6K weekly downloads
- [@opensip-cli/lang-go](https://npm.io/package/@opensip-cli/lang-go.md) — 1.2K weekly downloads
- [@temporal-architect/claude-plugin](https://npm.io/package/@temporal-architect/claude-plugin.md) — 335 weekly downloads

## Recent versions

- 1.7.3 (latest) — 2023-07-31
- 1.7.2 — 2022-12-28
- 1.6.8 — 2022-10-29
- 1.6.5 — 2022-04-02
- 1.6.4 — 2021-05-13
- 1.6.3 — 2020-09-06
- 1.6.2 — 2020-07-25
- 1.6.1 — 2020-07-06
- 1.5.0 — 2020-03-29
- 1.4.7 — 2020-01-12
- 1.4.5 — 2019-10-27
- 1.4.3 — 2019-10-20
- 1.4.2 — 2019-06-08
- 1.4.1 — 2019-05-27
- 1.3.6 — 2017-09-17
- … 12 more at https://npm.io/package/vue-jwt-mongo/versions

## README

# vue-jwt-mongo

[![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/dubov94/vue-jwt-mongo/blob/master/LICENSE)
[![Coverage](https://codecov.io/gh/dubov94/vue-jwt-mongo/branch/master/graph/badge.svg)](https://codecov.io/gh/dubov94/vue-jwt-mongo)

A [package](https://www.npmjs.com/package/vue-jwt-mongo) for bootstrapping a simple [JSON Web Token](https://jwt.io/)-based authentication system using [Vue.js](https://vuejs.org/), [MongoDB](https://www.mongodb.com/) and [Express.js](https://expressjs.com/).

## Installation

```bash
npm install vue-jwt-mongo --save
```

## Server

```javascript
const app = require('express')()

const vjmServer = require('vue-jwt-mongo').Server({
  mongoUrl: 'mongodb://localhost/db',
  jwtSecret: 'shhh'
})
```

### Options

* `mongoUrl` (__mandatory__): an address of the Mongo database.
  * See [`mongoose.createConnection`](http://mongoosejs.com/docs/api.html#index_Mongoose-createConnection) for details.
* `jwtSecret` (__mandatory__): a secret key for token generation.
  * One can get such a key [here](https://www.grc.com/passwords.htm).
  * See [`jsonwebtoken.sign`](https://www.npmjs.com/package/jsonwebtoken#jwtsignpayload-secretorprivatekey-options-callback) for details.
* `userModelName`: a name for the [mongoose](http://mongoosejs.com) model storing encoded user credentials.
  * Defaults to `'User'`.
* `jwtExpiresIn`: token expiration time in seconds.
  * Defaults to `7 * 24 * 60 * 60` (one week).
  * See [`jsonwebtoken.sign`](https://www.npmjs.com/package/jsonwebtoken#jwtsignpayload-secretorprivatekey-options-callback) for details.

### Endpoints

#### registerHandler

Expects `{ username, password }` in the request body. Returns an empty response.

The password is salted and hashed via [passport-local-mongoose](https://npmjs.com/package/passport-local-mongoose).
```javascript
app.post('/auth/register', vjmServer.registerHandler)
```

#### loginHandler

Expects `{ username, password }` in the request body. Returns a string &mdash; the token.

```javascript
app.post('/auth/login', vjmServer.loginHandler)
```

#### refreshHandler

Expects an empty request body and `Authorization: Bearer {token}` as one of the HTTP headers. Returns a string with a new token if the original token is valid.

```javascript
app.post('/auth/refresh', vjmServer.refreshHandler)
```

### Protector

`jwtProtector` ensures that the incoming request has a valid token. Expects `Authorization: Bearer {token}` as one of the HTTP headers.

```javascript
app.get('/protected', vjmServer.jwtProtector, (request, response) => {
    console.log(request.user.username)
})
 ```

## Client

```javascript
Vue.use(require('vue-resource'))
Vue.use(require('vue-jwt-mongo').Client, {
  /* options, if any */
})
```

### Options

* `registerEndpoint`: the server's endpoint for registration requests.
  * Defaults to `'/auth/register'`.
* `loginEndpoint`: the server's endpoint for authentication requests.
  * Defaults to `'/auth/login'`.
* `refreshEndpoint`: the server's endpoint for refreshing the token.
  * Defaults to `'/auth/refresh'`.
* `storageKey`: a [localStorage](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage) key used for saving the token.
  * Defaults to `'jsonwebtoken'`.
* `bearerLexem`: a lexem prepending tokens in [`Authorization`](https://developer.mozilla.org/en/docs/Web/HTTP/Headers/Authorization) headers.
  * Defaults to `'Bearer '` (extra space intended).

### Requests

### Authentication

All of the following requests return [vue-resource](https://github.com/pagekit/vue-resource) Promises, so one can get an idea of the callback structure [here](https://github.com/pagekit/vue-resource/blob/master/docs/http.md#response).

```javascript
this.$auth.register('login', 'password')
```

```javascript
this.$auth.logIn('login', 'password')
```

```javascript
this.$auth.refresh()
```

### Authorization
If `bearer: true` is passed then `Authorization: Bearer {token}` is added as a [header](https://developer.mozilla.org/en/docs/Web/HTTP/Headers/Authorization).

```javascript
this.$http.get('/protected', { bearer: true }).then(response => {
    console.log(response)
})
```

### Token

#### isLoggedIn

Returns `true` if the saved token is valid and `false` otherwise.

```javascript
let isLoggedIn = this.$auth.isLoggedIn()
```

#### getToken

Returns a string if the saved token is valid and `null` otherwise.

```javascript
this.$auth.getToken()
```

#### logOut

Purges the saved token.

```javascript
this.$auth.logOut()
```

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