Code Monkey home page Code Monkey logo

Comments (10)

danschultzer avatar danschultzer commented on May 8, 2024 1

I would just call the plug actions directly in the individual actions rather than using the controller callbacks:

# In session create action
PowPersistentSession.Plug.create(conn, user)

# In session delete action
PowPersistentSession.Plug.delete(conn)

In Pow most important methods can be found in the plug modules, controller callbacks are usually very light.

from pow.

danschultzer avatar danschultzer commented on May 8, 2024 1

But when the persistent session is in place with a longer ttl than the regular sessions, it will allow sessions that have expired to be renewed.

In that case, the only changes I need to make are to plug in the cookie, and to call PowPersistentSession.create/3 in my custom controller actions, and otherwise the use of pow remains the same. Is that right?

Yeah, you are correct. You need to call PowPersistentSession.Plug.create/2 when you create the session with Pow.Plug.create/2.

from pow.

vaer-k avatar vaer-k commented on May 8, 2024 1

Thanks Dan. I think I have things working now. Here is how I finally ended up in my controller actions:

  def signup(conn, %{"user" => user_params}) do
    conn
    |> Pow.Plug.create_user(user_params)
    |> case do
      {:ok, user, conn} ->
        conn
        |> PowPersistentSession.Plug.create(user)
        |> redirect(to: Routes.page_path(conn, :show))

      {:error, changeset, conn} ->
        render(conn, "index.html", changeset: changeset)
    end
  end

  def login(conn, %{"user" => user_params}) do
    conn
    |> Pow.Plug.authenticate_user(user_params)
    |> case do
      {:ok, conn} ->
        conn
        |> PowPersistentSession.Plug.create(conn.assigns.current_user)
        |> redirect(to: Routes.page_path(conn, :show))

      {:error, conn} ->
        changeset = Pow.Plug.change_user(conn, conn.params["user"])

        conn
        |> put_flash(:info, "Invalid email or password")
        |> render("index.html", changeset: changeset)
    end
  end

from pow.

augnustin avatar augnustin commented on May 8, 2024 1

Thanks @vaer-k for your code šŸ‘

This is definitely related to #516 which I just opened this morning.

from pow.

danschultzer avatar danschultzer commented on May 8, 2024

Yeah, and this is for a good reason. Controller callbacks is mostly a compromise to make Pow plug nā€™ play, but ideally developers take control of everything they change rather than having implicit callbacks. I have written about it in the custom controller guide, and Iā€™m also thinking of redoing how the callbacks work in #66.

I would recommend writing the controllers as explicit as possible. However, you can build a controller that uses the Pow.Phoenix.Controller macro, and will use the controller callbacks from the config. Take a look at the registration controller to get an idea of how it works:
https://github.com/danschultzer/pow/blob/master/lib/pow/phoenix/controllers/registration_controller.ex

Controllers are very thin in Pow with a process and response separation that makes it possible to hook in callbacks.

from pow.

 avatar commented on May 8, 2024

@danschultzer so how would one go about making a custom controller work with persisted sessions plug?

from pow.

 avatar commented on May 8, 2024

@danschultzer thank you! It seems to have worked.

from pow.

vaer-k avatar vaer-k commented on May 8, 2024

I would appreciate fuller documentation regarding how to integrate PowPersistentSession with custom controllers. For instance, how do you properly RequireAuthenticated in the router? As a test, I set persistent_session_ttl to just 30 seconds, but after the ttl I see that my user is still able to access protected routes, when I expected that their session would have been purged from the store and the cookie invalidated by that time.

from pow.

danschultzer avatar danschultzer commented on May 8, 2024

You have to use plug PowPersistentSession.Plug.Cookie as described in https://hexdocs.pm/pow/1.0.19/pow_persistent_session.html#content

It's not a replacement for the session. It's there to ensure that the session can be recreated once it has expired. PowPersistentSession sets a cookie that can be used once to create a new session for the user. In your case the normal session works as expected. Let me know if this could be better described in the docs!

from pow.

vaer-k avatar vaer-k commented on May 8, 2024

Oh, I see! So setting a ttl for the persistent session lower than the regular session will not result in sessions being invalidated after the persistent ttl. In that case the regular session TTLs will dictate when the session expires. But when the persistent session is in place with a longer ttl than the regular sessions, it will allow sessions that have expired to be renewed.

In that case, the only changes I need to make are to plug in the cookie, and to call PowPersistentSession.create/3 in my custom controller actions, and otherwise the use of pow remains the same. Is that right?

from pow.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    šŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. šŸ“ŠšŸ“ˆšŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ā¤ļø Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.