riverrun / phauxth

Not actively maintained - Authentication library for Phoenix, and other Plug-based, web applications
409 stars 21 forks source link

Reviewers needed for 2.0 #80

Closed BryanJBryce closed 5 years ago

BryanJBryce commented 6 years ago

Version 2 has been merged into master and is now alpha on hex. Please, review the changes that have been made and create issues where needed.

riverrun commented 6 years ago

Please read the upgrade guide and let me know if you have any comments / questions about that.

It is important to note that some of the changes in version 2.0 are negotiable. If any of you feel that changes should be made, or that we should revert to previous behavior, then we can discuss that either here or on the Gitter channel.

goravbhootra commented 6 years ago

@riverrun I am using phauxth to run authentication against two separate user tables (and separate contexts) in my app. My current setup has:

router.ex:

  ...
  pipeline :browser do
    ...
    plug(Phauxth.Authenticate, max_age: 64800) # 60 x 60 x 18 - 18 hours
    plug(Phauxth.Remember)
  end

  pipeline :lms_browser do
    ...
    plug(MyApp.LMS.Authenticate, max_age: 64800, user_context: MyApp.LMS.Accounts) # 60 x 60 x 18 - 18 hours
    plug(MyApp.LMS.Remember, user_context: MyApp.LMS.Accounts)
  end
  ...

myapp/lms/authenticate.ex:

  defmodule MyApp.LMS.Authenticate do
    use Phauxth.Authenticate.Base

    alias MyApp.LMS.Accounts.User, as: LmsUser

    @doc """
    Set the `lms_user` variable.
    """
    def set_user(nil, conn), do: assign(conn, :lms_user, nil)
    def set_user(%LmsUser{} = lms_user, conn), do: assign(conn, :lms_user, lms_user)
  end

my_app_web/controllers/session_controller.ex:

  defmodule MyAppWeb.SessionController do
    use MyAppWeb, :controller

    alias MyApp.Accounts
    alias MyApp.Accounts.User
    alias Phauxth.Confirm.Login

    ...

    def create(conn, %{"session" => params}) do
      case Login.verify(params, Accounts) do
        {:ok, user} ->
          path = home_redirect(conn, user)
          Accounts.process_successful_login(conn, user, params, path)
          |> login_success(path)
        {:error, message} ->
          render_error(conn, message, Routes.session_path(conn, :new))
      end
    end
  end

my_app_web/controllers/lms/session_controller.ex:

  defmodule MyAppWeb.LMS.SessionController do
    use MyAppWeb, :controller

    alias MyApp.LMS.Accounts, as: LmsAccounts
    alias MyApp.LMS.Accounts.User, as: LmsUser
    alias Phauxth.Confirm.Login
    ...

    def create(conn, %{"lms_session" => params}) do
      case Login.verify(params, LmsAccounts) do
        {:ok, %LmsUser{} = lms_user} ->
          path = home_redirect(conn, lms_user)
          LmsAccounts.process_successful_login(conn, lms_user, params, path)
          |> login_success(path)
        {:error, message} ->
          render_error(conn, message, Routes.lms_session_path(conn, :new))
      end
    end
  end

With user_context moving to config in the new version, how would I use it for the above setup?

riverrun commented 6 years ago

@goravbhootra I'll look at your setup in more detail later, and I will get back to you soon.

riverrun commented 6 years ago

@goravbhootra I will bring back the keyword argument for user_context - I will let you know when that is done.

goravbhootra commented 6 years ago

@riverrun thanks a lot

riverrun commented 6 years ago

@goravbhootra version 2.0.0-rc.1 now allows you to use keyword arguments to define the user_context.

With the Plugs, you can use them the way that you have been using them in v1.2.

With the verify functions, you need use them in the following way:

To use the value set in the config - Login.verify(params)

To use a custom value - Login.verify(params, user_context: LmsAccounts)

Thanks for raising this issue - this kind of feedback really helps me know what changes can be made.

goravbhootra commented 6 years ago

@riverrun I will upgrade asap and revert with feedback.

riverrun commented 5 years ago

I think we can close this issue now - thanks to everyone for your valuable feedback.