Skip to content

Instantly share code, notes, and snippets.

@Gazler
Created October 30, 2014 23:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gazler/7ccfac8862e3b4ef952b to your computer and use it in GitHub Desktop.
Save Gazler/7ccfac8862e3b4ef952b to your computer and use it in GitHub Desktop.
defmodule Authex.Mixfile do
use Mix.Project
def project do
[app: :authex,
version: "0.0.1",
elixir: "~> 1.0",
deps: deps]
end
# Configuration for the OTP application
#
# Type `mix help compile.app` for more information
def application do
[applications: [:logger, :bcrypt]]
end
# Dependencies can be Hex packages:
#
# {:mydep, "~> 0.3.0"}
#
# Or git/path repositories:
#
# {:mydep, git: "https://github.com/elixir-lang/mydep.git", tag: "0.1.0"}
#
# Type `mix help deps` for more examples and options
defp deps do
[
{:ecto, "~> 0.2.5"},
{:postgrex, "~> 0.6.0"},
{:bcrypt, github: "smarkets/erlang-bcrypt", tag: "0.4.1"}
]
end
end
defmodule AuthexTest.Repo do
use Ecto.Repo, adapter: Ecto.Adapters.Postgres
def conf do
# The scheme can be anything, "ecto" is just an example
parse_url "postgresql://docker:docker@localhost:5432/authex_development"
end
end
defmodule Authex.User do
use Ecto.Model
schema "users" do
field :email
field :encrypted_password
end
def authenticate!(email, password) do
case find_by_email(email) do
user = %Authex.User{} -> check_password(user, password)
_ -> nil
end
end
defp check_password(nil, _password), do: nil
defp check_password(user = %Authex.User{encrypted_password: encrypted_password}, password_verify) do
:bcrypt.start
<< salt :: binary-size(29), cipher :: binary-size(31)>> = encrypted_password
verifier = :bcrypt.hashpw(password_verify, String.to_char_list(salt))
IO.inspect verifier
IO.inspect encrypted_password
salt
#IO.inspect password
end
defp find_by_email(email) do
query = from u in Authex.User,
where: downcase(u.email) == downcase("#{email}")
hd AuthexTest.Repo.all(query)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment