How can I update(user profile) family_name and given_name for Regular Web Applications

In Post-login trigger I created an action that collects family_name and given_name from a form.

 exports.onExecutePostLogin = async (event, api) => {
 const form_id = 'form_id';
 if(event.user.given_name==null || event.user.family_name==null)
 {
    api.prompt.render(form_id);
  }else{
     return;
  }
};

exports.onContinuePostLogin = async (event, api) => {  
 const givenName = event.prompt.fields.given_name;
 const familyName = event.prompt.fields.family_name;

 const ManagementClient = require('auth0').ManagementClient;

 const management = new ManagementClient({
     domain: event.secrets.domain,
     clientId: event.secrets.clientId,
     clientSecret: event.secrets.clientSecret
 });

 const params =  { id: event.user.user_id};

 const data = {
   "family_name" : familyName,
   "given_name" : givenName
 }
 try{
   const res = await management.users.update(params, data);
   console.log("response ", res);
   
 }catch(e){
   console.log('logged error is', e);
 }
 return;
};

I have added the Dependencies auth0@4.18.0 to the action and secrets as well.

When I try to update/create user profile with family_name and given_name I’m getting the error below.

logged error is AuthApiError: Grant type ‘client_credentials’ not allowed for the client.
at OAuth.parseError (/data/layers/core.windows.net/K5K9mAdro_Ps3rfuIeWZNilCuA4S63U8XEGvr8a7YI0=/node_modules/auth0/dist/cjs/auth/base-auth-api.js:43:16)
.
.
.
at async UsersManager.update (/data/layers/core.windows.net/K5K9mAdro_Ps3rfuIeWZNilCuA4S63U8XEGvr8a7YI0=/node_modules/auth0/dist/cjs/management/__generated/managers/users-manager.js:561:26)
at async exports.onContinuePostLogin (/data/io/node22/ec817423-b816-47eb-9855-058bd48631e9/webtask.js:40:20) {
error: ‘unauthorized_client’,
error_description: “Grant type ‘client_credentials’ not allowed for the client.”,
statusCode: 403,
body: {"error":"unauthorized_client","error_description":"Grant type 'client_credentials' not allowed for the client.","error_uri":"https://manage.teranet.auth0app.com/docs/clients/client-grant-types"},
.
.

I tried to enable Client Credentials in Applications → Your Application → Advanced Settings → Grant Types, but this type is in disabled status since it is not M2M.

Please let me know how to update (or create if family/given name does not exist) user profile.

Hi @criz.c

Welcome to the Auth0 Community!

In order to update or add data for the user, there is no need to create a ManagementClient. You can add these values inside user metadata in the ContinuePostLoginAction to be easily accessible:

  api.user.setUserMetadata('company_name', event.prompt.fields.company_name);

You can read more about this in our documentation.

You can also update that information directly during the login process:

event.user.family_name = event.prompt.fields.family_name;
event.user.given_name = event.prompt.fields.given_name;

Alternatively, you can do this inside the form directly, by creating a flow which contains similar code:

This would be the form:

This is the output inside the user raw JSON after a login:

"given_name": "Daniel",
 "family_name": "Smith",

Hope this helps! If you have any other questions, feel free to leave a reply!

Kind Regards,
Nik

Hi Nik,

Thanks for your reply. I want the names inside the root user profile, not in user_metadata, like below.

Raw JSON.
{
“email”: “username@gmail.com”,
“email_verified”: true,
“given_name”: “firstName”,
“family_name”: “secondName”,
“name”: “firstName secondName”,
“user_metadata”: {},
“created_at”: “2025-02-09T23:37:07.665Z”,
“updated_at”: “2025-02-25T22:09:33.638Z”
}

I tried your suggestion inside onContinuePostLogin as below

exports.onContinuePostLogin = async (event, api) => {  
event.user.family_name = event.prompt.fields.family_name;
event.user.given_name = event.prompt.fields.given_name;

};

It gives me the error warning ‘Use api.* to apply changes’.

Please keep in mind initially given_name and family_name is not inside the User Profile(Raw JSON).

I really appreciate your time!
Thanks,

Hi @criz.c

Thanks for the additional info.

If you want to have them in the root user profile, you can follow the suggestion above where you can update the user data directly in the form using an Update User Flow. That way, those attributes will be in the root profile and also visible in the RAW JSON in the dashboard.

Alternatively, you can do this inside the form directly, by creating a flow which contains similar code:

This would be the form:

This is the output inside the user raw JSON after a login:

"given_name": "Daniel",
 "family_name": "Smith",

Kind Regards,
Nik

I have added the flow after my form as below.

And then added code inside flow as you have mentioned

My Action is

 exports.onExecutePostLogin = async (event, api) => {
 const form_id = 'ap_intDpyXxEiFZMFos8WMi32';
 if(event.user.given_name==null || event.user.family_name==null)
   {
      api.prompt.render(form_id);
   }
};

exports.onContinuePostLogin = async (event, api) => {}

If I don’t write onContinuePostLogin method inside Post-login trigger action, I get the following error.

“description”: “Invalid function signature for the "onContinuePostLogin" handler. Use "exports.onContinuePostLogin = async function(event, api) { }" as the signature.”,

I have included the form in json format in case if it helps to investigate.

   		{
	  "version": "4.0.0",
	  "form": {
		"name": "Blank form",
		"languages": {
		  "primary": "en"
		},
		"nodes": [
		  {
			"id": "step_xTZy",
			"type": "STEP",
			"coordinates": {
			  "x": 500,
			  "y": 0
			},
			"alias": "Update User",
			"config": {
			  "components": [
				{
				  "id": "given_name",
				  "category": "FIELD",
				  "type": "TEXT",
				  "label": "Given Name",
				  "required": true,
				  "sensitive": false,
				  "config": {
					"multiline": false,
					"placeholder": "Enter your first name",
					"min_length": 1,
					"max_length": 75
				  }
				},
				{
				  "id": "family_name",
				  "category": "FIELD",
				  "type": "TEXT",
				  "label": "Surname Name",
				  "required": true,
				  "sensitive": false,
				  "config": {
					"multiline": false,
					"placeholder": "Enter your second name",
					"min_length": 1,
					"max_length": 75
				  }
				},
				{
				  "id": "next_button_ZWmh",
				  "category": "BLOCK",
				  "type": "NEXT_BUTTON",
				  "config": {
					"text": "Continue"
				  }
				}
			  ],
			  "next_node": "flow_LXZk"
			}
		  },
		  {
			"id": "flow_LXZk",
			"type": "FLOW",
			"coordinates": {
			  "x": 1025,
			  "y": 217
			},
			"alias": "Update flow",
			"config": {
			  "flow_id": "#FLOW-1#",
			  "next_node": "$ending"
			}
		  }
		],
		"start": {
		  "next_node": "step_xTZy",
		  "coordinates": {
			"x": 0,
			"y": 0
		  }
		},
		"ending": {
		  "resume_flow": true,
		  "coordinates": {
			"x": 1361,
			"y": 32
		  }
		}
	  },
	  "flows": {
		"#FLOW-1#": {
		  "name": "Update user_profile",
		  "actions": [
			{
			  "id": "update_user_TXoV",
			  "type": "AUTH0",
			  "action": "UPDATE_USER",
			  "allow_failure": true,
			  "mask_output": true,
			  "params": {
				"connection_id": "#CONN-1#",
				"user_id": "{{context.user.user_id}}",
				"changes": {
				  "given_name": "{{fields.given_name}}",
				  "family_name": "{{fields.gamily_name}}"
				}
			  }
			}
		  ]
		}
	  },
	  "connections": {
		"#CONN-1#": {
		  "id": "ac_e7DPpp9JPpHUFF64eb3ebU",
		  "app_id": "AUTH0",
		  "name": "Auth0"
		}
	  }
	}

Also when I tried to add vault connection I get the error message as below

So it did not update user profile with family name and given name.

Do I have to add any code inside onContinuePostLogin to update with new values and fields.

Also I have three applications where users log-in and Need to do the same for all the users regardless of application to which they log-in.

Thank you,

Hi again

In order for you to use a flow, it needs a valid vault connection. It appears that the credentials that you are using are invalid when creating the vault. Please use the credentials of the API Explorer application.

Also, I can see that you perhaps misspelled the variable for the family name field:

"family_name": "{{fields.gamily_name}}" -> "family_name": "{{fields.family_name}}"

Also, make sure that the id of the specified fields are the ones you use in the Update User flow, otherwise the execution will throw in an error. Also, make sure to change the id of the text components inside the step.

When rendering the form, you can copy paste the rendering provided by the Forms feature and just add the condition for trigger. Since a form is possibly rendered, the exports.onContinuePostLogin is required.

I have imported your form on my end, changed the misspelled variable in the flow and it executed and updated the user with the info which is visible in the user raw JSON.

Hope this helps!

Kind Regards,
Nik

Thanks for notifying me the misspell. Also I could do the vault connection successfully and the user info was updated in user profile.

Is it possible from Post-User-Registration do the same?

My Post User Registration Action is

exports.onExecutePostUserRegistration = async (event, api) => {
   const FORM_ID = 'ap_intDpyXxEiFZMFos8WMi32';
   if(!event.user.given_name || !event.user.family_name){
      api.prompt.render(FORM_ID);    
    }
};

but it show the error below

Property ‘prompt’ does not exist on type ‘PostUserRegistrationAPI’.

The forms cannot be rendered in PostUserRegistration triggers, they can only be rendered in PostLogin triggers.

If you wish for the form to be rendered upon registration, I would advise to check the login count of the user instead of checking if they have an assigned given and family name.

You could do something like:

if(event.user.logins_count === 1){
      api.prompt.render(FORM_ID);    
}

A newly registered user will always have a login count of 1 since the moment they sign up, that is considered their first successful login. However, that might not be suitable for existing members. If you have existing members, your approach would be more suitable. Also, the PostLogin trigger will work even on signup.

If you need any other clarifications, let me know!

Kind Regards,
Nik

1 Like

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