Retrieving User Properties
The following JavaScript utility functions are provided for accessing the logged-in user’s properties and user roles. These functions will only provide relevant information after a user has logged-in.
Knack.getUserAttributes()
This function returns data for the logged-in Account. See the table below for how the information is returned. If a user is not logged-in, the string “No user found” will be returned.
Data in connected User Roles will need to be accessed separately via an API request.
Attribute | Description |
---|---|
a string of the logged-in user’s email | |
id | a string of the logged-in user’s record id |
name | a string of the logged-in user’s name |
roles | an array of object-keys that the user is assigned to ["object_1", "object_2"] |
values | the complete values from the Account record, indexed by field {field_1: "[email protected]", field_2: "Tester"} |
Knack.getUserAttributes();
/*
{
id: "5a5ce0c75954805576173519",
values: {…},
roles: Array(1),
email: "[email protected]",
name: "Amy Adams"
}
*/
This function also allows you to grab individual pieces, if not all information is needed:
Knack.getUserAttributes().email;
//"[email protected]"
Custom fields added to the top-level Account object can be accessed using object notation on the values
attribute (individual user role object fields are not accessible here):
// Change field_1 to the field key of the field you want to retreive
Knack.getUserAttributes().values.field_1;
//"my custom field value"
Knack.getUserToken()
Returns the logged-in user's token (string).
Knack.getUserToken();
//"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiNTU0N2VhNjVkMmE3MzgxNDA5MjU3YWQzIiwiYXBwbGljYXRpb25faWQiOiI1MzllNjYzYjZlNTNlY2M1NjAwMDBiNzkiLCJpYXQiOjE0MzA3NzY0NDF9.g7C3iJexG3Na2dt6m_vumheXLUe1H_4NEYKziH9Gtm8"
Knack.getUserRoles()
When no parameter is provided, this function returns an array of object keys representing the user roles to which the user is assigned.
Knack.getUserRoles(); // ['object_1', 'object_2']
If you pass in an object key - as a string - this function will return true if the user has access to that role, or false if not:
Knack.getUserRoles('object_1'); // true
Updated almost 4 years ago