Web API: Search user by custom field?
-
I am using a customized nodebb-plugin-sso-oauth plugin. After login, the user is created initially and the users id on my oauth identity server is saved in a custom field (
faf-nodebbId
).My OAuth identity server offers a concept of changing your username. So I built some code that calls the
api/v2/users
endpoint of the write plugin. However, to write the correct thing I need to lookup the nodebb userid with my external id /faf-nodebbId
.It seems like the nodebb web api has no feature to search for users by custom fields. Am I right? Is there a plugin that offers this? Or what plugin could be taken as reference to implement this on my own?
-
@Brutus5000 said in Web API: Search user by custom field?:
nodebb-plugin-sso-oauth
https://github.com/julianlam/nodebb-plugin-sso-oauth/blob/master/library.js#L209
This is usually done by saving the mapping of oauth id to nodebb uid. If it is saved as an object like that then getting the nodebb uid with a oauth id is done with
const nbbUid = await db.getObjectField(constants.name + 'Id:uid', payload.oAuthid);
-
But that is internal api. I am looking for a solution for an external service to lookup the user with the custom field.
This already exists: http://localhost:4567/api/user/uid/2
I am looking for something likehttp://localhost:4567/api/user/<my custom field>/<my custom value>
orhttp://localhost:4567/api/users?filter=<my custom field>==<my custom value>
-
I think you'll need to create that route in the plugin that you're using to add the custom field - NodeBB only provides routes for uid and username. You can check for privileges with
middleware.canViewUsers
if you don't want everyone to be able to see users info. I think the oauth plugin already sets up some custom routes, so you can base the api implementation on them. -
Ah yes, perfect. That works locally. Thank you very much.
Last question: I published a new version of my plugin on npmjs.org, but the plugin list still shows the old version (even a complete new installation). What am I doing wrong? Or is this list centrally stored somewhere and only refreshed once a day?
-
It should cache for an hour, I think (based on a quick look at nodebb package manager code). So yes - you have to wait a bit before the nbbpm refreshes. You can just install the updated version manually by using
npm install <package name>
from the command line in NodeBB folder.