sessids

1.0.4 • Public • Published

functions

main

require("sessids")(sessfile:string)

returns all functions below

sessions

require("sessids")().sessions

sessions is a express middleware that will allow you to use sessions. it sets req.session to a object that will look something like this:

name description
set(property, value) sets a value on the session
get(property) gets a value from the session
delete(property) deletes a value from the session
getAllProperties() gets all properties with thier values and puts it into a object
destroy() destroys a session instantly
id id of the session
lifetime lifetime of the session(will not refresh)

example

const express = require('express');
const app = express();

const sessids = require('sessids')();

app.use(sessids.sessions);

app.get('/get', (req, res) => {
    res.json(req.session.getAllProperties());
});

app.get('/set', (req, res) => {
    for (const key in req.query) {
        req.session.set(key, req.query[key]);
    }
    res.json(req.session.getAllProperties());
});

app.listen(3000, () => {
    console.log('Example app listening on port 3000!');
});

configue

require("sessids")().configue(scope: string, value: any);

configure sets scope in the configuration to value

scopes

scope default value description notes
lifetime 86400 the lifetime of all sessions created after setting this value
sessfile "sessions.json" the file used for storing sessions, will automatically be changed when calling const sessids = require('sessids')("value"); do not change yourself

session

(require("sessids")().session: class constructor(data?: object, lifetime?: number, id?: string) {}:{
    lifetime: number,
    storeddata: object,
    id: string,
    update: function() updates this session in the file,
    destory: function() destroys this session,
    data: {
        set: function(scope:string, value:any) updates value in this session,
        get: function(scope:string) gets value in this session,
        remove: function(scope:string) removes value in this session
    }
})

find

require("sessids")().find(search: {
    id?: string | undefined;
    data?: {
        value: any;
    } | undefined;
});
return:session[]

searches for the session meeting the requirements

name usage
id search for a session with that session id
data a object any value in there will be required to also be the value in the object

full examples

login screen example

  1. create a new directory

bash/ps

mkdir loginexample
  1. cd into the directory

bash/ps/cmd

cd loginexample
  1. initialize the directory

bash/ps/cmd

npm init -y
  1. install sessids and express

bash/ps/cmd

npm i sessids express 
  1. require sessids and express

index.js

const express = require('express');
const app = express();

const sessids = require('sessids')();
  1. initialize server

index.js

const express = require('express');
const app = express();

const sessids = require('sessids')();

app.use(sessids.sessions);

app.get('/', (req, res) => {
    res.json(req.session.getAllProperties());
});

app.get('/set', (req, res) => {
    for (const key in req.query) {
        req.session.set(key, req.query[key]);
    }
    res.json(req.session.getAllProperties());
});
  1. creating statics

index.js

const express = require('express');
const app = express();

const sessids = require('sessids')();

app.use(sessids.sessions);

app.get('/', (req, res) => {
    res.json(req.session.getAllProperties());
});

app.get('/set', (req, res) => {
    for (const key in req.query) {
        req.session.set(key, req.query[key]);
    }
    res.json(req.session.getAllProperties());
});

app.use(express.static("public"));
  1. create the public directory

bash/ps

mkdir public
cd public
  1. create a new file in public: login.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>example login page</title>
</head>
<body>
    <form action="submitlogin" method="post">
        <label for="username">username: </label><input name="username" id="username">
        <label for="password">password: </label><input name="password" id="password" type="password">
        <input type="submit">
    </form>
</body>
</html>
  1. setting up login callback

bash/ps/cmd

cd ../
npm i body-parser

index.js

const express = require('express');
const app = express();

const bodyParser = require('body-parser');

const sessids = require('sessids')();

app.use(bodyParser.urlencoded({ extended: true }));
app.use(sessids.sessions);

app.get('/', (req, res) => {
    res.json(req.session.getAllProperties());
});

app.get('/set', (req, res) => {
    for (const key in req.query) {
        req.session.set(key, req.query[key]);
    }
    res.json(req.session.getAllProperties());
});

app.post('/submitlogin', (req, res) => {
    req.session.set("username", req.body.username);
    req.session.set("password", req.body.password);
});

app.use(express.static("public"));

app.listen(3000, () => {
    console.log("example login system up and running!");
});
  1. try it out

bash/ps/cmd

node index.js

go to localhost:3000's login page

you will see a new file in root directory called sessions.json, after you have logged in, you will see it looks something like this:

{
    "sessions": [
        {
            "id": "w20nYhiPZ93fIyaUCbp7DETLquvjeMX!Q5l8ONKt46VAoGgBxkRsdFrJzHScW1mGMuWovgsfdymZ78Y3Tjea1EU2xC6SpKhcA0IVlqJHNP594k!rnFbQDwBiXLzOtR",
            "data": {
                "username": "test",
                "password": "test"
            },
            "lifetime": 86300
        }
    ]
}
  1. improve user experience

index.js

const express = require('express');
const app = express();

const bodyParser = require('body-parser');

const sessids = require('sessids')();

app.use(bodyParser.urlencoded({ extended: true }));
app.use(sessids.sessions);

app.get('/', (req, res) => {
    res.json(req.session.getAllProperties());
});

app.get('/set', (req, res) => {
    for (const key in req.query) {
        req.session.set(key, req.query[key]);
    }
    res.json(req.session.getAllProperties());
});

app.post('/submitlogin', (req, res) => {
    req.session.set("username", req.body.username);
    req.session.set("password", req.body.password);
    res.redirect('/');
});

app.get('/submitlogin', (req, res) => {
    res.redirect('/');
});

app.use(express.static("public"));

app.listen(3000, () => {
    console.log("example login system up and running!");
});

Package Sidebar

Install

npm i sessids

Weekly Downloads

26

Version

1.0.4

License

GPL-3.0-only

Unpacked Size

2.2 MB

Total Files

525

Last publish

Collaborators

  • -sweatycircle439