shahryarjb
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
how can I do this ?
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 8- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
kokolegorille
Given that conn is a struct, I would update it like this…
BTW It really depends what You want to do with assigns, You could use Map.update, or Map.merge or anything You need depending on situation.
With Map.update…
shahryarjb
Thanks , I edited your code and now it works for me
code :
kokolegorille
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.
Only if You want to add multiple keys, You would use Map.merge
You choose depending on your requirements.
I prefer to_string(climes[“ip”]}) over “#{climes[“ip”]}” when then is no real interpolation needed, but that is personal preference.
If your solution is working, that is fine too
peerreynders
With that out of the way:
Why? What is forcing you to break with convention:
What is it that makes it impossible to use:
Is there a compelling reason that
last_iphas to be a string key?shahryarjb
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 :
Thank you too , yeh your help did that for me. and now I edited it in order to improve.
peerreynders
Yes but action functions receive 2 parameters:
Plug.ConnConnection fields:So rather than polluting
Plug.Conn.paramswith non-standard data, you should usePlug.Conn.assignswhich is intended for “user data”, e.g.:and then inside the action function retrieve it:
shahryarjb
Hello @peerreynders , I can’t use this , because I send params which I want like this :
I send
allreqbyI send
allreqwhen 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"]}"})}peerreynders
The idea is not to subvert the intent behind
Plug.Conn.params- it’s not a general data “dumping ground” - that isPlug.Conn.assignsjob. So the strategy would be to put the information inPlug.Conn.assignsfirst and then combine it in the last possible moment. So start withAnd then have a helper function that assembles everything
which you can use at the last possible moment, like:
You really shouldn’t be sending the entire contents of the
Plug.Connstructure.Plug.ConnPlug.Connstructure could change in the next versionPlug.Connverbatim you are unnecessarily coupling other parts of your system to it (and the version that you started using).In the interest of decoupling:
Plug.Conn.