Suspicious Session
This is a package to manage sessions stored in encrypted files (with AES), using UUIDv4 for client identification for Express.js. This package it's a newer version writted from zero based of session-crossover package (now deprecated). This package is developed with typescript and contains all required *.d.ts
definitions inside it.
Implementation
- First install this package in your project:
npm install --save suspicious-session
- Then create a new
express();
instance, and use the middleware as follows:
import express from 'express';
import { suspiciousSession } from 'suspicious-session';
const app = express();
app.use(suspiciousSession({
path: './data',
name: 'i-see-you',
maxAge: 15,
algorithm: 'aes-256-ccm',
}));
Basic Usage
The core of the package resides in req.session
, which contains the necessary methods to manage the current sessions. All operations about sessions are available in that object. These are some examples of usage:
- Create a new session, and save inside an object:
app.get('/create', async (req, res) => {
await req.session.create();
await req.session.current().save({
id: 543,
nick: 'nadja',
typeUser: 4
});
res.end();
});
- Rewind the expiration time of the current session:
app.get('/rewind', async (req, res) => {
const exist = !!req.session.current();
if (exist) {
req.session.rewind();
}
res.end();
});
- Destroy the current session:
app.get('/destroy', async (req, res) => {
const exist = !!req.session.current();
if (exist) {
await req.session.destroy();
}
res.end();
});
- Read data from a session:
app.get('/read', async (req, res) => {
const current = req.session.current();
if (current) {
const data = await current.load();
res.json(data);
} else {
res.json(null);
}
});