<@U04UQ68083H> I am currently building kratos, ket...
# ory-copilot
a
@chilly-king-10285 I am currently building kratos, keto and oathkeeper and trying to make it work. I have an after registration hook which works fine if I do the API request by myself but If I use the Ory SDK, it seems not to work. Thats as far as I can go looking at the source code. May someone look at my code and tell me, what I did wrong?
Copy code
This does not works:
func (kc *KratosClient) UpdateAdminMetaData(ctx context.Context, signUpRequest requests.SignUpRequest, role string) (interface{}, error) {
	// Construct the traits from the signUpRequest
	traits := map[string]interface{}{
		"email":           signUpRequest.Email,
		"firstname":       signUpRequest.Firstname,
		"lastname":        signUpRequest.Lastname
	}

	// Convert traits to json.RawMessage
	// traitsBytes, err := json.Marshal(traits)
	// if err != nil {
	// 	kc.logger.Error().Msgf("Error marshalling traits: %v", err)
	// 	return nil, err
	// }

	// Set up the metadata you want to add
	metadataAdmin := AdminMetadata{
		Role:      role,
	}

	<http://kc.logger.Info|kc.logger.Info>().Msgf("Metadata admin: %v", metadataAdmin)
	// Create a new Identity object
	identity := &ory.UpdateIdentityBody{}
	<http://kc.logger.Info|kc.logger.Info>().Msgf("Identity: %v", identity)
	// Set the state, schema ID, and traits
	identity.SetState("active")     
	identity.SetSchemaId("default") 
	identity.SetTraits(traits)
	// var traitsMap map[string]interface{}
	// err = json.Unmarshal(traitsBytes, &traitsMap)
	// if err != nil {
	// 	kc.logger.Error().Msgf("Error unmarshalling traits: %v", err)
	// 	return nil, err
	// }

	// identity.SetTraits(traitsMap)

	// Set the admin metadata
	identity.SetMetadataAdmin(metadataAdmin)

	<http://kc.logger.Info|kc.logger.Info>().Msgf("Identity end: %v", identity)
	// Use the Ory SDK to update the identity
	updatedIdentity, res, err := kc.oryClient.IdentityAPI.UpdateIdentity(ctx, signUpRequest.IdentityId.String()).UpdateIdentityBody(*identity).Execute()
	if err != nil {
		kc.logger.Error().Msgf("Error updating identity metadata: %v", err)
		kc.logger.Error().Msgf("Response: %v", res)
		kc.logger.Error().Msgf("Updated identity: %v", updatedIdentity)
		return nil, err
	}

	<http://kc.logger.Info|kc.logger.Info>().Msg("Admin metadata updated successfully")
	return identity, nil
}
and here the logging I got:
Copy code
{"level":"info","time":"2023-11-21T18:47:46+01:00","message":"Identity: &{<nil> <nil> <nil>   map[] map[]}"}
{"level":"info","time":"2023-11-21T18:47:46+01:00","message":"Identity end: &{<nil> {ADMIN} <nil> default active map[email:popoip@poas.de firstname:okdas lastname:dasopk position:dalksm,] map[]}"}
{"level":"error","time":"2023-11-21T18:47:46+01:00","message":"Error updating identity metadata: undefined response type"}
{"level":"error","time":"2023-11-21T18:47:46+01:00","message":"Response: &{404 Not Found 404 HTTP/1.1 1 1 map[Content-Length:[19] Content-Type:[text/plain; charset=utf-8] Date:[Tue, 21 Nov 2023 17:47:46 GMT] X-Content-Type-Options:[nosniff]] {404 page not found\n} 19 [] false false map[] 0x1400057e400 <nil>}"}
{"level":"error","time":"2023-11-21T18:47:46+01:00","message":"Updated identity: <nil>"}
{"level":"error","time":"2023-11-21T18:47:46+01:00","message":"Error updating admin metadata: undefined response type"}
c
A message from kapa.ai
Based on the information provided in the extracts, it seems like you're trying to update the admin metadata of an identity using the Ory SDK. However, the error message you're receiving suggests that the server is returning a 404 Not Found error. This typically means that the URL you're trying to reach does not exist. In your code, you're trying to update an identity using the
UpdateIdentity
method:
Copy code
go updatedIdentity, res, err := kc.oryClient.IdentityAPI.UpdateIdentity(ctx, signUpRequest.IdentityId.String()).UpdateIdentityBody(*identity).Execute()
According to the [Ory documentation](https://www.ory.sh/docs/kratos/manage-identities/managing-users-identities-metadata), to update the metadata of an identity, you should use the
/admin/identities/{id}
endpoint. Make sure that the
signUpRequest.IdentityId.String()
in your code corresponds to a valid identity ID in your system. Also, ensure that the server URL you're using is correct. In the [Ory Kratos Go SDK setup example](https://www.ory.sh/docs/kratos/self-hosted/go), the server URL is set to
<http://127.0.0.1:4434>
, which is the Kratos Admin API. Make sure that your
oryClient
is configured with the correct server URL. If the issue persists, it might be helpful to check the server logs or debug the server to see why it's returning a 404 error.
a
@chilly-king-10285 thats how i connect to admin api type KratosClient struct { oryClient *ory.APIClient adminAPIURL string publicAPIURL string httpClient *http.Client logger *log.Logger } func NewKratosClient(adminAPIURL string, publicAPIURL string, logger *log.Logger) *KratosClient { conf := ory.NewConfiguration() conf.Servers = ory.ServerConfigurations{ {URL: adminAPIURL}, } apiClient := ory.NewAPIClient(conf) return &KratosClient{ oryClient: apiClient, adminAPIURL: adminAPIURL, publicAPIURL: publicAPIURL, httpClient: &http.Client{}, logger: logger, } } the adminAPIURL is correctly set in my env variables as adminApiUrl: http://127.0.0.1:4434/
c
A message from kapa.ai
Your code for connecting to the admin API seems correct. You are correctly initializing the
oryClient
with the
adminAPIURL
and setting it in the
KratosClient
struct. However, the 404 error you're encountering when trying to update an identity suggests that the server can't find the requested resource. This could be due to a few reasons: 1. The identity ID you're trying to update does not exist. Make sure that
signUpRequest.IdentityId.String()
corresponds to a valid identity ID in your system. 2. The endpoint or method you're trying to access does not exist. According to the [Ory Kratos API reference](https://www.ory.sh/docs/reference/api), the correct endpoint to update an identity is
/identities/{id}
. Make sure that the
UpdateIdentity
method in the Ory SDK corresponds to this endpoint. 3. There might be an issue with the Ory server itself. Check the server logs or debug the server to see why it's returning a 404 error. If you're still encountering issues, you might want to reach out to the Ory community on [Ory Community Slack](http://slack.ory.sh/) for further assistance.
a
never mind, i found the problem