Force update user on login with Pow/Pow-Assent

Hello,

I recently implemented pow/pow-assent with OIDC to login and everything is working well but I struggle with one thing: how to update user data (like name) in the database upon login if the value returned by the OIDC server is different? I found no mention of this scenario in the docs / previous questions.

Thanks in advance

PowAssent will upsert the user identity, but not the user.

If you want to modify the user as well, you should be able to use custom context to do it, something like:

defmodule MyApp.UserIdentities do
  use PowAssent.Ecto.UserIdentities.Context,
    repo: MyApp.Repo,
    user: MyApp.Users.User

  def upsert(user, user_identity_params) do
    with {:ok, user_identity} <- pow_assent_upsert(user, user_identity_params),
         {:ok, _user} <- update_user(user, user_identity_params) do
      {:ok, user_identity}
    end
  end

  defp update_user(user, user_identity_params) do
    user
    # The user identities params will have all the user params on the `userinfo` key
    |> MyApp.Users.User.changeset(user_identity_params["userinfo"])
    |> MyApp.Repo.update()
  end
end
1 Like

Hello Dan,

Thanks for your help, it was exactly what was needed!