Skip to content

Instantly share code, notes, and snippets.

@shavit

shavit/user.ex

Created Sep 21, 2016
Embed
What would you like to do?
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