Skip to content

Commit

Permalink
Cleans Create Identity Code
Browse files Browse the repository at this point in the history
  • Loading branch information
Aryan51203 committed Dec 21, 2023
1 parent 232627f commit e0d3251
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 34 deletions.
19 changes: 1 addition & 18 deletions api/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,6 @@ func HandleCreateIdentityFlow(c *gin.Context) {
return
}

var mappedJsonIdentity map[string]interface{}

data, err := json.Marshal(t)

if err != nil {
log.ErrorLogger("Unable to convert map to json", err)

errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0])
c.JSON(errCode, gin.H{
"error": err.Error(),
"message": "Unable to convert map to json",
})
return
}

err = json.Unmarshal(data, &mappedJsonIdentity)

if err != nil {
log.ErrorLogger("Unable to convert JSON to map", err)

Expand All @@ -59,7 +42,7 @@ func HandleCreateIdentityFlow(c *gin.Context) {
return
}

createdIdentity, r, err := admin.CreateIdentityFlowWrapper(mappedJsonIdentity)
createdIdentity, r, err := admin.CreateIdentityFlowWrapper(t)

if err != nil {
log.ErrorLogger("Error while calling `AdminCreateIdentity`", err)
Expand Down
23 changes: 17 additions & 6 deletions pkg/wrapper/kratos/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,27 @@ import (
client "github.com/ory/client-go"

"github.com/sdslabs/nymeria/config"
"github.com/sdslabs/nymeria/pkg/middleware"
)

func CreateIdentityFlowWrapper(identityMap map[string]interface{}) (*client.Identity, *http.Response, error) {
apiClient := client.NewAPIClient(config.KratosClientConfigAdmin)
func CreateIdentityFlowWrapper(data Identity) (*client.Identity, *http.Response, error) {
timeStamp := middleware.CurrentTimeStamp()

trait := map[string]interface{}{
"email": data.Email,
"name": data.Name,
"password": data.Password,
"phone_number": data.PhoneNumber,
"active": true,
"verified": false,
"role": data.Role,
"created_at": timeStamp,
"totp_enabled": false,
}

adminCreateIdentityBody := *client.NewAdminCreateIdentityBody(
"default",
identityMap,
) // AdminCreateIdentityBody | (optional)
adminCreateIdentityBody := *client.NewAdminCreateIdentityBody("default", trait) // AdminCreateIdentityBody | (optional)

apiClient := client.NewAPIClient(config.KratosClientConfigAdmin)
createdIdentity, r, err := apiClient.V0alpha2Api.AdminCreateIdentity(context.Background()).AdminCreateIdentityBody(adminCreateIdentityBody).Execute()

return createdIdentity, r, err
Expand Down
15 changes: 5 additions & 10 deletions pkg/wrapper/kratos/admin/types.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
package admin

type Identity struct {
Name string `json:"name"`
Email string `json:"email"`
Phone_number string `json:"phone_number"`
Password string `json:"password"`
Image_url string `json:"img_url"`
Active bool `json:"active"`
Verified bool `json:"verified"`
Role string `json:"role"`
Created_at string `json:"created_at"`
Totp_enabled bool `json:"totp_enabled"`
Name string `json:"name"`
Email string `json:"email"`
PhoneNumber string `json:"phone_number"`
Password string `json:"password"`
Role string `json:"role"`
}

0 comments on commit e0d3251

Please sign in to comment.