CuratorDatabaseAuthenticatable
Support password based sign-in by comparing the password to a hashed password. It also provides a generator for creating a sign-in page.
Installation
Add
curator_database_authenticatable
to your list of dependencies inmix.exs
:def deps do [{:curator_database_authenticatable, "~> 0.1.0"}] end
Run the install command
mix curator_database_authenticatable.install
Update
web/models/user.ex
defmodule Auth.User do use Auth.Web, :model use CuratorDatabaseAuthenticatable.Schema schema "users" do ... curator_database_authenticatable_schema ... end end
Update
web/router.ex
scope "/", Auth do pipe_through [:browser] get "/sessions", SessionController, :new post "/sessions", SessionController, :create delete "/sessions", SessionController, :delete ... end
Update
web/controllers/error_helper.ex
def unauthenticated(conn, %{reason: {:error, reason}}) do respond(conn, response_type(conn), 401, reason, session_path(conn, :new)) end def no_resource(conn, %{reason: reason}) do respond(conn, response_type(conn), 403, reason, session_path(conn, :new)) end
Update
test/supprt/session_helper.ex
def create_user(user, attrs) do user |> PhoenixCurator.User.changeset(attrs) |> PhoenixCurator.User.password_changeset(%{password: "TEST_PASSWORD", password_confirmation: "TEST_PASSWORD"}) ... |> PhoenixCurator.Repo.insert! end
Update
test/controllers/page_controller_test.exs
test "visiting a secret page w/o a user", %{conn: conn} do conn = get conn, "/secret" assert Phoenix.Controller.get_flash(conn, :danger) == "Please Log In" assert Phoenix.ConnTest.redirected_to(conn) == session_path(conn, :new) end