1.0.5 • Published 7 years ago

session-validator v1.0.5

Weekly downloads
12
License
Apache-2.0
Repository
github
Last release
7 years ago

Session Validator

So that there is no confusion, it is important to note that the only file you need from this project, for it to work in your project, is the session-validator.js and jQuery. You can obtain this file by simply grabbing it manually or installing it through npm. Read this document once, and you'll be on your way.

The goal of this mini project was to create a simple to use session validator which allows for updating a displayed session timeout for the user. I opted for a callback as opposed to updating the DOM from within the library, as it provides the ultimate flexibility. The callback receives a simple json result from the server...

{ sessionValid: true, sessionTime: 600 }

The session time (time before expiry) is in seconds.

Web Service

Your application must have a web service which does not manipulate the session, but only states whether the session is valid, and how much time there is left. As indicated earlier, the data returned should be JSON in the following format.

{ sessionValid: true, sessionTime: 600 }

If desired, your web service may also have an option to refresh the session, so that the user does not need to leave the current page.

Usage

HTML

You are required to include jQuery and the session validator. Examples below...

Javascript

It's as simple as instantiating a new instance of the session validator, with the session web service url, the redirect url when the session becomes invalid, and your callback function. A working example for the test web service that this project provides is below, and can also be found in the validator-example.js ...

function displayTime(result) {\ jQuery('#session-time').text(Math.ceil(result.sessionTime / 60) + 'm or ' +\ result.sessionTime + 's');\ };\ let sessionValidator = new SessionValidator({validate_url:\ 'http://localhost:3000/check-session', refresh_url:\ 'http://localhost:3000/refresh-session', check_frequency: 1,\ timeout_url:\ 'http://localhost:3000/timeout', callback:\ function (result)\ {\ displayTime(result);\ if (result.sessionTime < 60)\ {\ jQuery('#more-time').show();\ }\ }});\ sessionValidator.monitor();

In the event that the session becomes invalid, the browser will be redirected to the redirect url, unless the redirect url is null.

Playing

This project provides a facility for playing around, so that you can see how it all fits together.

git clonehttps://github.com/TrentonAdams/session-validator.git\ cd session-validator\ sudo npm install -g bower grunt grunt-cli\ npm install\ bower install\ grunt

Now visit http://localhost:3000; this first visit starts the session counter. This main page creates a new session with a 10 minute timer. If you'd like to see the web service results, go to...

# visithttp://localhost:3000/check-sessionin your browser or\ curl -H 'Accept: application/json'http://localhost:3000/check-session

If you'd like to expire the session (in another tab) to see what happens on the main page, you can go to...

http://localhost:3000/check-session?expire=true

If you'd like to set the session time to 60 seconds or less, to see what happens on the main page with an example "I need more time" button, you can set the session time manually (after you've visited the page once)...

curl -d 'time=60' -H 'Accept: application/json'http://localhost:3000/refresh-session

Documentation

You can generate some quick docs to get an overview of the SessionValidator class.

grunt jsdoc\ google-chrome doc/SessionValidator.html