hello, I need to add some Map in phoenix conn’s params in Plug
for example :
def init(_params) do
end
def call(%Plug.Conn{params: %{"ptoken" => ptoken}} = conn, params) do
outputs = case Guardian.decode_and_verify(ptoken) do
{:ok, _climes} ->
# I need to add it in blow conn , map like : %{"last_ip" => "127.1.2.3"}
assign(conn, :user_ip_checked, params)
{:error, _ms} ->
conn
|> put_status(403)
|> json(%{error_code: "403", error_msg: "You don't have an access."})
|> halt()
end
outputs
end
If You have only one key, “last_ip”, You could use Map.put like this. Use Map.put if You don’t care what was the previous value. Use Map.update if You want to combine the previous value with the new one.
Hello , thank you for this, I have 3 microservices, the Ms1 sends Jwt token to Ms2 and Ms2 send it to M3
The token has 2 things like user Ip and Os info which is string , and I need to receive them in all router’s fn in Ms3, then I take them when I valid the token
my code is :
defmodule TrangellApiGateway.Plug.CheckPrivateToken do
import Plug.Conn
import Phoenix.Controller
alias TrangellApiGateway.GuardianLib.Guardian
def init(_params) do
end
def call(%Plug.Conn{params: %{"ptoken" => ptoken}} = conn, params) do
outputs = case Guardian.decode_and_verify(ptoken) do
{:ok, climes} ->
conn = %{conn | params: Map.merge(conn.params, %{"last_ip" => "#{climes["ip"]}"})}
assign(conn, :user_ip_checked, params)
{:error, _ms} ->
conn
|> put_status(403)
|> json(%{error_code: "403", error_msg: "You don't have an access."})
|> halt()
end
outputs
end
def call(conn, _params) do
conn
|> put_status(403)
|> json(%{error_code: "403", error_msg: "You don't have an access."})
|> halt()
end
end
you think why my way has a problem? I didn’t edit it , I had just updated Phoenix’s params before sending to normal action function.
Thank you too , yeh your help did that for me. and now I edited it in order to improve.
def get_posts_category_cms_sender(params) do
post_sender_json_without_authorization(params, "post/category")
end
# POST
defp post_sender_json_without_authorization(params, link) do
body = Jason.encode!(params)
HTTPoison.post(
"#{@link}#{link}",
body,
[
{"Content-Type", "application/json; charset=UTF-8"},
],
[timeout: 50_000, recv_timeout: 50_000]
)
end
I send allreq when I put os info and ip in params plug , do you understand what I sad that why I send it like this ?
do you have a suggestion which I use instead of my way ?
for ex :
in real I send blow codes : allreq + conn = %{conn | params: Map.merge(conn.params, %{"last_ip" => "#{climes["ip"]}"})}
The idea is not to subvert the intent behind Plug.Conn.params - it’s not a general data “dumping ground” - that is Plug.Conn.assigns job. So the strategy would be to put the information in Plug.Conn.assigns first and then combine it in the last possible moment. So start with
You really shouldn’t be sending the entire contents of the Plug.Conn structure.
The actual content may change in the next version of Plug.Conn
The organization of the Plug.Conn structure could change in the next version
By using Plug.Conn verbatim you are unnecessarily coupling other parts of your system to it (and the version that you started using).
In the interest of decoupling:
Decide what minimal information is actually useful.
Structure that information in a way the makes sense to your system. Your information structure has different reasons to change than those that will force a change on Plug.Conn.