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
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
Also Liked
ZoomQuiet
Popular in Questions
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
For example for a current url like
http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2,
I would like to get:
...
New
Hello everybody,
usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
Hi,
I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
Hi guys, i’m new in the Elixir world, and i have to say, that i love it!
i’m having some problem to understand anonymous functions with ...
New
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
I have a User schema with a :from_id field set to type :string:
defmodule TweetBot.Repo.Migrations.CreateUsers do
use Ecto.Migration
...
New
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
Lets say i have map like this fetching from my database
%{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => "XX...
New
Other popular topics
Hello!
tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability.
After spen...
New
Hello guys,
I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine)
This is a plugin that adds support for Elixir to JetBrains IntelliJ...
New
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar.
I p...
New
I am trying to run a deploy with docker and I successfully runned with this command:
docker build -t romenigld/blog-prod .
but when I t...
New
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New








