Customizing Ash JSONAPI Response

In one of the projects that I work on, we need json api authenticated by cookies. With AshJson I can put the forwarded jsonrouter through a pipeline like

  scope "/", HelloWeb do
    pipe_through :api
    forward "/api", ApiRouter
  end

and in the :api pipeline, I can fetch the cookies and set the context. However, I am facing issue with login, register and logout api endpoints where I need to set or clear the cookies. As AshJsonApi doesn’t expose any controller and does all the magic of rendering the JSON through the domain model, I’m wondering how I can intercept AshJsonAPI to set cookie values or should I just go plain old controllers without Ash for login/register/logout apis and use AshJsonApi for the rest?

Thanks for your inputs!

There are two ways to do it:

  1. As you said, write your own controller actions
  2. You can add a modify_conn/4 function to a route
post :create do
  route "/login"
  modify_conn fn conn, subject, result, request -> 
    # do what you want to the conn
  end
end
1 Like