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;
}
import Session from 'fusion-plugin-jwt';
The plugin. Should typically be registered to SessionToken
Typically should be registered with Session
. See https://github.com/fusionjs/fusion-tokens#sessiontoken
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;
import {SessionCookieNameToken} from 'fusion-plugin-jwt';
Required. A cookie name
type CookieName = string;
import {SessionCookieExpiresToken} from 'fusion-plugin-jwt';
Required. An expiration time in seconds.
type CookieName = number;
const session: Session = Session.from((ctx: Context));
ctx: Context
- a Fusion.js context- returns
session: 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. Required- returns
value: any
session.get
const value: any = session.get(key: string);
key: string
- Required- returns
value: any
Note that there's a storage limit of ~4kb since data is stored in the cookie.