ZastrixArundell

ZastrixArundell

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?

Marked As Solved

kokolegorille

kokolegorille

Redirect takes a keyword as second parameters…

Maybe try this

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

Also Liked

Where Next?

Trending in Questions Top

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
tj0
I’ve been following the steps here for the upgrade from 1.6 to 1.7 and it has gone relatively smoothly all the way till the phoenix_view ...
New
cgraham
Hi! What is currently the best library/method for parsing text and tabular data out of PDF files in Elixir or Erlang?
New
stefanchrobot
Hi, I need a way to handle data migrations in my application. I found an article by @wojtekmach about manual migrations: Automatic and ma...
New
stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
webofbits
Squid Mesh is an open source workflow automation runtime for Elixir applications. It is aimed at Phoenix and OTP apps that want to defin...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
kip
In 2021 I started a new library called Tempo with the objective of modelling time as a set of intervals - not as instants. In 2022 I gave...
New

We're in Beta

About us Mission Statement