ash-project / ash_json_api

The JSON:API extension for the Ash Framework
https://hexdocs.pm/ash_json_api
MIT License
56 stars 40 forks source link

Support being able to sign in via ash_authentication #113

Closed sevenseacat closed 2 months ago

sevenseacat commented 6 months ago

A standard API sign-in action from AA looks something like this -

read :sign_in_with_longlife_token do
  argument :username, :string, allow_nil?: false
  argument :password, :string, allow_nil?: false

  metadata :token, :string

  prepare set_context(%{
            token_type: :user,
            strategy_name: :password,
            private: %{ash_authentication?: true}
          })

  prepare AshAuthentication.Strategy.Password.SignInPreparation
end

And I came across two issues when trying to get this exposed in a JSON API.

Request: GET /api/open_api
** (exit) an exception was raised:
    ** (KeyError) key :accept not found in: %Ash.Resource.Actions.Read{
  arguments: [
    %Ash.Resource.Actions.Argument{
      allow_nil?: false,
      type: Ash.Type.String,
      name: :username...

It expects that the action will always have an accept option, which reads don't support.

zachdaniel commented 2 months ago

@sevenseacat I've pushed this to main. Feel free to try it out, but it will be a few days at least before a proper release is cut. The two major changes are allowing read actions to be used with the post route type, and adding a modify_conn/4 option that can be specified on any route. See the new authenticate-with-json-api.md guide for more details.

sevenseacat commented 2 months ago

Fantastic! Thank you 🫶