Set a user attribute

User attributes exist so that you can keep track of different things about your user. You might want to put info like name, location, company, etc. so that you can keep whatever values you want on your users.

User attributes support any native JavaScript types. Here's a simple example of how to use it:

weasl.setAttribute('firstName', 'Ryan');
weasl.getCurrentUser().then(user => document.write("Hey there, " + user.attributes.firstName.value));

Needless to say, this will only work for logged in users -  you can't set attributes if there isn't a user to set it on!

The setAttribute simply takes arguments in the form of (attribute name, attribute value), so you can even do things like this:

weasl.setAttribute('name', {first: 'Vinny', last: 'Sabo', full: 'Vinny Sabo'}, 'JSON');

and it'll put that exact value on to the user.

User attributes can have certain types, and they'll be serialized based on those types. In the above example, we serialize the value to JSON. The third argument to the setAttribute function is the type, which must be a string of one of the following:

  • STRING (default if argument is not passed)
  • NUMBER
  • BOOLEAN
  • JSON

User attributes also have a notion of being "trusted". Consider you use an attribute for "is_admin" to determine, in your server code, if the user is an admin. With Weasl's JavaScript code, any site visitor can set "is_admin" to "true" for the logged in user. If you use the client secret as well as the client ID when setting user attributes, they will be trusted. This means that when checking business logic type attributes, you'll want to also check if the attribute is trusted.


Did this help answer your question?

thumbs up
thumbs down

Thanks for the feedback! 🙏🏽


Help by drift