Hris

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.

Showing Posts 1 to 10

ravi11o

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

Hris OP

Guardian.ex

defmodule WebPage.Guardian do
    use Guardian, otp_app: :watch_tok_phoenix
    alias WebPage.Users

    def subject_for_token(user, _claims) do
      {:ok, to_string(user.id)}
    end
    def resource_from_claims(claims) do
      user = claims["sub"]
      |> Users.get_user!
      {:ok, user}
      # If something goes wrong here returns {:error, reason}
    end
  end

Pipeline.ex

defmodule WebPage.Pipeline do
    use Guardian.Plug.Pipeline,
      otp_app: :web_page,
      error_handler: WebPage.ErrorHandler,
      module: WebPage.Guardian
    # If there is a session token, validate it
    plug Guardian.Plug.VerifySession, claims: %{"typ" => "access"}
    # If there is an authorization header, validate it
    plug Guardian.Plug.VerifyHeader, claims: %{"typ" => "access"}
    # Load the user if either of the verifications worked
    plug Guardian.Plug.LoadResource, allow_blank: true
  end

error_handler.ex

defmodule WebPage.ErrorHandler do
    import Plug.Conn
    def auth_error(conn, {type, _reason}, _opts) do
      body = to_string(type)
      conn
      |> put_resp_content_type("text/plain")
      |> send_resp(401, body)
    end
  end
ravi11o

ravi11o

have you tried piping halt() in auth_error function in WebPage.ErrorHandler.
error_handler.ex

defmodule WebPage.ErrorHandler do
    import Plug.Conn
    def auth_error(conn, {type, _reason}, _opts) do
      body = to_string(type)
      conn
      |> put_resp_content_type("text/plain")
      |> send_resp(401, body)
      |> halt()
    end
  end

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,

|> send_resp(401, body)

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

Hris OP

It is not helping.

ravi11o

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

defp login_reply({:ok, user}, conn) do
    conn
    |> put_flash(:success, "Welcome back!")
    |> WebPage.Guardian.Plug.sign_in(user)
    |> redirect(to: "/")
  end

OR

Can I check the source code, if you don’t have any problem ? you can message me.

7a6163

7a6163

Hello,

use Guardian, otp_app: :watch_tok_phoenix

replace with

use Guardian, otp_app: :web_page
Hris

Hris OP

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

bnymn

How did you solve it? What was the root cause of the problem? I am facing the same problem right now.

bnymn

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

billtong

This saves my life!

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
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
spammy
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
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
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
bottlenecked
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
rahultumpala
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 Top

JesseHerrick
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews