Created
December 21, 2016 21:14
-
-
Save javierg/e6179bc38baabd0a021097efcae53551 to your computer and use it in GitHub Desktop.
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
defmodule User do | |
use PhoenixUeberauthComeonin.Web, :model | |
import Comeonin.Bcrypt | |
alias Ueberauth.Auth | |
alias PhoenixUeberauthComeonin.Repo | |
schema "users" do | |
field :name, :string | |
field :email, :string | |
field :password, :string | |
timestamps() | |
end | |
def authenticate(%Auth{provider: :identity} = auth) do | |
Repo.get_by(__MODULE__, email: auth.uid) | |
|> authorize(auth) | |
end | |
@doc """ | |
Builds a changeset based on the `struct` and `params`. | |
""" | |
def changeset(struct, params \\ %{}) do | |
struct | |
|> cast(params, [:name, :email]) | |
|> validate_required([:name, :email]) | |
end | |
defp authorize(nil,_auth), do: {:error, "Invalid username or password"} | |
defp authorize(user, auth) do | |
checkpw(auth.credentials.other.password, user.password) | |
|> resolve_authorization(user) | |
end | |
defp resolve_authorization(false, _user), do: {:error, "Invalid username or password"} | |
defp resolve_authorization(true, user), do: {:ok, user} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment