ZastrixArundell
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?
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
So i have been using ash framework for a while and i love it. However currently the issue im having with ash framework is the error handl...
New
Other Trending Topics
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
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
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Latest Phoenix Threads
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex











Showing Posts 1 to 2- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
kokolegorille
Redirect takes a keyword as second parameters…
Maybe try this
ZoomQuiet
pure plug way:
URL Redirect - Questions / Help - Elixir Programming Language Forum