<@U04UQ68083H> What is the ctx object in the pre-r...
# ory-copilot
b
@chilly-king-10285 What is the ctx object in the pre-registration hook?
b
A message from kapa.ai
Based on the knowledge sources, I can provide information about the
ctx
object in pre-registration hooks for Ory. In Ory webhooks, which can be used as pre-registration hooks, the
ctx
object contains information about the registration flow and request details. The
ctx
object binds the following data: - `flow`: Contains details about the registration flow including ID, expiration time, UI nodes, etc. - `request_headers`: Contains HTTP headers from the request - `request_method`: The HTTP method used (e.g., POST) - `request_url`: The URL of the request For the pre-registration hook specifically (which uses the "before" execution path), the
ctx
object doesn't include the
identity
object since the identity hasn't been created yet. The
identity
object is only available in the "after" execution path. Here's an example of what the
ctx
object might look like in a pre-registration hook:
Copy code
json {   "ctx": {     "flow": {       "expires_at": "2023-01-31T12:19:35.782238Z",       "id": "cec1c06e-48eb-4f9d-abf1-2e287371f4eb",       "issued_at": "2023-01-31T11:19:35.782238Z",       "oauth2_login_challenge": null,       "request_url": "<https://playground.projects.oryapis.com/self-service/registration/browser?return_to=>",       "transient_payload": {         "custom_data": "test"       },       "type": "browser",       "ui": {         "action": "<http://localhost:4455/self-service/registration?flow=cec1c06e-48eb-4f9d-abf1-2e287371f4eb>",         "method": "POST",         "nodes": [           // UI nodes for the registration form         ]       }     },     "request_headers": {       "Accept": [         "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"       ],       // Other headers     },     "request_method": "POST",     "request_url": "<https://playground.projects.oryapis.com/self-service/registration?flow=cec1c06e-48eb-4f9d-abf1-2e287371f4eb>"   } }
You can access these fields in your Jsonnet template for customizing webhook requests, for example:
Copy code
jsonnet function(ctx) {   customData: ctx.flow.transient_payload.custom_data   // Other fields you want to include }
[Trigger custom logic and integrate with external systems with webhooks](https://www.ory.sh/docs/guides/integrate-with-ory-cloud-through-webhooks#jsonnet-templating) provides more details about the available fields and how to use them in webhook templates.
b
@chilly-king-10285 Can we patch metadata in the response of a pre-registration hook?
b
A message from kapa.ai
Yes, you can modify identity metadata in the response of a pre-registration webhook in Ory. This is possible by configuring your webhook to parse the response and returning the appropriate JSON structure. To enable this functionality, you need to: 1. Configure your webhook with response parsing enabled:
Copy code
hook: web_hook config:   response:     parse: true
2. Return a 200 OK response from your webhook with a JSON object that includes the
identity
key containing the metadata you want to set:
Copy code
json {   "identity": {     "metadata_public": {       "the_webhook": "changed this value",       "and_added_this_one": "too"     },     "metadata_admin": {       "the_webhook": "updated this value",       "and_this_one": "too"     }   } }
It's important to note that this method replaces the entire metadata object. You cannot update only a single value in
metadata_public
or
metadata_admin
- you need to include all values you want to preserve. This functionality is specifically mentioned in the documentation as being available during registration flows: > Modifying the identity is currently only possible during the registration and settings flows. [Trigger custom logic and integrate with external systems with webhooks](https://www.ory.sh/docs/guides/integrate-with-ory-cloud-through-webhooks#modify-identities) provides detailed information about modifying identities through webhooks, including updating metadata. Additionally, if you need to pass complex data types in the metadata, you can use the schema-less nature of the metadata fields, as they support JSON objects with additional properties.