Dear phoenix programmers,
I am getting crazy with something that looks like a simple (and answered problem).
However I am still stuck.
I created:
mix phx.gen.auth Accounts User users
mix phx.gen.live Sms_service Sms sms service:string number:string username:string
As you might imagine, I want to save the current username (email) in the tests table.I add the routes, as suggested by the mix command, in the route.ex, then I assign the current_email in the mount function, I can see it’s here when I click on “new sms”:
socket #=> #Phoenix.LiveView.Socket<
id: "phx-GDEG1QiduAhv86ui",
endpoint: PlatacalienteWeb.Endpoint,
view: PlatacalienteWeb.SmsLive.Index,
parent_pid: nil,
root_pid: #PID<0.47320.0>,
router: PlatacalienteWeb.Router,
assigns: %{
__changed__: %{live_action: true},
page_title: "Listing Sms",
current_user: #Platacaliente.Accounts.User<
__meta__: #Ecto.Schema.Metadata<:loaded, "users">,
id: 5,
email: "masd@gmail.com",
confirmed_at: nil,
inserted_at: ~U[2025-03-28 15:02:40Z],
updated_at: ~U[2025-03-28 15:02:40Z],
...
>,
flash: %{},
sms: nil,
live_action: :new,
streams: %{
__changed__: MapSet.new([]),
sms_collection: %Phoenix.LiveView.LiveStream{
name: :sms_collection,
dom_id: #Function<3.121959031/1 in Phoenix.LiveView.LiveStream.new/4>,
ref: "0",
inserts: [],
deletes: [],
reset?: false,
consumable?: false
},
__configured__: %{},
__ref__: 1
},
current_email: "masd@gmail.com"
},
transport_pid: #PID<0.47312.0>,
...
>
But then, when I finish to click on “save”, here is the value of socket I am getting:
[debug] HANDLE EVENT "save" in PlatacalienteWeb.SmsLive.Index
Component: PlatacalienteWeb.SmsLive.FormComponent
Parameters: %{"sms" => %{"number" => "popo", "service" => "lolo", "username" => "coco"}}
[lib/platacaliente_web/live/sms_live/form_component.ex:80: PlatacalienteWeb.SmsLive.FormComponent.save_sms/3]
socket #=> #Phoenix.LiveView.Socket<
id: "phx-GDEG1QiduAhv86ui",
endpoint: PlatacalienteWeb.Endpoint,
view: PlatacalienteWeb.SmsLive.Index,
parent_pid: nil,
root_pid: #PID<0.666.0>,
router: PlatacalienteWeb.Router,
assigns: %{
id: :new,
title: "masd@gmail.com",
form: %Phoenix.HTML.Form{
source: #Ecto.Changeset<
action: :validate,
changes: %{service: "lolo", number: "popo", username: "coco"},
errors: [],
data: #Platacaliente.Sms_service.Sms<>,
valid?: true,
...
>,
impl: Phoenix.HTML.FormData.Ecto.Changeset,
id: "sms",
name: "sms",
data: %Platacaliente.Sms_service.Sms{
__meta__: #Ecto.Schema.Metadata<:built, "sms">,
id: nil,
service: nil,
number: nil,
username: nil,
inserted_at: nil,
updated_at: nil
},
action: :validate,
hidden: [],
params: %{"number" => "popo", "service" => "lolo", "username" => "coco"},
errors: [],
options: [method: "post"],
index: nil
},
action: :new,
patch: "/sms",
__changed__: %{},
flash: %{},
sms: %Platacaliente.Sms_service.Sms{
__meta__: #Ecto.Schema.Metadata<:built, "sms">,
id: nil,
service: nil,
number: nil,
username: nil,
inserted_at: nil,
updated_at: nil
},
myself: %Phoenix.LiveComponent.CID{cid: 1}
},
transport_pid: #PID<0.650.0>,
...
>
So I changed the title, to pass the current_email to my component.
This could work, but the field is then controllable by the user, and a logged user could register an SMS under a fake username, by modifying the response to the server.
Basically, when I receive the event from my component, I lose the session information and I have no clue about the current user.
I read 1000 times Security considerations — Phoenix LiveView v1.0.9 and many other posts on this forum, but I am stuck.
I would very appreciate your help.
Thank you.