pow-auth / pow_assent

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

Callback for creating new user #148

Closed alukito closed 4 years ago

alukito commented 4 years ago

Is there a way to listen to new user created event? I'm trying to use Phoenix.ControllerCallbacks, but I could not differentiate between new user and signed in user callback as both of them calls before_respond(Elixir.PowAssent.Phoenix.AuthorizationController, :callback, {:ok, conn}.

Digging deeper, I find that there is conn.private.pow_assent_action that is set to :registration when user is newly created, and is nil when user is signed in. Do you think this is the way to go? I'm thinking maybe there is better way, as I could not find this field anywhere in the code or docs.

Thank you.

danschultzer commented 4 years ago

I would usually put that in the context or DB rather than at plug level. What do you need the event for?

Also, the next release will have a much simplified controller setup: https://github.com/pow-auth/pow_assent/blob/master/CHANGELOG.md#v046-tba

With this change, you would just check conn.private[:pow_assent_callback_state] == {:ok, :create_user} to know if the user was created. More in the docs: https://github.com/pow-auth/pow_assent/blob/bf5038c9cfac9db04e799f5a2fe42d211f90450c/lib/pow_assent/plug.ex#L67-L86

alukito commented 4 years ago

I'm building a system which uses an event store/CQRS in parts of the system. I don't want to treat user registration using CQRS though, so pow is a great fit. However, I need to trigger a UserRegistered event each time a user is newly registered either through email confirmation or third party authorization. The implementation is in the context but I need a way to integrate with the rest of the system, and I thought controller callbacks are easier to use than custom controllers.

For email confirmation, def before_respond(Elixir.PowEmailConfirmation.Phoenix.ConfirmationController, :show, {:ok, user, conn} = result, _config) works great.

Oh, so conn.private[:pow_assent_callback_state] == {:ok, :create_user} was not released yet, I read that in the docs but my test keeps failing. I didn't realise the release version, my bad. I think the new change would work fine if it is stable and supported.

Thank you!

danschultzer commented 4 years ago

Released v0.4.6 🚀 Let me know if that works for you.

alukito commented 4 years ago

I'm only days to production, so I think I'll stick to v0.4.5 for now. I'll test it soon, so I think we could close this issue.