
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
@coolgk/session
Advanced tools
a javascript / typescript module
npm install @coolgk/session
An session handler that works without cookie (and with cookie too).
Report bugs here: https://github.com/coolgk/node-utils/issues
When working without cookie, this class reads the session token from the "Authorization" header. e.g. Authorization : Bearer cn389ncoiwuencr...
// express middleware
const session = require('@coolgk/session');
const app = require('express')();
app.use(
session.express({
redisClient: require('redis').createClient({
host: process.env.REDIS_HOST,
port: process.env.REDIS_PORT,
password: process.env.REDIS_PASSWORD
}),
secret: '123' // secret is required for creating the session token / id
})
);
app.use(async (request, response, next) => {
// allow access if it's the login page or the request has a valid session
if ('/login' === request.url || await request.session.verifyAndRenew()) { // if session is verified, renew session
next();
} else { // deny access
response.send('Please Login');
// output
// 'Please Login'
}
});
app.get('/login', async (request, response, next) => {
// start a new session (create a new session id)
const accessToken = await request.session.init();
// set session variables
await request.session.set('user', { id: 1, username: 'abc' });
// send session token/id back
response.json({ accessToken });
// output
// {"accessToken":"eyJleHAiOjAsIml..."}
});
app.get('/user', async (request, response, next) => {
// get session variable
response.json(await request.session.get('user'));
// output
// {"id":1,"username":"abc"}
});
app.get('/session', async (request, response, next) => {
// get all session values
response.json(await request.session.getAll());
// output
// {"user":{"id":1,"username":"abc"}}
});
app.get('/logout', async (request, response, next) => {
// destroy current session
await request.session.destroy();
response.json(await request.session.getAll());
// output
// {}
});
app.listen(8888);
import { Session } from '@coolgk/session';
// OR
// const { Session } = require('@coolgk/session');
const http = require('http');
http.createServer(async (request, response) => {
const session = new Session({
redisClient: require('redis').createClient({
host: process.env.REDIS_HOST,
port: process.env.REDIS_PORT,
password: process.env.REDIS_PASSWORD
}),
secret: '123',
request,
response
});
// ... some middelware
// ... in some routes
// set sesstion
await session.start();
await session.set('user', {id: 1, username: 'user@example.com'});
// check session and renew if verified
const verified = await session.verifyAndRenew();
if (verified) {
// session exists, logged in, do something
} else {
// deny access or show login screen
}
// show session data
response.end(
JSON.stringify(
await session.getAll()
)
); // {"user":{"id":1,"username":"user@example.com"}}
}).listen(8888);
Create a session without the "response" property and the sessoin object will read the session id from the "Authorization" header i.e. Authorization : Bearer cn389ncoiwuencr...
const session = new Session({
redisClient: require('redis').createClient({
host: process.env.REDIS_HOST,
port: process.env.REDIS_PORT,
password: process.env.REDIS_PASSWORD
}),
secret: '123',
request
});
This class extends @coolgk/token see set(), get(), delete(), getAll() in @coolgk/token
Kind: global class
promisepromisepromisedestory the current session
Kind: instance method of Session
promiserenew session optionally with a different expiry time
Kind: instance method of Session
Returns: promise - - false if session has not been started or has a invalid token string
| Param | Type | Description |
|---|---|---|
| [expiry] | number | in seconds |
FAQs
An session handler that works without cookie (and with cookie too).
We found that @coolgk/session demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.