npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@coolgk/session

v2.0.1

Published

An session handler that works without cookie (and with cookie too).

Downloads

11

Readme

@coolgk/session

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 Example

// 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);

Native Node App Example

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: '[email protected]'});

    // 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":"[email protected]"}}

}).listen(8888);

To use without cookie

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
});

Session

This class extends @coolgk/token see set(), get(), delete(), getAll() in @coolgk/token

Kind: global class

session.destroy() ⇒ promise

destory the current session

Kind: instance method of Session

session.renew([expiry]) ⇒ promise

renew 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 |