Giter VIP home page Giter VIP logo

sesscook's Introduction

sesscook

Local session manager module for nodejs with expressjs applications. Uses signed & masked cookies to maintain local session only.

Install

npm i sesscook

Methods

After declaring

const express = require("express");
const app = express();
const sesscook = require("sesscook");

const session = sesscook(
  app, //Express.Application object
  "somesessionsecret",  //private key
  60*60*2,  //validity in seconds (here 2 hours), defaults  to 10 years.
  "somesessionpublickey" //optional public key.
);

//an optional fifth parameter (boolean) is also present, but not recommended to set true, as session will only be created on https protocol then.

the following methods can be accessed via session object.

  • session.create(response,sessiondata)

    This method creates a new session based on following parameters.

    • response is parameter of app.get(path, (request,response)=>{...})'s callbackfn.

    • sessiondata is JSON/String type data, to be stored in current session (like username, or userid).

    • secured is Boolean type, defaults to false. If set true, then session will only be created on https (secured) protocol. (not recommended)

  • session.isValid(request) This method checks if session is valid, returns false if not, or returns sessiondata which was provided as second parameter of session.create() method.

    • request is parameter of app.get(path, (request,response)=>{...})'s callbackfn.
  • session.finish(response)

    This method finishes and thus invalidates any current session created via session.create() method.

    • response is parameter of app.get(path, (request,response)=>{...})'s callbackfn.

Example

const express = require("express");
const app = express();
const sesscook = require("sesscook");

const session = sesscook(app,"sessionsecret",60*60*2,"sessionpublickey");

//now use session object to access Sesscook methods, wherever needed.

/** This endpoint creates a new session with dummy session
 data, and after that the session.isValid() method will return that same data.
 */
app.get("/login",(req,res)=>{
  ...
  //after successfull authentication

  let sessiondata = {
    username:"someuser",
    email:"someemail"
  };

  session.create(res,sessiondata);
  //session is created
});

/**An endpoint which redirects to login page, if session is
invalid, else session.isValid(req) method returns the
session data stored using session.create() method.
*/
app.get("/session",(req,res)=>{
  const client = session.isValid(req);
  if(!client) return res.redirect("/login");  //if client is false, session is invalid.

  ...
  //if session is valid, then client contains sessiondata for current valid session.
  const email = client.email;
  const username = client.username;
  ...
})

/**An endpoint which removes the current session. This will
 lead the session.isValid() method to return false.
 */
app.get("/logout",(req,res)=>{
  session.finish(res);
  //session finished.
  ...
});

app.listen(8000);

The session object is an object of unexported class Sesscook, accessed via exported sesscook() method, which returns a new instance of the same.

Dependencies

sesscook's People

Contributors

geopic avatar ranjanistic avatar

sesscook's Issues

TypeScript type declaration file

If you want I can write a TypeScript type declaration file for this package so that TS developers can utilise the type-checking feature of the language as they use this package. Let me know if you are interested.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.