A compact redis token manager that lets you create, fetch, update, refresh and delete tokens and their associated data
import { RedisTokens } from 'redis-tokens';
// You can pass an optional connection string here
const redisTokens = new RedisTokens('redis://localhost:6379');
const token = await redisTokens.create({
app: 'my-app',
type: 'user-session',
owner: 'abc',
data: {
something: 'here'
},
duration: 60 * 60 * 24 * 7
});
Optionally pass in a manually generated token
const myToken = myTokenGeneratorFunction();
const token = await redisTokens.create({
app: 'my-app',
type: 'user-session',
owner: 'abc',
data: {
something: 'here'
},
duration: 60 * 60 * 24 * 7,
customToken: myToken
});
const token = await redisTokens.get({
app: 'my-app',
type: 'user-session',
token: 'token'
});
await redisTokens.refresh({
app: 'my-app',
type: 'user-session',
token: 'token',
duration: 60 * 60 * 24 * 7
});
- Values sent as null will be deleted if they exist
- Undefined values are ignored
- Other values are inserted into the token data
const token = await redisTokens.update({
app: 'my-app',
type: 'user-session',
token: 'token',
data: {
insertThis: 'kaionesyan',
deleteThis: null,
doNothing: undefined
}
});
await redisTokens.delete({
app: 'my-app',
type: 'user-session',
token: 'token'
});
await redisTokens.deleteAll({
app: 'my-app',
type: 'user-session',
owner: 'abc'
});
The native IORedis client is exposed inside the RedisTokens class, should you wish to perform other kinds of operations.
Contributions are always welcome!