Skip to content

Commit

Permalink
fix: not able to change password
Browse files Browse the repository at this point in the history
  • Loading branch information
ruioliveira02 committed Feb 6, 2025
1 parent c2e32cb commit ca2bdd2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
19 changes: 16 additions & 3 deletions lib/safira/accounts/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,24 @@ defmodule Safira.Accounts.User do
|> cast_assoc(:attendee, with: &Attendee.changeset/2)
end

def changeset(user, attrs, opts \\ []) do
@doc """
A user changeset for changing the profile (name, handle, password and email).
"""
def profile_changeset(user, attrs, opts \\ []) do
user
|> cast(attrs, @required_fields ++ @optional_fields)
|> validate_email(opts)
|> cast(attrs, [:name, :handle, :email])
|> validate_handle()
|> if_changed_password_changeset(attrs, opts)
end

defp if_changed_password_changeset(changeset, attrs, opts) do
password = Map.get(attrs, "password")
password_exists? = password != nil && String.trim(password) != ""

case password_exists? do
true -> password_changeset(changeset, attrs, opts)
false -> changeset
end
end

defp validate_email(changeset, opts) do
Expand Down
2 changes: 1 addition & 1 deletion lib/safira/companies/company.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ defmodule Safira.Companies.Company do
|> cast(attrs, @required_fields ++ @optional_fields)
|> unique_constraint(:badge_id)
|> unique_constraint(:user_id)
|> cast_assoc(:user, with: &User.changeset/2)
|> cast_assoc(:user, with: &User.profile_changeset/2)
|> cast_assoc(:badge)
|> cast_assoc(:tier)
|> validate_required(@required_fields)
Expand Down

0 comments on commit ca2bdd2

Please sign in to comment.