Created
October 30, 2014 23:53
-
-
Save Gazler/7ccfac8862e3b4ef952b to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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 |
This file contains hidden or 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 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 |
This file contains hidden or 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 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