Hris
(Plug.Conn.AlreadySentError) the response was already sent
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.
Marked As Solved
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.
Also Liked
bnymn
I have just resolved my problem. In my case, my configuration in config.exs was wrong.
It was like this before:
config :guardian, Hello.Users.Guardian,
issuer: "hello",
secret_key: "4T5tuL+XbJ5yrmFKP/QfpE8gaqIRaTnmeXuIfuZGKkKKzc0dgGYhdVsWTrJWEVJ2"
I have changed it into:
config :hello, Hello.Users.Guardian,
issuer: "hello",
secret_key: "4T5tuL+XbJ5yrmFKP/QfpE8gaqIRaTnmeXuIfuZGKkKKzc0dgGYhdVsWTrJWEVJ2"
billtong
This saves my life!
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.
Popular in Questions
Other popular topics
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
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









