80 lines
2.0 KiB
Markdown
80 lines
2.0 KiB
Markdown
# Cloudflare Worker JWT
|
|
|
|
A lightweight JWT implementation with ZERO dependencies for Cloudflare Workers.
|
|
|
|
## Contents
|
|
|
|
- [Usage](#usage)
|
|
- [Install](#install)
|
|
|
|
## Usage
|
|
|
|
### Simple Example
|
|
|
|
```javascript
|
|
const jwt = require('@tsndr/cloudflare-worker-jwt')
|
|
|
|
// Creating a token
|
|
const token = jwt.sign({ name: 'John Doe', email: 'john.doe@gmail.com' }, 'secret')
|
|
|
|
// Verifing token
|
|
const isValid = jwt.verify(token, 'secret')
|
|
|
|
// Decoding token
|
|
const payload = jwt.decode(token)
|
|
|
|
```
|
|
|
|
<hr>
|
|
|
|
### `jwt.sign(payload, secret, [algorithm])`
|
|
|
|
Signs a payload and returns the token.
|
|
|
|
#### Arguments
|
|
|
|
Argument | Type | Satus | Default | Description
|
|
----------- | -------- | -------- | ------- | -----------
|
|
`payload` | `object` | required | - | The payload object
|
|
`secret` | `string` | required | - | A string which is used to sign the payload.
|
|
`algorithm` | `string` | optional | `HS256` | The algorithm used to sign the payload, possible values: `HS256` or `HS512`
|
|
|
|
#### `return`
|
|
returns token as a `string`
|
|
|
|
<hr>
|
|
|
|
### `jwt.verify(token, secret, [algorithm])`
|
|
|
|
Verifies the integrity of the token and returns a boolean value.
|
|
|
|
Argument | Type | Satus | Default | Description
|
|
----------- | -------- | -------- | ------- | -----------
|
|
`token` | `string` | required | - | The token string generated by `jwt.sign()`.
|
|
`secret` | `string` | required | - | The string which was used to sign the payload.
|
|
`algorithm` | `string` | optional | `HS256` | The algorithm used to sign the payload, possible values: `HS256` or `HS512`
|
|
|
|
#### `return`
|
|
returns `boolean`
|
|
|
|
<hr>
|
|
|
|
### `jwt.decode(token)`
|
|
|
|
Returns the payload without verifying the integrity of the token.
|
|
|
|
Argument | Type | Satus | Default | Description
|
|
----------- | -------- | -------- | ------- | -----------
|
|
`token` | `string` | required | - | The token string generated by `jwt.sign()`.
|
|
|
|
#### `return`
|
|
returns payload `object`
|
|
|
|
<hr>
|
|
|
|
|
|
## Install
|
|
|
|
```
|
|
npm i @tsndr/cloudflare-worker-jwt
|
|
``` |