riverrun / openmaize

No longer maintained - was an Authentication library for Plug-based applications in Elixir
Other
206 stars 30 forks source link

How to use Phoenix.Token? #77

Closed puruzio closed 7 years ago

puruzio commented 7 years ago

I'm trying to get Openmaize to work with Phoenix.Token for user authentication. I tried to modify authenticate.ex in the library like this, but it doesn't seem to be called as I was expecting it.

Can you please tell me what is the right way to use Phoenix.Token with Openmaize?

 defp set_current_user(user, conn) do 
    token = Phoenix.Token.sign(conn, "user socket", user.id)
    IO.inspect(token)
    conn
    |> assign(:current_user, user)
    |> assign(:user_token, token)
riverrun commented 7 years ago

Could you let me know the output of IO.inspect(token)?

If it's nil or there's an error, let me know the output to IO.inspect(user) (just above the token = line).

If IO.inspect(token) returns a string, let me know the output of assign(:user_token, token) |> IO.inspect.

puruzio commented 7 years ago

Actually it doesn't seem to hit set_current_user line at all. Is there anything that needs to be wired up other than adding the plug Openmaize.Authenticate in the router.ex?

I did follow the steps indicated at https://github.com/riverrun/openmaize for installation and setup.

puruzio commented 7 years ago

Adding the following to router.ex did the trick. Thanks.

  pipeline :browser do
   ...
    plug Openmaize.Authenticate
    plug :put_user_token
  end

  defp put_user_token(conn, _) do
    if current_user = conn.assigns[:current_user] do
      token = Phoenix.Token.sign(conn, "user socket", current_user.id)
      assign(conn, :user_token, token)
    else
      conn
    end
  end
riverrun commented 7 years ago

Glad to hear you got it working.