1
0

Rearranged readme

This commit is contained in:
2021-05-26 19:16:25 +02:00
parent d99e361d65
commit af3ea4cf8d
3 changed files with 49 additions and 46 deletions

View File

@@ -2,11 +2,13 @@
A lightweight JWT implementation with ZERO dependencies for Cloudflare Workers.
## Contents
- [Install](#install)
- [Usage](#usage)
- [Examples](#examples)
- [Usage](#usage)
## Install
@@ -14,6 +16,48 @@ A lightweight JWT implementation with ZERO dependencies for Cloudflare Workers.
npm i @tsndr/cloudflare-worker-jwt
```
## Examples
### Basic Example
```javascript
async () => {
const jwt = require('@tsndr/cloudflare-worker-jwt')
// Creating a token
const token = await jwt.sign({ name: 'John Doe', email: 'john.doe@gmail.com' }, 'secret')
// Verifing token
const isValid = await jwt.verify(token, 'secret')
// Decoding token
const payload = jwt.decode(token)
}
```
### Restrict Timeframe
```javascript
async () => {
const jwt = require('@tsndr/cloudflare-worker-jwt')
// Creating a token
const token = await jwt.sign({
name: 'John Doe',
email: 'john.doe@gmail.com',
nbf: Math.floor(Date.now() / 1000) + (60 * 60), // Not before: Now + 1h
exp: Math.floor(Date.now() / 1000) + (2 * (60 * 60)) // Expires: Now + 2h
}, 'secret')
// Verifing token
const isValid = await jwt.verify(token, 'secret') // false
// Decoding token
const payload = jwt.decode(token) // { name: 'John Doe', email: 'john.doe@gmail.com', ... }
}
```
## Usage
<hr>
@@ -59,45 +103,4 @@ Argument | Type | Satus | Default | Description
`token` | `string` | required | - | The token string generated by `jwt.sign()`.
#### `return`
Returns payload `object`.
## Examples
### Basic Example
```javascript
async () => {
const jwt = require('@tsndr/cloudflare-worker-jwt')
// Creating a token
const token = await jwt.sign({ name: 'John Doe', email: 'john.doe@gmail.com' }, 'secret')
// Verifing token
const isValid = await jwt.verify(token, 'secret')
// Decoding token
const payload = jwt.decode(token)
}
```
### Restrict Timeframe
```javascript
async () => {
const jwt = require('@tsndr/cloudflare-worker-jwt')
// Creating a token
const token = await jwt.sign({
name: 'John Doe',
email: 'john.doe@gmail.com',
nbf: Math.floor(Date.now() / 1000) + (60 * 60), // Not before: Now + 1h
exp: Math.floor(Date.now() / 1000) + (2 * (60 * 60)) // Expires: Now + 2h
}, 'secret')
// Verifing token
const isValid = await jwt.verify(token, 'secret') // false
// Decoding token
const payload = jwt.decode(token) // { name: 'John Doe', email: 'john.doe@gmail.com', ... }
}
```
Returns payload `object`.