maz

maz

I am using Guardian Plug to authenticate via a REST API with JWT tokens. I would like to use the same site to login via the web and store a session with a cookie. I do this with the same pipeline as the one that issues jwt json. I’ve also tried implementing this with a completely different Guardian pipeline, but still seeing the same problem .. so I think I’m missing something.

login_controller.ex:

    case Authenticator.get_user_for_email_or_name_password(email, password) do
      nil ->
        conn
        |> put_flash(:error, "Invalid credentials!")
        |> render("new.html")
      user ->
        conn
        |> GuardianImpl.Plug.sign_in(user)
        |> assign(:current_user, user)
        |> put_user_token(user)
        |> render("home.html")

This following chunk momentarily reports a valid current_resource(), but the moment I try to access an authenticated route /admin/notification/send, the Guardian ErrorHandler fires and returns 401(unauthorized because unauthenticated). I know for sure sign_out() is not being called .. so what could be wrong?

      def user_logged_in?(conn) do
        GuardianImpl.Plug.current_resource(conn)
        !is_nil(GuardianImpl.Plug.current_resource(conn))
      end

router:

  # ---- Pipelines ----
  pipeline :browser do
    plug :accepts, ["html"]
    plug :fetch_session
    plug :fetch_flash
    plug :protect_from_forgery
    plug :put_secure_browser_headers
    plug :put_layout, {FaithfulWordApi.LayoutView, :app}
    plug(GuardianImpl.Pipeline)
  end

  pipeline :authentication_required do
    plug Guardian.Plug.EnsureAuthenticated
  end

  scope "/", FaithfulWordApi do
    pipe_through [:browser, :authentication_required]
    scope "/admin" do
      get "/upload", UploadController, :index
      get "/notification/send", PushMessageController, :index
    end
  end

Showing Posts 1 to 3

mythicalprogrammer

mythicalprogrammer


        conn
        |> GuardianImpl.Plug.sign_in(user)
        |> assign(:current_user, user)
        |> put_user_token(user)
        |> render("home.html")

Is this… correct? I have no idea how your Guardian Plug is implemented but IIRC guardian have it’s own function to create jwt tokens either :refresh or :claim. And iirc it would write the user cookie with the jwt created so I’m not entirely sure about this line |> assign(:current_user, user) |> put_user_token(user) or even what the logic behind it? Maybe I’m wrong…

maz

maz OP

Sure I’ll look into that, tinker with it. I might just write my own Plug for web login session auth specifically. I think something with my guardian config is wrong, but I’m not sure.

One thing I forgot to mention: this is an umbrella app and the guardian config is in an app that is NOT running Phoenix. I will try adding a guardian config to the app running Phoenix and use an entirely new namespace to refer to the Guardian Plug. If that doesn’t work, I will try the non-Guardian Plug idea.

maz

maz OP

OK so I managed to have Guardian save the session for web login. The issue was that I was using a Guardian pipeline in my router.ex configured in a separate umbrella app, which was configured to return JSON Web Token exclusively to ALSO manage sign_in() and sign_out() via a session. It just didn’t work.

In the app that contains phoenix, the solution was to configure in my config/config.exs another instance of Guardian which would manage a separate pipeline and plug under the app which shared the app namespace with phoenix. Both these Guardian pipelines seem to co-exist OK in the same router.ex file. Probably easier to show than write this out.. but it works.

— All posts loaded —

Where Next? Top

Trending in Chat/Questions Top

Other Trending Topics Top

GenericJam
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
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
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
georgeguimaraes
Just published claude-code-elixir, a plugin marketplace for Claude Code with Elixir support. These are the plugins I’ve been using for my...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews