Home > JavaScript Server-side Reference > Login
Module Identifier
erbix/login
Compatibility
The login module is a DRAFT API. It is not standardized and currently it is Erbix-specific.
Overview
The login module exports a Login object who should receive in the constructor as a parameter the request parameter of the JSGI function being processed. This object offers the following functions:
isLoggedIn()
Returns a boolean indicating if a user is logged in or not.
getUserId()
Returns the id of the currently logged user, or null if no user is logged in.
getUsername()
Returns the string containing the current username of the logged user, or null if no user is logged in.
Please note that the username might change during the lifetime of the user, therefore getUserId() should be used instead to obtain an immutable user reference.
redirectToLogin(returnUrl)
Receives as input a return URL; defaults to the same URL.
Returns a JSGI redirect response to the Erbix login form; upon a successful login the user will be sent back to the specified URL.
redirectToLogout(returnUrl)
Receives as input a return URL; defaults to the same URL.
Returns a JSGI redirect response to the Erbix logout form; upon a successful logout the user will be sent back to the specified URL.
checkGateway()
Returns true if it should redirect to the Erbix Single-Sign-On service for a gateway check. See redirectToGateway().
redirectToGateway()
Returns a JSGI redirect response to the Erbix Single-Sign-On (SSO) with the gateway option. If there's no logged in user, the SSO will return without showing the login form. This option is useful for pages that can be customized for the current user but are not actually restricted. The SSO gateway flag is set in the current browser session.
resetGateway()
Resets the SSO gateway flag. After each redirectToGateway the SSO gateway flag is set. checkGateway() returns false if the SSO gateway flag is set. Should be called by the function that handles the URL used in redirectToGateway. See Usage Examples.
Usage Example
The following example demonstrates how this module can be used to get the username of the current user (or force login if there is no user logged-in):
exports.jsgi_main = function (request) {
var {Login} = require("erbix/login");
var login = new Login(request);
if (!login.isLoggedIn()) {
return login.redirectToLogin("http://myapp.erbix.com/");
}
return {
status: 200,
headers: {
"Content-type": "text/html"
},
body: [
"Welcome " + login.getUsername() + "!"
]
};
};
For pages where we'd like to know the currently logged in user, if any, but it's also ok to view the page without being logged in, we can use a gateway redirect:
exports.jsgi_non_restricted_page = function(request) {
var {Login} = require("erbix/login");
var login = new Login(request);
if (login.checkGateway()) {
// return to the same url
return login.redirectToGateway();
}
// reset gateway flag
login.resetGateway();
...
}
