@marblejs/middleware-jwt
TypeScript icon, indicating that this package has built-in type declarations

3.5.2 • Public • Published

Marble.js logo

@marblejs/middleware-jwt

A JWT middleware for Marble.js.

Installation

$ npm i @marblejs/middleware-jwt

Requires @marblejs/core to be installed.

Usage

Generate token:

import { EffectFactory } from '@marblejs/core';
import { generateToken } from '@marblejs/middleware-jwt';
import { SECRET_KEY } from './config';

const login$ = EffectFactory
  .matchPath('/login')
  .matchType('POST')
  .use(req$ => req$.pipe(
    //
    map(payload => generateToken({ secret: SECRET_KEY })(payload)), 👈
    // ...
  ));

Validate payload:

const verifyPayload$ = (payload: { id: string }) =>
  of(payload).pipe(
    map(payload => payload.id),
    flatMap(UserRepository.findById),  // the repository can throw an error if not found or...
    catchError(/* ... */)              // the `verifyPayload$` can throw it explicitly
  );

Validate routes:

import { r } from '@marblejs/core';
import { authorize$ } from '@marblejs/middleware-jwt';
import { SECRET_KEY } from './config';

const getUsers$ = r.pipe(
  r.matchPath('/'),
  r.matchType('GET'),
  r.useEffect(req$ => req$.pipe(
    // ...
  )));

const user$ = combineRoutes('/user', {
  effects: [
    getUsers$
  ],
  middlewares: [
    authorize$({ secret: SECRET_KEY }, validatePayload$) 👈
  ],
});

License: MIT

Readme

Keywords

none

Package Sidebar

Install

npm i @marblejs/middleware-jwt

Weekly Downloads

0

Version

3.5.2

License

MIT

Unpacked Size

10.7 kB

Total Files

15

Last publish

Collaborators

  • jflakus