Created
September 21, 2016 02:36
-
-
Save shavit/af8104e162b838861595ea622b3c22b1 to your computer and use it in GitHub Desktop.
Update user without password in Elixir Phoenix with Guardian plug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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