kow-sessions

A compact redis session manager that lets you create, get, update, refresh and kill sessions.

Usage no npm install needed!

<script type="module">
  import kowSessions from 'https://cdn.skypack.dev/kow-sessions';
</script>

README

Kow Sessions

Kow Sessions will help you manage your users' sessions by letting you easily create, fetch, refresh, update and kill sessions. It even lets you kill all sessions owned by a user at once.

It is built on top of ioredis for lightning speed operations.

I've built this package for the sake of simplicity, but I also exposed the Redis client through the package so you can also use all advanced features that redis offers without having the need to install both this package and a redis client.

Disclaimer

This is not a production ready package (although it is pretty straight-forward).

Installation

yarn add kow-sessions

or

npm install kow-sessions

Features

  • create: Creates a session by providing an app name, a ownerId and custom data to be stored with the session. Returns a token.
  • get: Gets the owner and custom data of a specific token + app name combo. Returns undefined if the session is not found.
  • refresh: Refreshes the duration of a specific session. Returns true if the session was refreshed or false if it was not found.
  • update: Updates the custom data of an existing session. Returns true if the session was updated or false if it was not found.
  • kill: Kills an existing session. Returns true if the session was killed or false if it was not found.
  • killAll: Kills all sessions of the same user. Returns true if all sessions were killed or false if no sessions were found.

Going Further

  • client: An ioredis client is exposed within KowSessions class so you can use all advanced features of redis you wish.

Usage

Instantiate the client

// Pass an optional connection string to the constructor
// new KowSessions('redis://localhost:6379')
const kow = new KowSessions();

Create a session

const kow = new KowSessions();

const token = await kow.create({
  app: 'my-app',
  ownerId: '123qwe',
  data: { 
    firstName: 'John',
    lastName: 'Doe',
    currentDeviceToken: '123'
  },
  duration: 3600
);

Gets a session

const kow = new KowSessions();

const { ownerId, data } = await kow.get({
  app: 'my-app',
  token: 'qwerty',
);

Refreshes a session

const kow = new KowSessions();

const success = await kow.refresh({
  app: 'my-app',
  token: 'qwerty',
  duration: 7200
);

Updates a session

const kow = new KowSessions();

const success = await kow.update({
  app: 'my-app',
  token: 'qwerty',
  data: {
    name: null, // Use null to delete existing values
    age: 25
  }
);

Kills a session

const kow = new KowSessions();

const success = await kow.kill({
  app: 'my-app',
  token: 'qwerty'
);

Kills all sessions of a user

const kow = new KowSessions();

const success = await kow.killAll({
  app: 'my-app',
  ownerId: '123qwe'
);

Contributing

Feel free to contribute in any way you'd like. You can email me at kaionesyan@gmail.com if you have any questions.