Skip to content

Instantly share code, notes, and snippets.

@shavit
Created September 21, 2016 02:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shavit/af8104e162b838861595ea622b3c22b1 to your computer and use it in GitHub Desktop.
Save shavit/af8104e162b838861595ea622b3c22b1 to your computer and use it in GitHub Desktop.
Update user without password in Elixir Phoenix with Guardian plug
#
# web/models/user.ex
#
defmodule Plans.User do
use Plans.Web, :model
@primary_key {:id, :binary_id, autogenerate: true}
schema "users" do
field :username, :string
field :email, :string
field :password, :string, virtual: true
field :encrypted_password, :string
field :created, Ecto.DateTime, default: Ecto.DateTime.utc
timestamps()
end
@required_fields ~w(email password)
@optional_fields ~w(username email encrypted_password)
@doc """
Builds a changeset based on the `struct` and `params`.
"""
def changeset(struct, params \\ %{}) do
struct
|> cast(params, @required_fields, @optional_fields)
|> hash_password
|> unique_constraint(:email)
end
@doc """
Update user profile without providing a password
"""
def changeset_profile(struct, params \\ %{}) do
struct
|> cast(params, [:name], [])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment