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 = ;const app = ;const sesscook = ; const session = ; //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 ofapp.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 ofsession.create()
method.request
is parameter ofapp.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 ofapp.get(path, (request,response)=>{...})
's callbackfn.
Example
const express = ;const app = ;const sesscook = ; const session = ; //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; /**An endpoint which redirects to login page, if session isinvalid, else session.isValid(req) method returns thesession data stored using session.create() method.*/app /**An endpoint which removes the current session. This will lead the session.isValid() method to return false. */app; app;
The session
object is an object of unexported class Sesscook
, accessed via exported sesscook()
method, which returns a new instance of the same.