Skip to content

Instantly share code, notes, and snippets.

@mattSpell
Created November 25, 2015 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mattSpell/6ac809e2ab25578e774f to your computer and use it in GitHub Desktop.
Save mattSpell/6ac809e2ab25578e774f to your computer and use it in GitHub Desktop.
defmodule BusTracker.Session do
alias BusTracker.User
import Ecto.Repo
use Bustracker.Web, :model`?
def login(params, repo) do
user = repo.get_by(User, email: String.downcase(params["email"]))
case authenticate(user, params["password"]) do
true -> {:ok, user}
_ -> :error
end
end
defp authenticate(user, password) do
case user do
nil -> false
_ -> Comeonin.Bcrypt.checkpw(password, user.crypted_password)
end
end
def current_user(conn) do
id = Plug.Conn.get_session(conn, :current_user)
if id, do: Repo.get(User, id)
end
def logged_in?(conn), do: !!current_user(conn)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment