Skip to content

Commit

Permalink
fix: keep HTML form type on registration error (#698)
Browse files Browse the repository at this point in the history
closes #670

Co-authored-by: hackerman <3372410+aeneasr@users.noreply.github.com>
  • Loading branch information
zepatrik and aeneasr committed Sep 24, 2020
1 parent b84e659 commit 6c9e756
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
1 change: 0 additions & 1 deletion selfservice/form/html_form.go
Expand Up @@ -285,7 +285,6 @@ func (c *HTMLForm) SetValue(name string, value interface{}) {

if f := c.getField(name); f != nil {
f.Value = value
f.Type = toFormType(name, value)
return
}
c.Fields = append(c.Fields, Field{
Expand Down
3 changes: 2 additions & 1 deletion selfservice/strategy/password/registration.go
Expand Up @@ -58,7 +58,8 @@ func (s *Strategy) handleRegistrationError(w http.ResponseWriter, r *http.Reques

if p != nil {
for _, field := range form.NewHTMLFormFromJSON("", p.Traits, "traits").Fields {
method.Config.SetField(field)
// we only set the value and not the whole field because we want to keep types from the initial form generation
method.Config.SetValue(field.Name, field.Value)
}
}

Expand Down
4 changes: 4 additions & 0 deletions selfservice/strategy/password/registration_test.go
Expand Up @@ -104,6 +104,9 @@ func TestRegistration(t *testing.T) {
})

t.Run("case=should show the error ui because the request payload is malformed", func(t *testing.T) {
viper.Set(configuration.ViperKeyDefaultIdentitySchemaURL, "file://./stub/profile.schema.json")
defer viper.Set(configuration.ViperKeyDefaultIdentitySchemaURL, "file://./stub/registration.schema.json")

t.Run("type=api", func(t *testing.T) {
f := testhelpers.InitializeRegistrationFlowViaAPI(t, apiClient, publicTS)
c := testhelpers.GetRegistrationFlowMethodConfig(t, f.Payload, identity.CredentialsTypePassword.String())
Expand All @@ -121,6 +124,7 @@ func TestRegistration(t *testing.T) {
assert.Contains(t, res.Request.URL.String(), uiTS.URL+"/registration-ts")
assert.NotEmpty(t, gjson.Get(body, "id").String(), "%s", body)
assert.Contains(t, gjson.Get(body, "methods.password.config.messages.0.text").String(), "invalid URL escape", "%s", body)
assert.Equal(t, "email", gjson.Get(body, "methods.password.config.fields.#(name==\"traits.email\").type").String(), "%s", body)
})
})

Expand Down
17 changes: 9 additions & 8 deletions selfservice/strategy/password/stub/profile.schema.json
Expand Up @@ -7,18 +7,19 @@
"traits": {
"type": "object",
"properties": {
"email": {
"type": "string",
"ory.sh/kratos": {
"credentials": {
"password": {
"identifier": true
"email": {
"type": "string",
"format": "email",
"ory.sh/kratos": {
"credentials": {
"password": {
"identifier": true
}
}
}
}
}
}
}
}
},
"additionalProperties": false
}

0 comments on commit 6c9e756

Please sign in to comment.