From 19037c723f98ad9210eed23d5e8290171682e00f Mon Sep 17 00:00:00 2001 From: Dan Barber Date: Fri, 2 Feb 2018 14:37:45 -0500 Subject: [PATCH] More idiomatic password hashing --- lib/chess/auth/user.ex | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/chess/auth/user.ex b/lib/chess/auth/user.ex index 9f31d08..546bd82 100644 --- a/lib/chess/auth/user.ex +++ b/lib/chess/auth/user.ex @@ -23,11 +23,13 @@ defmodule Chess.Auth.User do |> hash_password() end - defp hash_password( - %Ecto.Changeset{valid?: true, changes: %{password: password}} = changeset - ) do - change(changeset, Argon2.add_hash(password)) + defp hash_password(changeset) do + password = get_change(changeset, :password) + if password do + changeset + |> change(Argon2.add_hash(password)) + else + changeset + end end - - defp hash_password(changeset), do: changeset end