SessionKit (Factory)

The SessionKit class provided by the Session Kit is responsible for creating and managing end user Session instances specifically for web applications, through actions such as Login and Restore. Only one instance is required for the entire application and should be made widely available.

Creation

When using the SessionKit within a web application, the developer will first establish an instance of the class with the required SessionKitArgs.

A simple example for a web application using the WebRenderer and WalletPluginAnchor would be as follows:

import { SessionKit } from "@wharfkit/session"
import { WebRenderer } from "@wharfkit/web-renderer"
import { WalletPluginAnchor } from "@wharfkit/wallet-plugin-anchor"

const args = {
  appName: "myapp",
  chains: [
    {
      id: "73e4385a2708e6d7048834fbc1079f2fabb17b3c125b146af438971e90716c4d",
      url: "http://jungle4.greymass.com",
    },
  ],
  ui: new WebRenderer(),
  walletPlugins: [new WalletPluginAnchor()],
}

const options = {
  // Additional options
}

const sessionKit = new SessionKit(args, options)

Arguments

The first parameter passed to the SessionKit is an object containing all the required configuration options.

  • appName: The name of the application.
  • chains: An array of blockchains that the app supports.
  • ui: An instance of a UserInterface, like the WebRenderer.
  • walletPlugins: An array containing at least one WalletPlugin.

Options

The second parameter passed would be all of the optional arguments:

  • allowModify: A boolean flag indicating whether plugins are allowed to modify transaction data. This parameter is passed down to each Session that this SessionKit generates.
  • expireSeconds: The default number of seconds before a transaction expires. This parameter is passed down to each Session that this SessionKit generates.
  • fetch: Primarily for development purposes in Node.js v16 or lower, an instance of fetch which can be used by all components.
  • loginPlugins: An array of LoginPlugins for use in all Session instances generated by this SessionKit.
  • storage: An option to override the default instance of SessionStorage, which is used to persist data in the SessionKit, the Sessions it creates, and all plugins.
  • transactPlugins: An array of TransactPlugins for use in all Session instances generated by this SessionKit.
  • transactPluginsOptions: A key/value pair object from which plugins can read global configuration options. This will only need to be used if the plugin instructions require it.

Usage

Methods

A number of methods are available on an instance of the SessionKit.

  • Login: Initiate the login process for a user.
  • Logout: Perform a logout of a given session, or logout all existing sessions.
  • Restore: Restore and return the most recently used session, or a specific session based on the args provided.
  • restoreAll: Restore all sessions and return them in an array.
  • getSessions: Return an array of SerializedSession objects.

Guides

TypeDocs