How do i update traits field after login hook ?
# talk-kratos
f
How do i update traits field after login hook ?
b
f
I have used that but I get a violation error
Copy code
Here is my  request
fetch(
                    // `<https://kratos-admin.dev.ukama.com/admin/identities/${response.data.id}>`,
                    `<http://localhost:4433/.ory/kratos/public/admin/identities/${response.data.id}>`,
                    {
                        credentials: "include",
                        method: "PUT",
                        headers: {
                            Accept: "application/json",
                            "Content-Type": "application/json",
                        },

                        body: JSON.stringify({
                            schema_id: response.data.id,
                            state: "active",
                            traits: {
                                firstVisit: true,
                            },
                        }),
                    }
                )
                    .then(res => res.json())
                    .then(res => console.log("DATA", res));
b
Could you post the validation error as well?
It seems that you are sending the request from a browser? We highly advice against that, as that opens you up to a whole host of security issues.
f
Yes ,What is the best way to update the traits.
b
Well, if I understand your situation correctly, you have a service running somewhere that has a URL that is called by Ory Kratos after a user logged in, correct? You would then call the kratos API from that service (not a browser) to update the traits.
f
I have selfhosted kratos as a mean of authentication with a react app on the side. Currently everything is working fine. I would like to check if it's the first time the user login so I can show a different UI.
I have added a trait on kratos schema called
isFirstVisit
which I need to updated when a user login check if it is firstime
b
Yes, but for that you will need a separate service running somewhere to update the trait. It can’t be a browser, because you should not call the admin API from a user’s browser.
f
Is there a way I can update a trait from kratos config in webhook ?
b
Well yes, just as I described: You need a service that runs somewhere on the internet. You register the service as an after login webhook. Once you receive the call you then go ahead and call the Kratos Admin API from this service to update the trait. But you definitely need a separate service (for example written in JavaScript, Go, etc.) that handles this as it can not happen from a browser.
f
Thanks @bland-eye-99092 How do I register the service in webhook .
login:
ui_url: /auth/login
lifespan: 10m
b
Webhooks are described here: https://www.ory.sh/docs/kratos/hooks/configure-hooks For your case the
after login
hook is relevant. 🙂
f
I don't see any example when external service outside service is called in webhook here.😔
b
Here for example: https://www.ory.sh/docs/kratos/hooks/configure-hooks#webhooks For your case (with some annotations):
selfservice:
flows:
login:
after:
hooks:
- hook: web_hook _# To use webhooks, you must set 'hook' to 'web_hook'_
config:
url: <https://test.hook.site.sh/before_login_hook>
# ****** THIS WOULD BE YOUR URL *******
method: POST _# HTTP method used to send request to the webhook URL._
body: file:///path/of/my/jsonnet/file _# HERE YOU NEED TO DEFINE A BODY DEFINITION_
# Ignored when using an HTTP method that doesn't allow sending body. OPTIONAL
can_interrupt: true _# You Probably want to set this to false_
auth:
type: _# Can be one of 'basic_auth' or 'api_key'_
config: _# Additional auth config for the hook. Read next section for details._
f
Thanks @bland-eye-99092 I got it working on a separate service. Is there a way to set default value to a traits in the schema ?