> For the complete documentation index, see [llms.txt](https://marblejs.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://marblejs.gitbook.io/docs/other/api-reference/middleware-jwt/token-creation.md).

# Token signing

## + **generateToken**

The middleware wraps auth0 [jsonwebtoken](https://github.com/auth0/node-jsonwebtoken) API into more RxJS friendly functions that can be partially applied and composed inside Observable streams.

**generateToken** signs new JWT token with provided payload and configuration object which defines the way how the token is signed.&#x20;

### Importing

```typescript
import { generateToken } from '@marblejs-contrib/middleware-jwt';
```

### Type declaration

```
generateToken :: GenerateOptions -> Payload -> string
```

### Parameters

| *parameter* | definition                             |
| ----------- | -------------------------------------- |
| *options*   | `GenerateOptions`                      |
| *payload*   | `Payload = string \| object \| Buffer` |

{% tabs %}
{% tab title="GenerateOptions" %}
Config object which defines a set of parameters that are used for token signing.

| *parameter*   | definition                       |
| ------------- | -------------------------------- |
| *secret*      | `string \| Buffer`               |
| algorithm     | \<optional> `string`             |
| keyid         | \<optional> `string`             |
| expiresIn     | \<optional> `string \| number`   |
| notBefore     | \<optional> `string \| number`   |
| audience      | \<optional> `string \| string[]` |
| subject       | \<optional> `string`             |
| issuer        | \<optional> `string`             |
| jwtid         | \<optional> `string`             |
| noTimestamp   | \<optional> `boolean`            |
| header        | \<optional> `object`             |
| encoding      | \<optional> `string`             |
| {% endtab %}  |                                  |
| {% endtabs %} |                                  |

{% hint style="info" %}
For more details about JWT token signing, please visit [jsonwebtoken package docs](https://github.com/auth0/node-jsonwebtoken).
{% endhint %}

## + **generateExpirationInHours**

The standard for JWT defines an `exp` claim for expiration. The expiration is represented as a **NumericDate.** This means that the expiration should contain the number of seconds since the epoch.

**generateExpiratinoInHours** is a small, but handy function that returns an numeric date for given hours as a parameter. If the function is called without any parameter then the date is generated with 1 hour expiration.

### Importing

```typescript
import { generateExpirationInHours } from '@marblejs-contrib/middleware-jwt';
```

### Type declaration

```
generateExpirationInHours :: number -> number
```

## **Example**

{% code title="token.helper.ts" %}

```typescript
export const generateTokenPayload = (user: User) => ({
  id: user.id,
  email: user.email,
  exp: generateExpirationInHours(4), 
  // 👆 token will expire within the next 4 hours
});
```

{% endcode %}

{% code title="login.effect.ts" %}

```typescript
import { r, HttpError, HttpStatus } from '@marblejs/http';
import { throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { generateTokenPayload } from './token.helper';

const login$ = r.pipe(
  r.matchPath('/login'),
  r.matchType('POST'),
  r.useEffect(req$ => req$.pipe(
    map(req => req.body),
    mergeMap(UserDao.findByCredentials),
    map(generateTokenPayload),
    // 👇
    map(generateToken({ secret: Config.jwt.secret })),
    map(token => ({ body: { token } })),
    catchError(() => throwError(() =>
      new HttpError('Unauthorized', HttpStatus.UNAUTHORIZED)
    )),
  )));
```

{% endcode %}
