Session library that uses JSON Web Token and cookies
yarn add fusion-plugin-jwt
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();
}
});
}
});
// 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;
}
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/fusionjs/tree/master/fusion-tokens#sessiontoken
SessionSecretToken
import {SessionSecretToken} from 'fusion-plugin-jwt';
Required. A secret for encrypting the JWT token / cookie. Can typically be a random static value.
type Secret = string;
SessionCookieNameToken
import {SessionCookieNameToken} from 'fusion-plugin-jwt';
Required. A cookie name
type CookieName = string;
SessionCookieExpiresToken
import {SessionCookieExpiresToken} from 'fusion-plugin-jwt';
Required. An expiration time in seconds.
type Expires = number;
const session: Session = Session.from((ctx: Context));
ctx: Context
- a Fusion.js contextsession: Session
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
- Requiredval: Object|Array|string|number|boolean
- A serializable value. Requiredvalue: any
session.get
const value: any = session.get(key: string);
key: string
- Requiredvalue: any
Note that there's a storage limit of ~4kb since data is stored in the cookie.