I'm having an issue with my web app with a compile error below:
== Compilation error on file web/controllers/auth.ex ==
** (CompileError) web/controllers/auth.ex:49: 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:117: anonymous fn/4 in Kernel.ParallelCompiler.spawn_compilers/1
I know most issues have been to do with missing routes on the router.ex
, but it doesn't look like there is any issues here:
scope "/", Rumbl do
pipe_through :browser # Use the default browser stack
get "/", PageController, :index
get "/users/:id", UserController, :show
resources "/users", UserController, only: [:index, :show, :new, :create]
resources "/sessions", SessionController, only: [:new, :create, :delete]
resources "/videos", VideoController
end
scope "/manage", Rumbl do
pipe_through [:browser, :authenticate_user]
resources "/videos", VideoController
end
My web.ex looks like this:
def router do
quote do
use Phoenix.Router
import Rumbl.Auth, only: [authenticate_user: 2] # New import
end
end
def controller do
quote do
use Phoenix.Controller
alias Rumbl.Repo
import Ecto
import Ecto.Query, only: [from: 1, from: 2]
import Rumbl.Router.Helpers
import Rumbl.Gettext
import Rumbl.Auth, only: [authenticate_user: 2] # New import
end
end
auth.ex looks like this
defmodule Rumbl.Auth do
import Plug.Conn
import Comeonin.Bcrypt, only: [checkpw: 2, dummy_checkpw: 0]
import Phoenix.Controller
alias Rumbl.Router.Helpers
def init(opts) do
Keyword.fetch!(opts, :repo)
end
def call(conn, repo) do
user_id = get_session(conn, :user_id)
user = user_id && repo.get(Rumbl.User, user_id)
assign(conn, :current_user, user)
end
def login_by_username_and_pass(conn, username, given_pass, opts) do
repo = Keyword.fetch!(opts, :repo)
user = repo.get_by(Rumbl.User, username: username)
cond do
user && checkpw(given_pass, user.password_hash) ->
{:ok, login(conn, user)}
user ->
{:error, :unauthorized, conn}
true ->
dummy_checkpw()
{:error, :not_found, conn}
end
end
def login(conn, user) do
conn
|> assign(:current_user, user)
|> put_session(:user_id, user.id)
|> configure_session(renew: true)
end
def logout(conn) do
configure_session(conn, drop: true)
end
def authenticate_user(conn, _opts) do
if conn.assigns.current_user do
conn
else
conn
|> put_flash(:error, "You must be logged in to access that page")
|> redirect(to: page_path(conn, :index))
|> halt()
end
end
end
In phoenix 1.4 you need to call the function with module name
Routes.page_path(@conn, :create)
Can you post auth.ex file? It seems there is an issue in it on line 49.
Please change this line:
|> redirect(to: page_path(conn, :index))
to
|> redirect(to: Helpers.page_path(conn, :index))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With