Skip to content

Instantly share code, notes, and snippets.

@rorymckinley
Created November 4, 2015 20:15
Show Gist options
  • Save rorymckinley/3e096adfefde264c18ae to your computer and use it in GitHub Desktop.
Save rorymckinley/3e096adfefde264c18ae to your computer and use it in GitHub Desktop.
Registrator module with some debugging
defmodule PhoenixTokenAuth.Registrator do
alias Ecto.Changeset
alias PhoenixTokenAuth.Util
alias PhoenixTokenAuth.UserHelper
@doc """
Returns a changeset setting email and hashed_password on a new user.
Validates that email and password are present and that email is unique.
"""
def changeset(params = %{"username" => username}) when username != "" and username != nil do
IO.inspect(params)
UserHelper.model.changeset(struct(UserHelper.model), params)
|> Changeset.cast(params, ~w(username))
|> Changeset.validate_change(:username, &Util.presence_validator/2)
|> Changeset.unique_constraint(:username)
|> Changeset.put_change(:hashed_confirmation_token, nil)
|> Changeset.put_change(:confirmed_at, Ecto.DateTime.utc)
|> changeset_helper
end
def changeset(params) do
UserHelper.model.changeset(struct(UserHelper.model), params)
|> Changeset.cast(params, ~w(email))
|> Changeset.validate_change(:email, &Util.presence_validator/2)
|> Changeset.unique_constraint(:email)
|> changeset_helper
end
defp changeset_helper(changeset) do
changeset
|> UserHelper.validator
|> set_hashed_password
end
def set_hashed_password(changeset = %{errors: [_]}), do: changeset
def set_hashed_password(changeset = %{params: %{"password" => password}}) when password != "" and password != nil do
hashed_password = Util.crypto_provider.hashpwsalt(password)
changeset
|> Changeset.put_change(:hashed_password, hashed_password)
end
def set_hashed_password(changeset) do
changeset
|> Changeset.add_error(:password, "can't be blank")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment