Created
November 25, 2015 16:18
-
-
Save mattSpell/6ac809e2ab25578e774f 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 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