Redirect with plug in router

I am trying to use the Router file to redirect all users to the login page if they are not logged in and I figured I could do that by using plug. My setup is the following:

Router

defmodule Voicer.Router do
  use Voicer.Web, :router
  alias Voicer.Plugs.Authenticate

  pipeline :browser do
    plug :accepts, ["html"]
    plug :fetch_session
    plug :fetch_flash
    plug :protect_from_forgery
    plug :put_secure_browser_headers
    plug Authenticate
  end

  #This following doesn't really matter

end

Plug

defmodule Voicer.Plugs.Authenticate do
  import Phoenix.Controller, only: [redirect: 2]
  import Plug.Conn, only: [halt: 1]
  import Voicer.Helpers
  alias Voicer.Router.Helpers, as: Helper

  def init(default), do: default

  def call(conn, _opts) do
      if !is_logged_in?(conn) do
        conn
        |> redirect(Helper.session_path(conn, :new))
        |> halt()
      else
        conn
      end
  end

end

Voicer.Helpers

defmodule Voicer.Helpers do
  alias Plug.Conn, as: Connection

  def is_logged_in?(conn) do
    Connection.get_session(conn, :username) != nil
  end

  def get_username!(conn) do
    Connection.get_session(conn, :username)
  end

end

Now when I try to use this I get an error on this code:

def call(conn, _opts) do
    if !is_logged_in?(conn) do
      conn
      |> redirect(Helper.session_path(conn, :new)) # The error is spawned here.
      |> halt()
    else
      conn
    end
end

And the error log is:

Request: GET /
** (exit) an exception was raised:
    ** (FunctionClauseError) no function clause matching in Phoenix.Controller.redirect/2
        (phoenix) lib/phoenix/controller.ex:297: Phoenix.Controller.redirect(%Plug.Conn{adapter: {Plug.Adapters.Cowboy.Conn, :...}, assigns: %{}, before_send: [#Function<0.122225743/1 in Plug.CSRFProtection.call/2>, #Function<3.6924754/1 in Phoenix.Controller.fetch_flash/2>, #Function<0.131550558/1 in Plug.Session.before_send/2>, #Function<1.20352387/1 in Plug.Logger.call/2>, #Function<0.127200512/1 in Phoenix.LiveReloader.before_send_inject_reloader/2>], body_params: %{}, cookies: %{"_dealmailer_key" => "SFMyNTY.g3QAAAACbQAAAAtfY3NyZl90b2tlbm0AAAAYTzM5cSt2ZG1zc3ZINlJnbVZ5K2ZOZz09bQAAAA9jdXJyZW50X3VzZXJfaWRhAQ.PXc8ffCcICxYQ9CmPqZ0Ryn5wvJsCXPAQOCmB3TbVf8", "_voicer_key" => "SFMyNTY.g3QAAAABbQAAAAtfY3NyZl90b2tlbm0AAAAYT2cxUERpKy90WWRubTVMOUFSZjgydz09.cfq3PgA6YSm5oOe5rdQD8DfarsJce6jiwvASNlkHIlU"}, halted: false, host: "localhost", method: "GET", owner: #PID<0.403.0>, params: %{}, path_info: [], path_params: %{}, peer: {{127, 0, 0, 1}, 54455}, port: 4000, private: %{Voicer.Router => {[], %{}}, :phoenix_endpoint => Voicer.Endpoint, :phoenix_flash => %{}, :phoenix_format => "html", :phoenix_pipelines => [:browser], :phoenix_route => #Function<4.2075796/1 in Voicer.Router.match_route/4>, :phoenix_router => Voicer.Router, :plug_session => %{"_csrf_token" => "Og1PDi+/tYdnm5L9ARf82w=="}, :plug_session_fetch => :done}, query_params: %{}, query_string: "", remote_ip: {127, 0, 0, 1}, req_cookies: %{"_dealmailer_key" => "SFMyNTY.g3QAAAACbQAAAAtfY3NyZl90b2tlbm0AAAAYTzM5cSt2ZG1zc3ZINlJnbVZ5K2ZOZz09bQAAAA9jdXJyZW50X3VzZXJfaWRhAQ.PXc8ffCcICxYQ9CmPqZ0Ryn5wvJsCXPAQOCmB3TbVf8", "_voicer_key" => "SFMyNTY.g3QAAAABbQAAAAtfY3NyZl90b2tlbm0AAAAYT2cxUERpKy90WWRubTVMOUFSZjgydz09.cfq3PgA6YSm5oOe5rdQD8DfarsJce6jiwvASNlkHIlU"}, req_headers: [{"host", "localhost:4000"}, {"connection", "keep-alive"}, {"cache-control", "max-age=0"}, {"upgrade-insecure-requests", "1"}, {"user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"}, {"accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3"}, {"sec-fetch-site", "same-origin"}, {"sec-fetch-mode", "navigate"}, {"referer", "http://localhost:4000/"}, {"accept-encoding", "gzip, deflate, br"}, {"accept-language", "en-US,en;q=0.9"}, {"cookie", "_dealmailer_key=SFMyNTY.g3QAAAACbQAAAAtfY3NyZl90b2tlbm0AAAAYTzM5cSt2ZG1zc3ZINlJnbVZ5K2ZOZz09bQAAAA9jdXJyZW50X3VzZXJfaWRhAQ.PXc8ffCcICxYQ9CmPqZ0Ryn5wvJsCXPAQOCmB3TbVf8; _voicer_key=SFMyNTY.g3QAAAABbQAAAAtfY3NyZl90b2tlbm0AAAAYT2cxUERpKy90WWRubTVMOUFSZjgydz09.cfq3PgA6YSm5oOe5rdQD8DfarsJce6jiwvASNlkHIlU"}], request_path: "/", resp_body: nil, resp_cookies: %{}, resp_headers: [{"cache-control", "max-age=0, private, must-revalidate"}, {"x-request-id", "mvh6lj6g8s4k3heq39j1gg44jco8811f"}, {"x-frame-options", "SAMEORIGIN"}, {"x-xss-protection", "1; mode=block"}, {"x-content-type-options", "nosniff"}], scheme: :http, script_name: [], secret_key_base: "FJNo7pAyKFnCc2uLuA7FlBTJQLPgrcclsmew7PY25p1lBOKgzCE6VTAFOo9nrL2f", state: :unset, status: nil}, "/login")
        (voicer) lib/voicer/plugs/authenticate.ex:12: Voicer.Plugs.Authenticate.call/2
        (voicer) Voicer.Router.browser/2
        (voicer) web/router.ex:1: Voicer.Router.match_route/4
        (voicer) web/router.ex:1: Voicer.Router.do_call/2
        (voicer) lib/voicer/endpoint.ex:1: Voicer.Endpoint.phoenix_pipeline/1
        (voicer) lib/plug/debugger.ex:123: Voicer.Endpoint."call (overridable 3)"/2
        (voicer) lib/voicer/endpoint.ex:1: Voicer.Endpoint.call/2
        (plug) lib/plug/adapters/cowboy/handler.ex:15: Plug.Adapters.Cowboy.Handler.upgrade/4
        (cowboy) c:/Users/Zastrix/Documents/Phoenix/voicer/deps/cowboy/src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4

I am using Phoenix 1.2.5 for now.

Am I doing something wrong with my plug creation?

Redirect takes a keyword as second parameters…

https://hexdocs.pm/phoenix/Phoenix.Controller.html#redirect/2

Maybe try this

|> redirect(to: Helper.session_path(conn, :new))
1 Like

pure plug way:
URL Redirect - Questions / Help - Elixir Programming Language Forum