Skip to content
This repository was archived by the owner on May 17, 2019. It is now read-only.

fusionjs/fusion-plugin-jwt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

fusion-plugin-jwt

Build status

Session library that uses JSON Web Token and cookies


Table of contents


Installation

yarn add fusion-plugin-jwt

Usage

export default createPlugin({ deps: {Session: SessionToken}, middleware() { return ({Session}) => { return async (ctx, next) => { const session = Session.from(ctx); session.set('some-key', 'some-value'); const someValue = session.get('some-key'); return next(); } }); } });

Setup

// src/main.js import React from 'react'; import App from 'fusion-react'; import JWTSession, { SessionSecretToken, SessionCookieNameToken, SessionCookieExpiresToken } from 'fusion-plugin-jwt'; import {SessionToken} from 'fusion-tokens'; export default () => { const app = new App(); // ... if (__NODE__) { app.register(SessionToken, JWTSession); app.register(SessionSecretToken, 'some-secret'); // required app.register(SessionCookieNameToken, 'some-cookie-name'); // required app.register(SessionCookieExpiresToken, 86400); // optional } // ... return app; }

API

Registration API

Session
import Session from 'fusion-plugin-jwt';

The plugin. Should typically be registered to SessionToken

SessionToken

Typically should be registered with Session. See https://github.com/fusionjs/fusion-tokens#sessiontoken

Dependencies

SessionSecretToken
import {SessionSecretToken} from 'fusion-plugin-jwt';

Required. A secret for encrypting the JWT token / cookie. Can typically be a random static value.

Types
type Secret = string; 
SessionCookieNameToken
import {SessionCookieNameToken} from 'fusion-plugin-jwt';

Required. A cookie name

Types
type CookieName = string; 
SessionCookieExpiresToken
import {SessionCookieExpiresToken} from 'fusion-plugin-jwt';

Required. An expiration time in seconds.

Types
type CookieName = number; 

Service API

const session: Session = Session.from((ctx: Context));
Types
type Session = { set: (key: string, value: Object | Array | string | number | boolean) => any, get: (key: string) => any, };

session.set

const value = session.set(key:string, val:Object|Array|string|number|boolean); 
  • key: string - Required
  • val: Object|Array|string|number|boolean - A serializable value. Required
  • returns value: any

session.get

const value: any = session.get(key: string); 
  • key: string - Required
  • returns value: any

Caveats

Note that there's a storage limit of ~4kb since data is stored in the cookie.