<@U04UQ68083H> Is there a way to store information...
# ory-copilot
q
@chilly-king-10285 Is there a way to store information in identity regarding which app was used to log into kratos when there are multiple apps that use the same server ?
c
A message from kapa.ai
Hmm, I don't know enough to give you a confident answer yet. However, Ory Kratos does allow for customization of identity schemas, which could potentially be used to store such information. You can find more details on how to customize identity schemas in the [Ory documentation](https://www.ory.sh/docs/kratos/manage-identities/customize-identity-schema#examples).
q
@chilly-king-10285 I want to store information regarding the app from which the user was redirected to kratos for login. How to do this
c
A message from kapa.ai
Based on the provided knowledge sources, Ory Kratos does not directly support storing information about the app from which the user was redirected for login. However, you might be able to achieve this by customizing the identity schema or by using the
request_url
parameter. The
request_url
parameter is the initial URL that was requested from Ory Kratos and can be used to forward information contained in the URL's path or query. This could potentially be used to track the app from which the user was redirected. You can find more about this in the [Ory Kratos API Reference](https://www.ory.sh/docs/kratos/reference/api). For more complex requirements, you might need to customize the identity schema. The identity schema in Ory Kratos is flexible and can be extended with custom fields. You could potentially add a field to store the information about the app from which the user was redirected. More details on how to customize the identity schema can be found in the [Ory Kratos documentation](https://www.ory.sh/docs/kratos/manage-identities/customize-identity-schema#examples). Please note that these are potential solutions and their feasibility would depend on your specific use case and setup.
q
@chilly-king-10285 How to store extra info in identity in metadata using HTTP req ? Give me example code
c
A message from kapa.ai
To store extra information in the identity metadata, you can use the Ory Kratos Admin API. The metadata can be updated using the
PUT /admin/identities/{id}
endpoint. Here is an example of how you might structure your HTTP request:
Copy code
json PUT /admin/identities/{id} Content-Type: application/json Authorization: Bearer API_KEY  {   "schema_id": "default",   "traits": {     "email": "<mailto:user@example.com|user@example.com>",     "name": "User Name"   },   "state": "active",   "metadata_public": {     "app": "App Name"   } }
In this example,
{id}
should be replaced with the ID of the identity you want to update, and
API_KEY
should be replaced with your actual API key. The
metadata_public
field is being used to store the name of the app. Please note that you need an API Key to call these endpoints. You can read more about [Authorization with API Keys](https://www.ory.sh/docs/concepts/personal-access-token) in the Ory documentation. Also, remember that metadata is not validated using the identity's JSON schema. You must ensure that the metadata you store is valid according to your schema and you must keep the schema up to date to accommodate changes in the data. For more information, refer to the [Ory Kratos API Reference](https://www.ory.sh/docs/kratos/reference/api).
q
@chilly-king-10285 How do I make this HTTP req after the user goes for sign up ? I am using self-service-ui-node
c
A message from kapa.ai
To make an HTTP request after a user signs up, you can use hooks in Ory Kratos. Hooks allow you to run custom logic before and after a Registration Flow. Here is an example of how you might use a hook to make an HTTP request after a user signs up:
Copy code
javascript const axios = require('axios');  // This is a hypothetical hook function async function afterRegistrationHook(request, response) {   const identityId = request.identity.id;    const updateResponse = await axios.put(`<https://your-kratos-server.com/admin/identities/${identityId}>`, {     schema_id: 'default',     traits: request.identity.traits,     state: 'active',     metadata_public: {       app: 'App Name'     }   }, {     headers: {       'Content-Type': 'application/json',       'Authorization': 'Bearer API_KEY'     }   });    // Handle the response from the update request   if (updateResponse.status === 200) {     console.log('Identity updated successfully');   } else {     console.error('Failed to update identity');   } }  // Use the hook function in your registration flow <http://app.post|app.post>('/auth/registration', (req, res) => {   // Registration logic here...    // Call the hook function after registration   afterRegistrationHook(req, res); });
In this example, replace
'<https://your-kratos-server.com>'
with the URL of your Ory Kratos server and
'Bearer API_KEY'
with your actual API key. Please note that this is a simplified example and you might need to adjust it according to your specific setup and requirements. For more information about hooks, please refer to the [Ory Kratos documentation](https://www.ory.sh/docs/kratos/hooks/configure-hooks).
q
@chilly-king-10285 In my kratos.yml I have added a web_hook in registration. I want it to add a metadata public into identity which is an array with the request_url in it. Can you give me jsonnet for it ?
c
A message from kapa.ai
Sure, you can use Jsonnet to customize the request body of your webhook. Here's an example of how you might structure your Jsonnet code to add the
request_url
to an array in `metadata_public`:
Copy code
jsonnet function(ctx) {   identity: {     metadata_public: {       request_urls: [ctx.request_url]     }   } }
In this example,
ctx.request_url
is the URL of the request that initiated the flow. This value is automatically provided by Ory Kratos and is available in the
ctx
object. This Jsonnet code will result in a JSON object that looks like this:
Copy code
json {   "identity": {     "metadata_public": {       "request_urls": ["<http://example.com/auth/registration>"]     }   } }
Please note that this is a simplified example and you might need to adjust it according to your specific setup and requirements. For more information about using Jsonnet with Ory Kratos, please refer to the [Ory Kratos documentation](https://www.ory.sh/docs/kratos/reference/jsonnet).
q
@chilly-king-10285 If I do this will my traits be overwritten ?
c
A message from kapa.ai
No, your traits will not be overwritten. The Jsonnet code provided only modifies the
metadata_public
field of the identity. It does not affect the
traits
field. However, if you want to modify the
traits
field in the future, be aware that when you use the
PUT /admin/identities/{{id}}
endpoint, the entire identity object is replaced with the one provided in the request body. So, if you want to keep the existing traits, you need to include them in your request body. For more information, refer to the [Ory Kratos API Reference](https://www.ory.sh/docs/kratos/reference/api).
q
Copy code
function(ctx) {
  identity: {
    metadata_public: {
      request_urls: [ctx.request_url]
    }
@chilly-king-10285 Do I need to have userId in this ?
c
A message from kapa.ai
No, you don't need to include
userId
in this Jsonnet code. The
userId
is not required for adding the
request_url
to the
metadata_public
field of the identity. However, if you want to include the
userId
for some reason, you can access it using
ctx.identity.id
. Here's how you could modify the Jsonnet code to include the `userId`:
Copy code
jsonnet function(ctx) {   identity: {     id: ctx.identity.id,     metadata_public: {       request_urls: [ctx.request_url]     }   } }
This will add the
userId
to the identity object in the request body. But remember, this is not necessary for adding the
request_url
to the
metadata_public
field. For more information about using Jsonnet with Ory Kratos, please refer to the [Ory Kratos documentation](https://www.ory.sh/docs/kratos/reference/jsonnet).
q
@chilly-king-10285 Is this correct registration: ui_url: http://localhost:4455/ui/registration login_hints: true enable_legacy_one_step: false after: hooks: - hook: web_hook config: url: http://localhost:4455/admin/identities/{id} method: PUT body: file:///etc/config/kratos/metadata.jsonnet response: parse: true
c
A message from kapa.ai
Your configuration seems mostly correct, but there are a few things to note: 1. The
<http://localhost:4455/ui/registration>
and
<http://localhost:4455/admin/identities/{id}>
should not be enclosed in
< >
. They should be just
<http://localhost:4455/ui/registration>
and
<http://localhost:4455/admin/identities/{id}>
respectively. 2. The
{id}
in the URL
<http://localhost:4455/admin/identities/{id}>
is a placeholder and should be replaced with the actual ID of the identity you want to update. 3. Make sure the file
metadata.jsonnet
exists at the path
/etc/config/kratos/
. Here's the corrected configuration:
Copy code
yaml registration:   ui_url: <http://localhost:4455/ui/registration>   login_hints: true   enable_legacy_one_step: false   after:     hooks:     - hook: web_hook       config:         url: <http://localhost:4455/admin/identities/{id}>         method: PUT         body: file:///etc/config/kratos/metadata.jsonnet         response:           parse: true
Remember to replace
{id}
with the actual ID of the identity you want to update. For more information, refer to the [Ory Kratos documentation](https://www.ory.sh/docs/kratos/reference/configuration).
q
url: http://localhost:4455/admin/identities/{ctx.identity.id} Will this work @chilly-king-10285
c
A message from kapa.ai
No, this will not work. The
url
field in the configuration file does not support dynamic values or expressions like
{ctx.identity.id}
. It should be a static URL. If you want to update the identity after registration, you might need to handle this in your application code. After the user registers, you can use the identity ID returned by Ory Kratos to make a
PUT
request to the
<http://localhost:4455/admin/identities/{id}>
endpoint. For more information, refer to the [Ory Kratos documentation](https://www.ory.sh/docs/kratos/reference/configuration).