danschultzer / phoenix_oauth2_provider

Get an OAuth 2 provider running in your phoenix with controllers, views and models in just two minutes
MIT License
84 stars 41 forks source link

Compile issue when after upgrading from your Phoenix 1.4 branch #22

Closed chasers closed 5 years ago

chasers commented 5 years ago

Thanks for updating this! Was actually thinking about it the other day.

However now I'm getting:

== Compilation error in file lib/logflare_web/router.ex ==
** (FunctionClauseError) no function clause matching in Keyword.get/3    

    The following arguments were given to Keyword.get/3:

        # 1
        :public

        # 2
        :path

        # 3
        "oauth"

    Attempted function clauses (showing 1 out of 1):

        def get(keywords, key, default) when is_list(keywords) and is_atom(key)

    (elixir) lib/keyword.ex:191: Keyword.get/3
    lib/logflare_web/router.ex:40: (module)
    (stdlib) erl_eval.erl:680: :erl_eval.do_apply/6
    (elixir) lib/kernel/parallel_compiler.ex:208: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/6
danschultzer commented 5 years ago

You should update your routes to this:

  pipeline :protected do
    # Require user authentication
  end

  scope "/" do
    pipe_through :api

    oauth_api_routes()
  end

  scope "/" do
    pipe_through [:browser, :protected]

    oauth_routes()
  end
danschultzer commented 5 years ago

Also the changelog has the upgrade steps required: https://github.com/danschultzer/phoenix_oauth2_provider/blob/v0.5.0/CHANGELOG.md#v050-2019-05-08

chasers commented 5 years ago

Hmmm now:

rotocol.UndefinedError at GET /oauth/authorized_applications
protocol Ecto.Queryable not implemented for Logflare.OauthAccessTokens.OauthAccessToken, the given module does not exist. This protocol is implemented for: BitString, Tuple, Ecto.Query, Atom, Ecto.SubQuery
danschultzer commented 5 years ago

Run mix ex_oauth2_provider.gen.schemas. The schema modules are now required to be generated, but it makes it much more explicit and easy to work with.

chasers commented 5 years ago

Okay now this:

UndefinedFunctionError at GET /oauth/authorized_applications
function nil.all/1 is undefined
danschultzer commented 5 years ago

Remove the previous PhoenixOauth2Provider config and change it to this:

config :logflare, ExOauth2Provider,
  repo: Logflare.Repo,
  resource_owner: Logflare.User,
  grant_flows: ~w(authorization_code),
  use_refresh_token: true,
  default_scopes: ~w(public),
  optional_scopes: ~w(read write),
  revoke_refresh_token_on_use: true

config :logflare, PhoenixOauth2Provider,
  current_resource_owner: :user,
  web_module: LogflareWeb
chasers commented 5 years ago

Sweet, and now it seems I have to do something with my views?

Thanks a lot, this is much appreciated.

chasers commented 5 years ago

I tried mix phoenix_oauth2_provider.gen.templates but it didn't seem to regen anything.

chasers commented 5 years ago

Oh wait, it's prompting me... hold on.

danschultzer commented 5 years ago

It's probably because the view files are already there, but the module names are different. You can just delete the lib/logflare_web/views/phoenix_oauth2_provider folder and then run the mix task again. The templates folders lib/logflare_web/templates/{application, authorization, authorized_application} should be moved to lib/logflare_web/templates/phoenix_oauth_provider/{application, authorization, authorized_application}

chasers commented 5 years ago

Okay I think I can manage from here! Got one of my view working.

danschultzer commented 5 years ago

Great, and thanks for the feedback! I've used it to update the changelog with better instructions.

I think the best way is just to regenerate all the templates/views from scratch, and then update the templates after with your previous styling. There's a few changes to what assigns there is access to (e.g. @application is now @changeset.data in some of them).

In the end, these changes will make PhoenixOauth2Provider much easier to work with in the future, and the new structure should make your project cleaner to navigate 😄

chasers commented 5 years ago

Glad it helped.

btw I didn't have an updated_at column in the oauth_access_grants. Seems the originally generated migration omitted it for some reason and now it is expected.

This helped:

def change do
    alter table(:oauth_access_grants) do
      timestamps(default: "2019-05-08 00:00:01", inserted_at: false)
    end
  end
danschultzer commented 5 years ago

That works. You can also just set the :updated_at option in the timestamp/1 schema macro in the Logflare.OauthAccessGrants.OauthAccessGrant module:

  schema "oauth_access_grants" do
    access_grant_fields()

    timestamps(updated_at: false)
  end
chasers commented 5 years ago

Ah nice ... that worked too. Would rather do that I think.

chasers commented 5 years ago

I should be integrating Pow soon actually. Need to build billing first but I think after that.