Helper functions for managing redis user sessions.
- 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)
npm install redis-user-sessions
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,
},
});
})();
Creates session data keyed on the session id provided.
async function createSession({ client, sessionId, data })
Property | Type | Default | Description |
---|---|---|---|
client | RedisClient |
required | Redis client created using createClient from the redis npm package. Must already be connected to Redis. |
sessionId | string |
auto generated | Unique identifier for the session. |
data | object |
required | Object that must contain the following properties: userId (string), expires (ISO 8601 timestamp). It can contain any other serialisable properties. |
return value |
undefined |
N/A | N/A |
Read session data keyed on the session id provided.
async function readSession({ client, sessionId })
Property | Type | Default | Description |
---|---|---|---|
client | RedisClient |
required | Redis client created using createClient from the redis npm package. Must already be connected to Redis. |
sessionId | string |
required | Unique identifier for the session. |
return value |
object / null |
N/A | Object 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. |
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 })
Property | Type | Default | Description |
---|---|---|---|
client | RedisClient |
required | Redis client created using createClient from the redis npm package. Must already be connected to Redis. |
sessionId | string |
required | Unique identifier for the session. |
data | object |
required | Object can contain any serialisable properties. |
return value |
undefined |
N/A | N/A |
Deletes an existin session keyed on the session id provided. If the session does not exist nothing happens.
async function deleteSession({ client, sessionId })
Property | Type | Default | Description |
---|---|---|---|
client | RedisClient |
required | Redis client created using createClient from the redis npm package. Must already be connected to Redis. |
sessionId | string |
required | Unique identifier for the session. |
return value |
undefined |
N/A | N/A |
Get all sessions for a particular user id.
async function getUserSessions({ client, userId })
Property | Type | Default | Description |
---|---|---|---|
client | RedisClient |
required | Redis client created using createClient from the redis npm package. Must already be connected to Redis. |
userId | string |
required | Unique identifier for the user. |
return value |
Array<{ sessionId, data }> |
N/A | sessionId 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. |
Update all sessions tied to a specific user id.
async function updateUserSessions({ client, userId, data })
Property | Type | Default | Description |
---|---|---|---|
client | RedisClient |
required | Redis client created using createClient from the redis npm package. Must already be connected to Redis. |
userId | string |
required | Unique identifier for the user. |
data | object |
required | Object can contain any serialisable properties. |
return value |
undefined |
N/A | N/A |
Delete all sessions tied to a specific user id.
async function deleteUserSessions({ client, userId })
Property | Type | Default | Description |
---|---|---|---|
client | RedisClient |
required | Redis client created using createClient from the redis npm package. Must already be connected to Redis. |
userId | string |
required | Unique identifier for the user. |
return value |
undefined |
N/A | N/A |