Phauxth: undefined session_path/2 , cant figure this out

I decided to go with Phauxth as authentication system.

I followed the instructions on their github page: https://github.com/riverrun/phauxth/wiki/Getting-started

After running the generation command and mix ecto.setup things started to fall apart.

Now i get the errors:

 Compilation error in file lib/figurism_web/controllers/authorize.ex ==
** (CompileError) lib/figurism_web/controllers/authorize.ex:35: undefined function page_path/2
    (stdlib) lists.erl:1338: :lists.foreach/2
    (stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
    (elixir) lib/kernel/parallel_compiler.ex:198: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/6

My route.ex looks like this:

defmodule FigurismWeb.Router do
  use FigurismWeb, :router

  pipeline :browser do
    plug(:accepts, ["html"])
    plug(:fetch_session)
    plug(:fetch_flash)
    plug(:protect_from_forgery)
    plug(:put_secure_browser_headers)
    plug(Phauxth.Authenticate)
    plug(Phauxth.Remember)
    plug(SetLocale, gettext: FigurismWeb.Gettext, default_locale: "sv")
  end

  scope "/:locale", FigurismWeb do
    pipe_through(:browser)

    get("/", PageController, :index)
    resources("/users", UserController)
    resources("/sessions", SessionController, only: [:new, :create, :delete])
    get("/confirm", ConfirmController, :index)
    resources("/password_resets", PasswordResetController, only: [:new, :create])
    get("/password_resets/edit", PasswordResetController, :edit)
    put("/password_resets/update", PasswordResetController, :update)
  end
end

Phoenix just dont seem to be able to generate path for the generated files :O?

What’s inside lib/figurism_web/controllers/authorize.ex?

defmodule FigurismWeb.Authorize do
  import Plug.Conn
  import Phoenix.Controller
  import FigurismWeb.Router.Helpers

  # This function can be used to customize the `action` function in
  # the controller so that only authenticated users can access each route.
  # See the [Authorization wiki page](https://github.com/riverrun/phauxth/wiki/Authorization)
  # for more information and examples.
  def auth_action(%Plug.Conn{assigns: %{current_user: nil}} = conn, _) do
    need_login(conn)
  end

  def auth_action(
        %Plug.Conn{assigns: %{current_user: current_user}, params: params} = conn,
        module
      ) do
    apply(module, action_name(conn), [conn, params, current_user])
  end

  # Plug to only allow authenticated users to access the resource.
  # See the user controller for an example.
  def user_check(%Plug.Conn{assigns: %{current_user: nil}} = conn, _opts) do
    need_login(conn)
  end

  def user_check(conn, _opts), do: conn

  # Plug to only allow unauthenticated users to access the resource.
  # See the session controller for an example.
  def guest_check(%Plug.Conn{assigns: %{current_user: nil}} = conn, _opts), do: conn

  def guest_check(%Plug.Conn{assigns: %{current_user: _current_user}} = conn, _opts) do
    error(conn, "You need to log out to view this page", page_path(conn, :index))
  end

  # Plug to only allow authenticated users with the correct id to access the resource.
  # See the user controller for an example.
  def id_check(%Plug.Conn{assigns: %{current_user: nil}} = conn, _opts) do
    need_login(conn)
  end

  def id_check(
        %Plug.Conn{params: %{"id" => id}, assigns: %{current_user: current_user}} = conn,
        _opts
      ) do
    (id == to_string(current_user.id) and conn) ||
      error(conn, "You are not authorized to view this page", user_path(conn, :index))
  end

  def success(conn, message, path) do
    conn
    |> put_flash(:info, message)
    |> redirect(to: path)
  end

  def error(conn, message, path) do
    conn
    |> put_flash(:error, message)
    |> redirect(to: path)
    |> halt
  end

  def login_success(conn, path) do
    path = get_session(conn, :request_path) || path

    delete_session(conn, :request_path)
    |> success("You have been logged in", get_session(conn, :request_path) || path)
  end

  def need_login(conn) do
    conn
    |> put_session(:request_path, current_path(conn))
    |> error("You need to log in to view this page", session_path(conn, :new))
  end
end

This is the content of authorize.ex

I also get errors about undefined paths in the generated views.

I think you need to pass locale into page_path/3. Try running

iex> FigurismWeb.Router.Helpers. # and press tab

to see what functions are available in router helpers. page_path/2 might not be among them (judging by the stack trace).

What do You get when running?

$ mix phx.routes

It is maybe because You use a :locale scope. That is typically what I was doing with Rails :slight_smile:

Did You try without?

Yeah im an old rails dev trying to get some fresh air.

I couldnt see mix phx.routes since …it refused to compile.

I deleted the application and started over without set_locale dependency and it now works great.

Main problem is to much ruby thinking and alot of junior elixir mistakes :smiley:

Thanks for the fast replies, need to figure out another way to have locales working more smoothly.

I have taken the same path, made the same mistakes…

Also when coming from Rails, forget about Active Record, Object and Design pattern. That might be sounds strange, but the sooner the better :-). Even if very similar in syntax, the mindset is different.

Think Functional, and You will understand why You can get concurrency, distribution and fiability. Something I could not achieve with RoR.

Play with processes and see why it’s fun to use Elixir :slight_smile:

2 Likes