redpoll
Strange behaviour on authentication (phx.gen.auth)
Hi, today I generated a new auth admin in my phoenix 1.7.7 app
mix phx.gen.auth Accounts Admin admins
and after creating a new admin using the built-in form everything seems to be alright. Well.. almost everything. When logged in, the admin token is seen
session: %{
"_csrf_token" => "qWmAvVGzs...",
"admin_return_to" => "/dashboard",
"admin_token" => <<195, 253, (...)>>,
"live_socket_id" => "admins_sessions:w_3CYZGonln4 ..."
}
and following a page, for example admins/log_in successfully redirect to /. The problem is that the routes which require authenticated admin still require it. I am clicking on the settings button which goes to /admins/settings and the toast says me I need to log in, but i was already logged in. I tried to put different route in that scope, but the error still remains with the “new” live route.
scope "/", TennisWeb do
pipe_through([:browser, :require_authenticated_admin])
live_session :require_authenticated_admin,
on_mount: [{TennisWeb.AdminAuth, :ensure_authenticated}] do
live("/admins/settings", AdminSettingsLive, :edit)
live("/admins/settings/confirm_email/:token", AdminSettingsLive, :confirm_email)
end
end
I have no idea what could be wrong. For sure the wrong is with me, but what could it be?
Best Regards
Most Liked
karlosmid
Hi!
session can not be updated in live view.
You need to submit live view login form to phoenix controller that will do the authentication of user credentials.
In controller you store user_id in session. In on_mount callback you read user_id from that session.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








