This is a user accounts library, supporting registration, login, logout, and profile manipulation.
Log in or register for an account.
The current user object is: undefined
To use this library, simple import it and make any user-specific modifications to the global user object. Those modifications will be persisted for that user. (Note that you should not modify the uname and pass properties unless you're sure you know what you're doing!).
To let users log in, log out, register, and modify their profiles, send them to /lib-user/login, /lib-user/logout, /lib-user/register, and /lib-user/profile respectively. All these URLs accept a next parameter that tells lib-user where to redirect the user when done. Default is "/".
For example:
if (user)
print("Hello ", user.uname, "!");
else
print("Please ", link("/lib-user/login", "log in"), ".");
...yields:
This library is customizable. You can specify a multitude of options, by
defining variable _lib_user_options as an object and setting its parameters
before importing this library. Those options include adding additional fields to the
registration form and replacing the existing text with custom text. The table below describes
the parameters of _lib_user_options as well as its default values.
| Paths and functions | ||
|---|---|---|
| Parameter | Default | Description |
| specify a different print function | ||
| loginPath | /lib-user/login | specify what path you want to serve as the login path. lib-user will usurp this path and show a login form. |
| registerPath | /lib-user/register | specify the path you want to serve for registring users. |
| profilePath | /lib-user/profile | specify the path you want to serve for letting users modify their profiles. |
| logoutPath | /lib-user/logout | specify the path you want to server as the logout path. |
| Error messages | ||
| Parameter | Default | Description |
| errorBadUserid | "Invalid user ID." | on the login page, when the user enters a bad userid. |
| errorBadPassword | "Incorrect password." | on the password page, when the user enters a bad password. |
| errorUseridShort | "User ID too short." | the text to display if the user tries to register with a userid that's too short. |
| errorPassShort | "Password too short." | the text to display if the user chooses a password that's too short. |
| errorPassMatch | "Passwords do not match." | the text to display if the password and verify password fields don't match. |
| errorUseridNotAvailable | "User ID not available." | the text to display if the user chooses a userid that's taken. |
| errorFieldRequired | "This field is required." | the text to display if the user omits a required field. |
| Adding fields You can add extra fields to the registration and profile pages by setting _lib_user_options.fields to an array of objects with these properties. | ||
| Parameter | "Email" example | Description |
| id | "email" | what to identify the field as. this is also the property name on the "user" object. |
| label | "E-mail Address" | the user-friendly text to display as the label of the field. |
| validate | function(email) {
var testRE = new RegExp(
'^([-\w\.\+]+|"[^"]+")'+
'\@([-\w]+\.)+([-\w]+)\.?$');
if (testRE.test(email)) {
return true;
} else {
return "Invalid email address."
}
} | a function that validates the user's input |
| required | false | whether the field is required |
| type | "text" | the type of the field (usually "text", but can also be "textarea") |
| value | "" | the default value to populate the field with. |
| Other text | ||
| Parameter | Default | Description |
| loginHeader | "Log in to "+appjet.appName | the text to display as the main header of the login page |
| useridName | "User ID" | the text that prefixes the userid entry box |
| passwordName | "Password" | the text that prefixes the password entry box |
| verifyPassName | "Verify Password" | the text that prefixes the "verify password" box |
| keepMeLoggedInText | "Keep me logged in on this computer." | the text that follows the checkbox users use to indicate that they wish to remain logged in. |
| loginButton | "Log In" | the text to display on the "Log In" button |
| returnText | "Go Back" | the text of the "go back" link on the login, profile, and registration pages. |
| noAccountText | "Don't have an account? <a href='"+_registerPath+"?next="+encodeURIComponent(_next)+"'>Register here</a>."); | the text that sends people who don't have an account from the login page to the registration page. |
| registerHeader | "Register for "+appjet.appName | the text to display as the main header for the registration page |
| profileHeader | "Profile on "+appjet.appName | the text to display as the main header for the profile page |
| optionalText | " (optional)" | the text to follow optional field names |
| requiredText | "" | the text to follow required field names |
| yesAccountText | "Already have an account? <a href='"+_loginPath+"?next="+encodeURIComponent(_next)+"'>Log in here</a>."); | the text that sends people who do have an account from the registration page to the login page. |
| registerButton | "Register" | the text to display on the "Register" button. |
| profileButton | "Update" | the text to display on the profile update button. |
| successText | "Updated" | the text to display next to fields that were successfully updated |