-
Notifications
You must be signed in to change notification settings - Fork 5
Guide to User Data
User data is maintained almost exclusively by the user
module. This module is specially designed to be functional in all parts of an application.
This method--when called--will return a boolean identifying whether a user is logged in.
user.get_settings()
Returns an object containing all settings related to the current user. Great care should be taken with this method, as it returns a direct reference to the settings object. Assigning values to the output of this method will mutate the user's settings. If a reference to the settings object persists beyond a user's session, you may accidentally spuriously write values for the wrong user.
user.get_setting(key[, default_])
Returns a user setting with the key key
. If the setting does not exist, default_
is returned (if unspecified, it will return undefined
).
user.update_settings(settings)
Updates the user's settings. settings
is expected to be an object where members are settings to overwrite. For example:
require('user').update_settings({
pet_preference: 'cats',
browser_of_choice: 'chrome'
});
Keys not included in settings
will not be updated.
user.clear_settings()
Calling this method clears all user settings.
user.get_permission(name)
Returns the value of a user permission named name
. If the permission does not exist, false
is returned.
user.update_permissions(permissions)
Updates the user's permissions. permissions
is expected to be an object describing ALL permissions for the user. Unlike user.update_settings()
, this will replace the entire permission list.
The user
module exposes a number of methods for storing and maintaining the user token used for shared secret authentication (used in the login
module). These methods are very sensitive, however, and should be used with great caution.
user.set_token(new_token[, new_settings])
Sets the user token to the value new_token
. If new_settings
is defined, user.update_settings()
is called with it.
Returns the current user token.
Clears the current user token. If email
is stored as a setting, it will be cleared. All user permissions will also be cleared.
The user module is exposed through an object in the global context, user
. The following methods are exposed:
logged_in()
get_setting()
get_permission()
Other methods are not exposed to prevent templates from being abused. No token-related methods are exposed.
User data is stored using the storage
module under the key "settings"
. You can read more about using storage in the storage guide.
Inside Phantom, the user module does not persist data. This is done because Phantom does not currently (as of writing) provide a way to clear localStorage
between sessions. In order to facilitate testing, the user module simply does not persist any data inside Phantom.