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
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
Anyone here using Honeybadger?
My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of
Bandit.HTTPError...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
Other Trending Topics
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
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
There are three potential reasons for members of this forum to have a look at https://vutuv.de
You are tired or annoyed of LinkedIn.
Yo...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











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