Hris
Using Guardian for register/login.
I am getting this error when I try to log in:
[info] Sent 401 in 819ms
[error] #PID<0.440.0> running WebPageWeb.Endpoint (cowboy_protocol) terminated
Server: localhost:4000 (http)
Request: POST /users/login_auth
** (exit) an exception was raised:
** (Plug.Conn.AlreadySentError) the response was already sent
(plug) lib/plug/conn.ex:668: Plug.Conn.put_resp_header/3
(phoenix) lib/phoenix/controller.ex:402: Phoenix.Controller.redirect/2
(web_page) lib/web_page_web/controllers/user_controller.ex:1: WebPageWeb.UserController.action/2
(web_page) lib/web_page_web/controllers/user_controller.ex:1: WebPageWeb.UserController.phoenix_controller_pipeline/2
(web_page) lib/web_page_web/endpoint.ex:1: WebPageWeb.Endpoint.instrument/4
(phoenix) lib/phoenix/router.ex:278: Phoenix.Router.__call__/1
(web_page) lib/web_page_web/endpoint.ex:1: WebPageWeb.Endpoint.plug_builder_call/2
(web_page) lib/plug/debugger.ex:122: WebPageWeb.Endpoint."call (overridable 3)"/2
(web_page) lib/web_page_web/endpoint.ex:1: WebPageWeb.Endpoint.call/2
(plug) lib/plug/adapters/cowboy/handler.ex:16: Plug.Adapters.Cowboy.Handler.upgrade/4
(cowboy) src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4
Controller:
def login(conn, _params) do
changeset = Users.change_user(%User{})
render(conn, "login.html", changeset: changeset)
end
def login_auth(conn, %{"user" => %{"email" => email, "password" => password}}) do
Auth.authenticate_user(email, password)
|> login_reply(conn)
end
defp login_reply({:error, error}, conn) do
conn
|> put_flash(:error, error)
|> redirect(to: "/")
end
defp login_reply({:ok, user}, conn) do
conn
|> put_flash(:success, "Welcome back!")
|> Guardian.Plug.sign_in(user)
|> redirect(to: "/")
end
Auth.ex
def authenticate_user(email, plain_text_password) do
query = from u in User, where: u.email == ^email
Repo.one(query)
|> check_password(plain_text_password)
end
defp check_password(nil, _), do: {:error, "Incorrect email or password"}
defp check_password(user, plain_text_password) do
case Bcrypt.checkpw(plain_text_password, user.password_hash) do
true -> {:ok, user}
false -> {:error, "Incorrect email or password"}
end
end
Router:
defmodule WebPageWeb.Router do
use WebPageWeb, :router
pipeline :browser do
plug :accepts, ["html"]
plug :fetch_session
plug :fetch_flash
plug :protect_from_forgery
plug :put_secure_browser_headers
end
pipeline :api do
plug :accepts, ["json"]
end
pipeline :auth do
plug WebPage.Pipeline
end
pipeline :ensure_auth do
plug Guardian.Plug.EnsureAuthenticated
end
scope "/", WebPageWeb do
pipe_through [:browser, :auth] # Use the default browser stack
get "/", PageController, :index
post("/login_auth", UserController, :login_auth)
get("/login", UserController, :login)
get("/logout", UserController, :logout)
resources "/users", UserController
end
scope "/admin", WebPageWeb do
pipe_through [:browser, :auth, :ensure_auth]
post("/login_auth", UserController, :login_auth)
get "/", PageController, :index
get("/login", UserController, :login)
get("/logout", UserController, :logout)
resources "/users", UserController
end
# Other scopes may use custom stacks.
# scope "/api", WebPageWeb do
# pipe_through :api
# end
end
So I investigated the problem and it comes out the verifications are allright.
Parameters which I give are correct but I couldn’t figure out where the the problem comes from.
After the login it saves the user into the connection regardless the given error.
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Kia ora,
We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
Hello,
I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New
Other Trending Topics
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
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
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
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
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All Posts (oldest first)
- Show All Posts (newest first)
ravi11o
You can check out this issue( Using halt on auth_error #401)
OR
you can provide more details like apps guardian module ie. WebpageWeb.Guardian and WebPage.Pipeline and Error handler for Guardian module.
Hris
Guardian.ex
Pipeline.ex
error_handler.ex
ravi11o
have you tried piping halt() in auth_error function in WebPage.ErrorHandler.
error_handler.ex
It looks like an auth error(401) in response before sending ** (Plug.Conn.AlreadySentError).
This lets it pass through auth_error function in error_handler.ex where response is sent i.e,
Since there is no halt, connection is passed forward and you are trying to respond through redirecting at “/”, which is throwing error because its already sent using send_resp() in auth_error function.
Hris
It is not helping.
ravi11o
You can troubleshoot from start like inspect Plug.Conn’s session since JWT is stored in session from Guardian.Plug.sign_in , check if token is available after login i.e, Authorisation token.
You can try WebPage.Guardian.Plug instead of Guardian.Plug inside login_reply function
OR
Can I check the source code, if you don’t have any problem ? you can message me.
7a6163
Hello,
replace with
Hris
Thanks for your response, but I actually managed to resolve the problem. It was actually a problem in the library. We had to wait for the latest updates.
bnymn
How did you solve it? What was the root cause of the problem? I am facing the same problem right now.
bnymn
I have just resolved my problem. In my case, my configuration in config.exs was wrong.
It was like this before:
I have changed it into:
billtong
This saves my life!