2.1.0 • Published 1 year ago

redis-user-sessions v2.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

redis-user-sessions

Helper functions for managing redis user sessions.

Benefits

  • Adds appropriate TTL for redis keys based on the required expires property on a session
  • Tracks sessions across user ids (using appropriate TTLs), making it easy to allow users see what sessions are currently active and manage deletion of their own sessions (when exposed to the user)

Installation

npm install redis-user-sessions

Example usage

import { createClient } = from 'redis';
import { createSession } = from 'redis-user-sessions';

(async () => {
  // Create and connect redis client
  const client = createClient();
  client.on('error', (error) => {
    throw error;
  });
  await client.connect();

  // Create session data
  const anyOtherData = { role: 'admin' };
  await createSession({
    client,
    sessionId: 'session-id',
    data: {
      expires: new Date().toISOString(),
      userId: 'user-id-for-eva',
      ...anyOtherData,
    },
  });
})();

API

createSession

Creates session data keyed on the session id provided.

async function createSession({ client, sessionId, data })

PropertyTypeDefaultDescription
clientRedisClientrequiredRedis client created using createClient from the redis npm package. Must already be connected to Redis.
sessionIdstringauto generatedUnique identifier for the session.
dataobjectrequiredObject that must contain the following properties: userId (string), expires (ISO 8601 timestamp). It can contain any other serialisable properties.
return valueundefinedN/AN/A

readSession

Read session data keyed on the session id provided.

async function readSession({ client, sessionId })

PropertyTypeDefaultDescription
clientRedisClientrequiredRedis client created using createClient from the redis npm package. Must already be connected to Redis.
sessionIdstringrequiredUnique identifier for the session.
return valueobject / nullN/AObject that must contain the following properties: userId (string), expires (ISO 8601 timestamp). It can contain any other serialisable properties. Returns null when the session does not exist.

updateSession

Updates existing sessions keyed on the session id provided. If the session does not exist the promise will be rejected.

async function updateSession({ client, sessionId, data })

PropertyTypeDefaultDescription
clientRedisClientrequiredRedis client created using createClient from the redis npm package. Must already be connected to Redis.
sessionIdstringrequiredUnique identifier for the session.
dataobjectrequiredObject can contain any serialisable properties.
return valueundefinedN/AN/A

deleteSession

Deletes an existin session keyed on the session id provided. If the session does not exist nothing happens.

async function deleteSession({ client, sessionId })

PropertyTypeDefaultDescription
clientRedisClientrequiredRedis client created using createClient from the redis npm package. Must already be connected to Redis.
sessionIdstringrequiredUnique identifier for the session.
return valueundefinedN/AN/A

getUserSessions

Get all sessions for a particular user id.

async function getUserSessions({ client, userId })

PropertyTypeDefaultDescription
clientRedisClientrequiredRedis client created using createClient from the redis npm package. Must already be connected to Redis.
userIdstringrequiredUnique identifier for the user.
return valueArray<{ sessionId, data }>N/AsessionId is a string. data is an object which will contain the following properties: userId (string), expires (ISO 8601 timestamp). The data object can also contain any other serialisable properties.

updateUserSessions

Update all sessions tied to a specific user id.

async function updateUserSessions({ client, userId, data })

PropertyTypeDefaultDescription
clientRedisClientrequiredRedis client created using createClient from the redis npm package. Must already be connected to Redis.
userIdstringrequiredUnique identifier for the user.
dataobjectrequiredObject can contain any serialisable properties.
return valueundefinedN/AN/A

deleteUserSessions

Delete all sessions tied to a specific user id.

async function deleteUserSessions({ client, userId })

PropertyTypeDefaultDescription
clientRedisClientrequiredRedis client created using createClient from the redis npm package. Must already be connected to Redis.
userIdstringrequiredUnique identifier for the user.
return valueundefinedN/AN/A
2.1.0

1 year ago

2.0.0

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago