nodebb-plugin-write-api - add custom user fields

NodeBB Plugins
  • I am integrating NodeBB into a site which also takes care of registration and login. nodebb-plugin-write-api and nodebb-plugin-session-sharing have worked well for that purpose so far.

    Now I need to store some additional fields with a user to interact with the main site. One example is fetching the user's avatar from there.

    The write-api documentation states for the v2 Create new user endpoint, "Any other data passed in will be saved into the user hash". Unfortunately, this applies only to NodeBB-known fields: it calls Users.create from the NodeBB source which only looks at NodeBB-known fields. Own fields, like "mySiteUserId", are not considered.

    What is the best way to enhance the user with own fields then when using write-api?

    I already had a look at nodebb-plugin-ns-custom-fields but might be a bit too much, as I don't need these fields to be user-editable. I could imagine that there's a much easier way?

  • Did you take a look at the hooks plugins.fireHook('filter:user.create', { user: userData, data: data }, next); and plugins.fireHook('filter:user.whitelistFields', { uids: uids, whitelist: fieldWhitelist.slice() }, next); The first one should allow you to save the passed in data to the user object, the second one should allow you to add your custom fields to the api returns.

  • Well, I have read some plugin code already, but I have yet to write my own one.

    You mean like so?

    In plugin.json:

    	"hooks": [
    		{ "hook": "filter:user.create", "method": "createUser" },
    		{ "hook": "filter:user.whitelistFields", "method": "whitelistFields" }

    In my plugin library.js:

    plugin.createUser = function(data, callback) {
    	data.user.customField =;
    	callback(null, data);
    plugin.whitelistFields = function(data, callback) {
    	callback(null, data);
  • Well, I can answer that for myself: it worked!> HGETALL user:28
     1) "username"
     2) "test21"
    43) "customField1"
    44) "123"
    45) "customField2"
    46) "xxx"
    47) "uid"
    48) "28"

    I could then fetch this field e.g. using:

    user.getUserFields(data.uid, ['customField1'], function(err, userData) {
    	// do something with userData.customField1
  • @hearsedriver Glad you figured it out 😄

Suggested Topics