pow-auth / pow_assent

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

Azure AD: "Record does not exist" on subsequent logins #206

Open pehbehbeh opened 3 years ago

pehbehbeh commented 3 years ago

We just setup Azure AD. The first login / registration works like a charm. But when we logout and login again, we get the following error:

Request: POST /auth/azure/callback
** (exit) an exception was raised:
    ** (RuntimeError) Record does not exist: %MyApp.Accounts.UserIdentity{__meta__: #Ecto.Schema.Metadata<:loaded, "user_identities">, id: "xxx-xxx-xxx-xxx-xxx", inserted_at: ~N[2021-02-12 12:49:00], provider: "azure", uid: "x-xxx-xxx", updated_at: ~N[2021-02-12 12:49:00], user: #Ecto.Association.NotLoaded<association :user is not loaded>, user_id: "xxx-xxx-xxx-xx-xxx"}
        (pow 1.0.22) lib/pow/ecto/context.ex:219: Pow.Ecto.Context.reload_after_write/2
        (pow_assent 0.4.10) lib/pow_assent/ecto/user_identities/context.ex:154: PowAssent.Ecto.UserIdentities.Context.upsert/3
        (pow_assent 0.4.10) lib/pow_assent/plug.ex:315: PowAssent.Plug.upsert_identity/2
        (pow_assent 0.4.10) lib/pow_assent/plug.ex:149: PowAssent.Plug.maybe_upsert_user_identity/1
        (pow_assent 0.4.10) lib/pow_assent/plug.ex:100: PowAssent.Plug.callback_upsert/4
        (pow 1.0.22) lib/pow/phoenix/controllers/controller.ex:77: Pow.Phoenix.Controller.action/3
        (pow_assent 0.4.10) lib/pow_assent/phoenix/controllers/authorization_controller.ex:1: PowAssent.Phoenix.AuthorizationController.action/2
        (pow_assent 0.4.10) lib/pow_assent/phoenix/controllers/authorization_controller.ex:1: PowAssent.Phoenix.AuthorizationController.phoenix_controller_pipeline/2

The record for the user_identity (and the user) definitely exists in the database. Do you have any idea how to solve this problem?

danschultzer commented 3 years ago

Can you verify that the :id printed does match with an existing record? Does this happen every time you log in? The reload logic just pulls the primary key(s) for the struct and then runs a Repo.get_by call (though I plan to remove these calls).

RodolfoSilva commented 3 years ago

@danschultzer I've the same issue when I try use a Custom ID generator, like: Exnowflake

I can register the user with the GitHub provider. But I can't log in after the session expires:

image

image

I thought it was related to belongs_to. But it seems not.

https://elixirforum.com/t/custom-useridentity-belongs-to-type/37530

pehbehbeh commented 3 years ago

Can you verify that the :id printed does match with an existing record?

Yes.

Does this happen every time you log in?

Also yes.

Anything else I can do?

RodolfoSilva commented 3 years ago

@pehbehbeh try to use the master branch of pow. This worked for me.

pehbehbeh commented 3 years ago

@pehbehbeh try to use the master branch of pow. This worked for me.

The master branch works for me. Thanks!

joshuataylor commented 3 years ago

I also get this for a custom integration, looks like it's from https://github.com/danschultzer/pow/commit/4b3433b5d6496f29058e5b5b35ba6a33ea69bbe0

Can we get a release for this? Happy using master until then. Thanks!