The session module
Utilities for managing the current user's session, including login, logout etc
This module contains functions related to session management. Logging in, logging out and checking the current state of a user's session.
Functions in this module follow the node.js callback style. The first argument is an error object (if one occurred), the following arguments are the results of the operation. The callback is always the last argument to a function.
Usage
First, add session to the dependencies in kanso.json.
// in kanso.json
{
"dependencies": {
...
"session": null
}
}
Remember to do kanso install, then simply require the session module.
// in your code
var session = require("session");
With the Duality framework
All session methods must be called client-side. If you are using the Duality framework, you can call them from the browser after page load by using the afterResponse event
// in your list or show function
events.once("afterResponse", function(err){
session.info(function(info_err, response){ ... });
})
If you are not using Duality, just be aware that you cannot call session functions inside show, list or update functions. They can only be used in the browser.
Events
The session module is an EventEmitter. See the events package for more information.
change
Emitted whenever a change to the user's session is detected, this can occur as the result of a login/logout call or by getting the user's session info (and it's changed).
var session = require("session");
session.on('change', function (userCtx) {
// update session information, eg "Logged in as ..."
});
API
login(username, password, callback)
Attempt to login using the username and password provided.
- username - String - the username to login with
- password - String - the user's password (unhashed)
- callback - Function - function called with the result of the login attempt
session.login('testuser', 'password', function (err, response) {
if (err) // an error occurred logging in
else // success
});
logout(callback)
Logs out the current user.
- callback - Function - function called with the result of the logout attempt
session.logout(function (err, response) {
if (err) // an error occurred logging out
else // success
});
info(callback)
Returns the current user's session information. The info object contains a userCtx property and an info property. The first contains the name and roles of the
current user, the second contains information about the user database and authentication handlers.
- callback - Function - function called with the session information
session.info(function (err, info) {
if (err) // an error occurred getting session info
else // success
});
Reference
For up to date API information be sure to check out the session package page. On this page, you can also find API details for older versions of the package.
Next
Creating, updating and deleting users: The users module