smpallen99 / coherence

Coherence is a full featured, configurable authentication system for Phoenix
MIT License
1.27k stars 224 forks source link

registration_create callback isn't called when allow_unconfirmed_access is enabled #320

Open quentindemetz opened 6 years ago

quentindemetz commented 6 years ago

Is this expected behavior? I would like to perform some one-time setup for a user and thus don't really know where to put it.

Thanks for your help, I'm loving this library so far, great work 👍

aalfson commented 5 years ago

@smpallen99 -> I'm also in the position where I need to do some additional setup work when a user is created. How do you feel about adding in a configurable callback like the registration_success_callback detailed below? The big advantage to making it a configuration option is that it avoids the need to generate and maintain the Coherence controllers.

I'd be more than happy to take this on if its something you would be interested in seeing done.

# %% Coherence Configuration %%   Don't remove this line
config :coherence,
  user_schema: MyApp.Coherence.User,
  repo: MyApp.Repo,
  module: MyApp,
  web_module: MyAppWeb,
  router: MyAppWeb.Router,
  messages_backend: MyAppWeb.Coherence.Messages,
  logged_out_url: "/",
  registration_permitted_attributes: ["email","name","password","current_password","password_confirmation"],
  invitation_permitted_attributes: ["name","email"],
  password_reset_permitted_attributes: ["reset_password_token","password","password_confirmation"],
  session_permitted_attributes: ["remember","email","password"],
  email_from_name: "Your Name",
  email_from_email: "yourname@example.com",
  opts: [:authenticatable, :recoverable, :lockable, :trackable, :unlockable_with_token, :confirmable, :registerable],
  layout: {MyAppWeb.LayoutView, :auth},
  registration_success_callback: &MyApp.do_extra_work/3

config :coherence, MyAppWeb.Coherence.Mailer,
  adapter: Swoosh.Adapters.Sendgrid,
  api_key: "your api key here"
# %% End Coherence Configuration %%

I added registration_success_callback: &MyApp.do_extra_work/3 in the configuration options above.

@doc """
  Create the new user account.

  Creates the new user account. Create and send a confirmation if
  this option is enabled.
  """
  @spec create(conn, params) :: conn
  def create(conn, %{"registration" => registration_params} = params) do
    user_schema = Config.user_schema
    :registration
    |> Controller.changeset(user_schema, user_schema.__struct__,
      Controller.permit(registration_params, Config.registration_permitted_attributes() ||
        Schema.permitted_attributes_default(:registration)))
    |> Schemas.create
    |> case do
      {:ok, user} ->
        conn
        |> registration_success_callback(user, user_schema, Config.registration_success_callback())
        |> send_confirmation(user, user_schema)
        |> redirect_or_login(user, params, Config.allow_unconfirmed_access_for)
      {:error, changeset} ->
        respond_with(conn, :registration_create_error, %{changeset: changeset})
    end
  end

  defp registration_success_callback(conn, _user, _user_schema, nil), do: conn

  defp registration_success_callback(conn, user, user_schema, callback) do
    callback.(conn, user, user_schema)
    conn
  end

The create function above is found in Coherence.RegistrationController. I added the call to registration_success_callback(user, user_schema, Config.registration_success_callback()) in the success path.

ringofhealth commented 5 years ago

any updates on this?