Skip to content

Commit

Permalink
Apply email_verified workaround
Browse files Browse the repository at this point in the history
  • Loading branch information
Serjlee committed Mar 17, 2023
1 parent 58363e5 commit 3358f4a
Showing 1 changed file with 53 additions and 1 deletion.
54 changes: 53 additions & 1 deletion management/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ type User struct {
// True if the user's email is verified, false otherwise. If it is true then
// the user will not receive a verification email, unless verify_email: true
// was specified.
EmailVerified *bool `json:"email_verified,omitempty"`
EmailVerified *bool `json:"-"`

// If true, the user will receive a verification email after creation, even
// if created with email_verified set to true. If false, the user will not
Expand All @@ -87,6 +87,58 @@ type User struct {
Blocked *bool `json:"blocked,omitempty"`
}

// UnmarshalJSON is a custom deserializer for the User type.
//
// We have to use a custom one due to possible inconsistencies in value types.
func (u *User) UnmarshalJSON(b []byte) error {
type user User
type userAlias struct {
*user
RawEmailVerified interface{} `json:"email_verified,omitempty"`
}

alias := &userAlias{(*user)(u), nil}

err := json.Unmarshal(b, alias)
if err != nil {
return err
}

if alias.RawEmailVerified != nil {
var emailVerified bool
switch rawEmailVerified := alias.RawEmailVerified.(type) {
case bool:
emailVerified = rawEmailVerified
case string:
emailVerified, err = strconv.ParseBool(rawEmailVerified)
if err != nil {
return err
}
default:
panic(reflect.TypeOf(rawEmailVerified))
}
alias.EmailVerified = &emailVerified
}

return nil
}

// MarshalJSON is a custom serializer for the User type.
func (u *User) MarshalJSON() ([]byte, error) {
type user User
type userAlias struct {
*user
RawEmailVerified interface{} `json:"email_verified,omitempty"`
}

alias := &userAlias{user: (*user)(u)}
if u.EmailVerified != nil {
alias.RawEmailVerified = u.EmailVerified
}

return json.Marshal(alias)
}

type UserIdentity struct {
Connection *string `json:"connection,omitempty"`
UserID *string `json:"-"`
Expand Down

0 comments on commit 3358f4a

Please sign in to comment.