maz

maz

Guardian session disappears

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

First Post!

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…

Last Post!

maz

maz

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.

Where Next?

Popular in Chat/Questions Top

jsnoble
I come from a javascript background and I just starting learning elixir. I went through the basic tutorials but I want something deeper. ...
New
makeitrein
More Ecto questions! More madness! The context: there’s a list of books that I want to filter with a dropdown… The dropdown: looks some...
New
Chawki
hi,i’m new to programming world i had learned front-end( javascript,react.js) and i wanna learn a back-end programming language i thought...
New
dopomecana
The title of this topic may sound silly at the first glance. But why I trying to ask you guys is how to go to the next level? I am curre...
New
ca1989
Hi all, is there any up to date resource out there (blog, talk, video, book…) about deploying elixir applications using releases? In pa...
New
armanm
I know zero downtime deployment can mean different things depending on your application and what your users can tolerate so expectations ...
New
Sujit
Hi Team From the title, I am entirely new to programming and i am interested in learning elixir but not sure where to start. Can someone...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44778 311
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

We're in Beta

About us Mission Statement