aaronrenner / phx_gen_auth

An authentication system generator for Phoenix 1.5 applications.
772 stars 55 forks source link

Feature Request: Depend on ecto "ecto_repos" config to find Repo. #112

Closed iantbutler01 closed 3 years ago

iantbutler01 commented 3 years ago

Right now we are unable to generate auth templates because we use a Repo in one of our umbrella apps as the Repo for our main phoenix application.

This is our config:

config :oracle,
  ecto_repos: [Janus.Repo]

Where as from looking through the library it looks like you depend on finding the Repo file in the application itself.

josevalim commented 3 years ago

Do you get an error or exception or similar?

iantbutler01 commented 3 years ago

Yes but nothing particularly crazy, just that it can't find "Oracle.Repo" which in our case makes sense.

mix phx.gen.auth Accounts User users
** (Mix) Unable to find Oracle.Repo

I looked through the docs and the codebase a bit that looks like it's coming from here: https://github.com/aaronrenner/phx_gen_auth/blob/3736a7184071689f6e81ff528913563c114fb229/lib/mix/tasks/phx.gen.auth.ex

    {context, schema} = Gen.Context.build(context_args, __MODULE__)
    Gen.Context.prompt_for_code_injection(context)

    # Needed so we can get the ecto adapter and ensure other
    # libraries are loaded.
    #
    # As far as I can tell, everything after this must be tested with an
    # integration test.
    Mix.Task.run("compile")

    validate_required_dependencies!()

    ecto_adapter = get_ecto_adapter!(schema)

Then in

  defp get_ecto_adapter!(%Schema{repo: repo}) do
    if Code.ensure_loaded?(repo) do
      repo.__adapter__()
    else
      Mix.raise("Unable to find #{inspect(repo)}")
    end
  end

I tried digging into the Gen module to figure out how schema was being generated but I couldn't find it in the Phoenix hex docs.

Ah, I found how schema is being created in Phoenix's github repo, looks like the option for the repo may be coming from a default arg somewhere?

iantbutler01 commented 3 years ago

Okay I figured it out, if you set the ":generators, :context_app" setting to the app with the repo in it phx.gen.auth is able to generate. There are also issues with where the files were then generated in my app but I think that's because this wasn't originally an umbrella app so in terms of folder locations there are some non-standard project structure things going on but that's really not an issue with the library itself.

That said the way to change where generators place files, in my opinion, is not clearly documented for phoenix umbrella apps and the only reason we didn't run into this until recently is because this used to be a json api without ecto and didn't really need to use generators. I also just luckily stumbled upon this solution while debugging something else entirely and was about to just ignore generators in favor of moving along with our implementation and handling it manually. That's a more general issue with phoenix documentation for umbrella apps and generators though.

I recognize that general phoenix stuff is out of scope for this repo but I may open a documentation issue on the phoenix repo if you think it's worth it.

josevalim commented 3 years ago

I think a PR would be welcome. Feel free to copy me. I think the biggest question is where you would document that... I assume it is a hard think to know that you need until you actually learn it exists. :)