pow-auth / pow_assent

Multi-provider authentication for your Pow enabled app
https://powauth.com
MIT License
323 stars 50 forks source link

Upon oauth login current_user not loaded with custom function. #225

Closed benonymus closed 2 years ago

benonymus commented 2 years ago

Hey,

I am having a problem with the current_user in conn in Pow.Phoenix.Routes.after_sign_in_path/1 .

In the current setup we use a custom function to load the user(with preloads) with the Pow.Ecto.Context.

  @impl true
  def get_by(clauses) do
    clauses
    |> pow_get_by()
    |> user_preloads()
  end

This works great in general.

We are using Pow.Phoenix.Routes.after_sign_in_path/1 and here the user from Pow.Plug.current_user(conn) when it is a regular email + pwd the user is preloaded correctly.

But when it is an oauth(google) login the user does not have the preloads.

It seems like based on logging that when it is an oauth login(maybe same for registration) the function is not used, whilst when email+ pwd it is.

Afterwards the user is loaded in fully, but for get_redirect_path/1 we need the user with the preloads.

Any idea as to why this inconsistency happens?

Thank you

danschultzer commented 2 years ago

This is because PowAssent looks up the user in a different table (using PowAssent.Operations.get_user_by_provider_uid/3) when loading the user:

https://github.com/pow-auth/pow_assent/blob/d93f9128faaf08d78644d692c153afdf0cf8293a/lib/pow_assent/plug.ex#L251-L267

PowAssent has :user_identities_context config which works the same way as the :users_context for Pow:

https://github.com/pow-auth/pow_assent/blob/d93f9128faaf08d78644d692c153afdf0cf8293a/lib/pow_assent/ecto/user_identities/context.ex#L2-L36

This way you can add get_user_by_provider_uid/3 to your context module and make add the preloads.

An alternative could also be to use a custom repo module for all of Pow, though that'll be less explicit.

benonymus commented 2 years ago

Thank you!