Hook for adding. user to my database via my API after auth0 registration

Hello there!
I am currently trying to figure out the best way to have auth0 ping the create method of my user API in my NESTJS service. the issue I’m having is that my create method has a authGaurd(‘jwt’) associated with it, so if a create a hook that pings it, it wont have a bearer token in the header to give it access, it takes the users name, email and sub from the JWT passed in the header. I’m sure this is a common enough thing to try to implement, but I have been struggling to follow the docs. If anyone has any clarity around the best way to do this that would be incredible help full, for reference, this is my create method in my user service: `

@UseGuards(AuthGuard('jwt'))
  @Post()
  create(@User() user): Promise<UserEntity> {
    const createUserDto: CreateUserDto = {
      name: user.name,
      email: user.email,
      sub: user.sub,
      isRegistered: true,
    };
    //return 'success';

    return this.userService.create(createUserDto);
  }`

and this is what I currently have as a hook in Auth0:


module.exports = function (user, context, cb) {

// Perform any asynchronous actions, e.g. send notification to Slack.

const axios = require("axios");

/**

* @param {Event} event - Details about registration event.

*/

exports.onExecutePostUserRegistration = async (event) => {

await axios.post("http://localhost:8000/user", {

params: {

name: event.user.name,

email: event.user.email,

sub: event.user.id,

}

});};

cb();

};
type or paste code here

Hi @nolanl13,

First, I would suggest using Actions for anything like this. Hooks is a legacy feature, and you will have to migrate to actions at some point in the future.

A bearer token in this type of scenario won’t typically contain the user information, because this is a Machine to Machine request. Usually, you would send the user data in a POST request body, and the token would simply authenticate the request.

Does that make sense?

Okay, i will move over to actions thank you for the tip!

I’m struggling to figure out the best way to do this with auth0, when would you suggest using organisations?
basically a user will be able to have a enterprise with team members that get added and invited with an admin being the one who created it. If I use organisations do I have to have multiple tenants? I am trying to get auth0 to notice when a user has signed up and ping my API, do I need to generate an access token to my API within the action before doing this if I am using JWT strategy?

@nolanl13,

This guide shows you how to set it up with a Rule. It’s the same premise, but using an Action.

Let me know if you have any questions about it!

thank you so much, this worked a charm!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.