@marblejs/middleware-jwt
v3.5.2
Published
JWT middleware for Marble.js
Downloads
18
Readme
@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