Post Registration Hook - User Properties Not Pulling In

Hi,

We are currently using a post registration web-hook to do a bit of custom functionality.
The JSON used when testing the web-hook through the interface looks like this

{
  "user": {
    "id": "abc123",
    "tenant": "my-tenant",
    "name":"Testing",
    "username": "user1",
    "email": "user1@foo.com",
    "emailVerified": true,
    "phoneNumber": "1-000-000-0000",
    "phoneNumberVerified": true,
    "user_metadata": {
      "hobby": "surfing"
    },
    "app_metadata": {
      "plan": "full"
    }
  },
  "context": {
    "requestLanguage": "en-us",
    "connection": {
      "id": "con_xxxxxxxxxxxxxxxx",
      "name": "Username-Password-Authentication",
      "tenant": "my-tenant"
    }
  }
}'

When printing out the user object i can see that the name property is set. However when creating a user using the /dbconnections/signup on the management API, setting any of the other use properties such as name, given_name, nickname etc, these don’t pull through into the hook when i print out the user object.

It seems silly to have to set the name in the user_metadata property to access it

Thanks

Hi @darren.tiplady,

when i print out the user object.

How do you print out the user? Is this in your client application, or via console log in the hook and checking the webtask realtime logs (asking cause the latter wouldn’t work in a post-reg hook due to asynchronisity)?

What’s the code of your hook?

Hi Mat,

The user is being printed out when i was testing the hook via the webtask realtime logs. Screenshot below.

When testing from my application i let the code run through the management API’s endpoint

/dbconnections/signup

I then checked the log of the web-hook to see what the user object is that was printed out was. As an example this is what was printed out .

image

Webhook below.

/**
@param {object} user - The user being created
@param {string} user.id - user id
@param {string} user.tenant - Auth0 tenant name
@param {string} user.username - user name
@param {string} user.email - email
@param {boolean} user.emailVerified - is e-mail verified?
@param {string} user.phoneNumber - phone number
@param {boolean} user.phoneNumberVerified - is phone number verified?
@param {object} user.user_metadata - user metadata
@param {object} user.app_metadata - application metadata
@param {object} context - Auth0 connection and other context info
@param {string} context.requestLanguage - language of the client agent
@param {object} context.connection - information about the Auth0 connection
@param {object} context.connection.id - connection id
@param {object} context.connection.name - connection name
@param {object} context.connection.tenant - connection tenant
@param {object} context.webtask - webtask context
@param {function} cb - function (error, response)
*/
module.exports = function (user, context, cb) {

    console.log('User: o%', user);
    
    // Constants
    const request = require('request-promise');
    const sgMail = require('@sendgrid/mail');
    const auth0verificationEmailUrl = 'https://lawyerly-internal.eu.auth0.com/api/v2/email-templates/verify_email';
    const sendGridApiKey = context.webtask.secrets['sendgrid-apikey'];
    const firmSignupClientId = context.webtask.secrets['firm-signup-client-id'];
    const firmSignupClientSecret = context.webtask.secrets['firm-signup-client-secret'];
    
    // Email Replacement Variables
    const displayName = user.user_metadata.firstname;
    const user_id = 'auth0|' + user.id;
    var verify_email_Template;
    
    // Auth0 Access Token
    var accessToken;
    
    // Auth0 Access Token Request Options
    var accessTokenOptions = {
        method: 'POST',
        url: 'https://lawyerly-internal.eu.auth0.com/oauth/token',
        form:{
          grant_type: 'client_credentials',
          client_id: firmSignupClientId,
          client_secret: firmSignupClientSecret,
          audience: 'https://lawyerly-internal.eu.auth0.com/api/v2/'
        },
        resolveWithFullResponse: true
    };
  
    // Main Request
    request(accessTokenOptions)
        .then(function onSuccess(response) {
            if (response.statusCode == 400)
                console.log('Invalid model received %o', response);
            if (response.statusCode == 200) {
                // Save the access token
                accessToken = JSON.parse(response.body);
            }
            
            // Auth0 Verification Email Settings
            var verifyEmailOptions = {
                method: 'GET',
                url: auth0verificationEmailUrl,
                headers: { 'Authorization': 'bearer ' + accessToken.access_token },
                resolveWithFullResponse: true
            };
            
            // Get the verification email template
            request(verifyEmailOptions)
                .then(function onSuccess(response) {
                    if (response.statusCode == 400)
                        console.log('Invalid model received %o', response);
                    if (response.statusCode == 200) {
                        verify_email_Template = response.body.replace(/user_id/g, user_id)
                            .replace('{{ user.app_metadata.displayName }}', displayName);
                        
                        // Parse Template to JSON
                        jsonVerificationTemplate = JSON.parse(verify_email_Template);
        
                        // Setup SendGrid Email
                        sgMail.setApiKey(sendGridApiKey);
                        const msg = {
                            to: user.email,
                            from: jsonVerificationTemplate.from,
                            subject: jsonVerificationTemplate.subject,
                            content: [
                                {
                                    type: 'text/html',
                                    value: jsonVerificationTemplate.body
                                }]
                        };
        
                        // Send mail
                        sgMail
                            .send(msg)
                            .then(() => {
                                console.log('Success SendGrid Mail Sent');
                            })
                            .catch(error => {
                                //Log friendly error
                                console.error(error.toString());
        
                                //Extract error msg
                                const { message, code, response } = error;
        
                                //Extract response msg
                                const { headers, body } = response;
                            });
                    }
        
                    // Ensure callback is called on each path
                        cb();
                    }).catch(function onError(err) {
                        console.error(error.toString());
                        cb();
                    });
            
            
            // Ensure callback is called on each path
            cb();
        }).catch(function onError(err) {
            console.error(error.toString());
            cb();
        });
};

To add to this, user.picture is also not available during the post-registration hook even though it is part of the normalized user profile.

1 Like

Any updates on this please? I’m facing the same problem.

Same problem here. Any updates…? It’s been a while now

Any update on this? I want to customize login to have first/last name.
Adding them like this gives me the option to use { user } = useAuth0() hook with user.given_name
additionalSignUpFields: [{
name: “given_name”,
storage: “root”,
placeholder: “enter your first name”,
},
{
name: “family_name”,
storage: “root”,
placeholder: “Enter your last name”
}]

but with this I can’t do user.given_name in my post registration hook.

and if I don’t add them to root and add them to user_metadata I can’t get them with the user useAuth0 hook…

Hey there!

As this topic is related to Rules - Hooks - Actions and Rules & Hooks are being deprecated soon I’m excited to let you know about our next Ask me Anything session in the Forum on Thursday, January 18 with the Rules, Hooks and Actions team on Rules & Hooks and why Actions matter! Submit your questions in the thread above and our esteemed product experts will provide written answers on January 18. Find out more about Rules & Hooks and why Actions matter! Can’t wait to see you there!

Learn more here!