A generic login module.
Starts a session for the given user if the user credentials are correct and the user is not already logged in.
The module HTML must contain a form#login which will be used for data collection. The form must contain the following named inputs:
usernamethe login user namepasswordthe login password- any additional named input will be submitted with the request and saved into the server side user object under the
additionalkey
200and no data if the login was successful400if the user is already logged in or any of theusernameorpasswordis missing403if the authentication failed or the user info (necessary for the building the session) could not be retrieved
Ends a session for the logged in user.
Gets the user session object or undefined if the user is not logged in.
Sends an email the user with a password reset link.
Node: Currently, this feature is only supported for login modules that use email addresses as user names.
If this operation receives a valid user name and a password reset token, it will display a simple reset password form. Upon form submission, the user password is reset.
For every operation in this module, the events are emitted in the order in which they are enumerated below.
To register a handler event you edit the application.json file of the application in the following way: after replacing the {miid} and {operation} placeholders, you add as properties to this object the events to be handled: miids.{miid}.operations.{operation}.params.
on.query
[optional] You can define a query handler event with handlers in the form of function (link, params, customData, callback) { ... } where the callback should be called with an error (possibly null) and a data object.
on.userCheck
[optional] You can define a userCheck handler event with handlers in the form of function (user, collection, session, callback) { ... } where the callback should be called like this: callback(err, user, collection) where the error err can be null. Here you can perform additional checks on the user and optionally respond with an error.
on.userInfo
[required] The userInfo handler event is required in order for the module to be able to build the user information to be saved in the new session. The handlers of this event should have this signature:
function (user, session, callback) {
// ... compute err (possibly null) and userInfo
callback(err, userInfo);
}and it must return through the callback a user information object, userInfo (if no error state is found). This object must contain:
{
rid: …, // the Mono role ID (use M.app.getRole to get the role ID for a role with a given name)
uid: …, // the user ID (application specific)
locale: …, // the user locale (2 letter lower case language code)
data: … // extra session data that will be saved as session.data (application specific)
}on.query
[optional] You can define a query handler event with handlers in the form of function (link, params, customData, callback) { ... } where the callback should be called with an error (possibly null) and a data object.
on.userCheck
[optional] You can define a userCheck handler event with handlers in the form of function (user, collection, session, callback) { ... } where the callback should be called like this: callback(err, user, collection) where the error err can be null. Here you can perform additional checks on the user and optionally respond with an error.
on.customReceiverHandler
[optional] You can define a customReceiverHandler handler event in the form of function (emitedData, callback) { ... } where the callback should be called like this: callback(err, receiver) where the error err can be null. Here you can change to whom the password reset email will be sent.
on.query
[optional] You can define a query handler event with handlers in the form of function (link, params, customData, callback) { ... } where the callback should be called with an error (possibly null) and a data object.
on.userCheck
[optional] You can define a userCheck handler event with handlers in the form of function (user, collection, session, callback) { ... } where the callback should be called like this: callback(err, user, collection) where the error err can be null. Here you can perform additional checks on the user and optionally respond with an error.
on.success
[optional] You can define a success handler event with handlers in the form of function (data) { ... } where data is an object with user and link properties.
templateFile
[optional] You can define a jade template file to create a custom reset form. This configuration is the path in the application where the file is located.
- transferred the module to the new jxMono organization
- updated Bind to
v0.4.0, Events tov0.4.0, Utils tov0.2.0
- Updated to Bind
v0.3.3, Eventsv0.3.1and Utilsv0.1.8
- Implemented a
userCheckevent inlogin,forgotandresetoperations; - Reimplemented
userInfoevent from theloginoperation usingM.emit; - Renamed
onSuccessevent from theresetoperation toon.success; - Renamed
params.customQueryevent from thelogin,forgotandresetoperations toparams.on.query; - Removed dead code;
- Updated the documentation in README.md.
- Added support for jade template for the
forgotoperation
- Updated to Bind v0.3.1
- Added reset password functionality implemented with the
resetandforgotoperations - Replaced most of the error messages with error codes (to be translated/properly displayed by applications)
- Send an object containing the
filter, the form data (object containing:usernameand `password fields) to the custom script. - Added
logoutmethod - Added
utilsas dependency - Added JSDoc comments
Events v0.1.11
- Sending all data from the client and save it in the user server object under
additionalskey Events v0.1.10andBind v0.2.2
- Hide login and logout elements matched by selectors on module init
- Cleaned up the code
- Added
readyOnUserInfofield inconfig.options. If this istruethereadyevent will be emitted after the login module gets the user info.
- Send user info data after login.
-
Handle
successPagein object format. ThesuccessPagecan look like:{ "scripts": ["/js/some-custom-script.js"], ..., "successPage": { "type": "function", "value": "Foo.login.computeSuccessPage" } }In the custom script (
some-custom-script.js) that definesFoo.login.computeSuccessPagewe will have:window.Foo = { login: { computeSuccessPage: function (options, callback) { var redirectUrl = "..."; doSomeCrudRequest(..., function (err, data) { if (err) { alert(err); } callback(redirectUrl); }); } } };
-
Added custom query feature that can be used when searching user in database. For this you have to provide
customQueryparameter in theparamsobject:{ "ds": "loginsDS", "hash": "...", "userkey": "..", "passkey": "...", "customQuery": "/login_custom_query", "on": {...} }In the application directory you have to create
login_custom_query.jsthat will export a function:/* * This function is called from the login module. * * */ module.exports = function (link, params, filter, callback) { filter["add any field"] = "you want"; // do a database request, for example foo (function (err) { // handle error if (err) return callback (err); // finally callback callback(); }); };
Events v0.1.8andBind v0.2.1
- TODO
- TODO
- TODO
- Fixed
getUserInfoapi from v0.1.0
- WARNING This version has a bug in it DO NOT USE!!
- Renamed the main file to the name of the module
- Added Events
v0.1.7dependency - Added skeleton documentation