How to change user name via external call?



  • Hi there,

    I was able to setup a nodeBB forum with SSO of our own OAuth2 compliant service. So people login first time, their user is created an everything works as intended.

    The problem is: In our service people can change their username. And I would like to update the name in nodeBB as well when this happens. How can I achieve that?

    1. I have not seen any documentation on changing usernames via plugin.
    2. I have no clue how to do this with the plugin system. From my understanding the hook system can only be triggered when actions are executed inside nodeBB. But in this case the action would be triggered from a 3rd party system without a user being logged in.

    If anybody could point me into a direction in the docs or any examples I'd be happy.



  • @Brutus5000 said in How to change user name via external call?

    2. I have no clue how to do this with the plugin system. From my understanding the hook system can only be triggered when actions are executed inside nodeBB. But in this case the action would be triggered from a 3rd party system without a user being logged in.

    Well, fortunately for you, you are wrong. Hooks are really just triggers - if x happens call every function that is using that hook. But there is no problem with using something else to trigger your plugin.
    A relatively common example would be websockets, that can be used to send data from frontend to backend when there is no hook covering what you want to get.
    Example: https://github.com/NodeBB/nodebb-plugin-category-notifications/blob/master/lib/websockets.js

    So as long as there is a way to communicate between your service and your NodeBB plugin there shouldn't be any problem there.

    Now, as for

    1. I have not seen any documentation on changing usernames via plugin.

    Unfortunately, there is not a lot of documentation on NodeBB internals so you mostly need to just look at the source and find what you need.
    In this case the functions you need are in src/user, so you can import them with const user = require.main.require('./src/user');.
    To update the username you will need a function from src/user/profile.js - user.updateProfile.
    But to do that you will need their uid - while I assume you only start with their (old) username. Fortunately there is a function getUidByUsername in src/user/index.js that works just like the name suggests.

    There is also another way to do what you want, however. There is a (write api plugin)[https://github.com/NodeBB/nodebb-plugin-write-api] you can use to change the username without writing your own plugin. Just get the uid from the standard read api (your.forum.com/api/user/userslug) and then make a PUT request to your.forum.com/api/v2/user/:uid (replacing :uid with the uid from the previous step, obviously).



  • Thank you very much @oplik0 for your very detailed and helpful answer. 👍 I will take a look into the write-api plugin first and see how far I get. From your description it sounds like what I need.


Log in to reply
 

Suggested Topics

  • 8
  • 3
  • 4
  • 12
  • 4
| |