shankardevy

shankardevy

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!

Marked As Solved

zachdaniel

zachdaniel

Creator of Ash

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

Where Next?

Popular in Questions Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36689 110
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49134 226
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New

We're in Beta

About us Mission Statement