munksgaard

munksgaard

CSRF error when logging in in two tabs

I’m, using the authentication system provided by phx.gen.auth, and am generally pretty happy with it. However, I’m experiencing issues when I have more than one tab open.

To reproduce, simply open two tabs, log in to the first and then log in again in the second tab. The result is a Plug.CSRFProtection.InvalidCSRFTokenError. Here is the log output when running mix phx.server:

[info] POST /accounts/log_in
[debug] Processing with SorgenfriWeb.UserSessionController.create/2
  Parameters: %{"_csrf_token" => "BQEcEBFGMAU6By4DfTYTCCMfcx0PPR4yvwYZTvZcbIb6LgvFQH1DgGRq", "account" => %{"email" => "philip@munksgaard.me", "password" => "[FILTERED]", "remember_me" => "false"}}
  Pipelines: [:browser, :redirect_if_user_is_authenticated]
[info] Sent 403 in 19ms
[debug] ** (Plug.CSRFProtection.InvalidCSRFTokenError) invalid CSRF (Cross Site Request Forgery) token, please make sure that:

  * The session cookie is being sent and session is loaded
  * The request include a valid '_csrf_token' param or 'x-csrf-token' header
    (plug 1.15.3) lib/plug/csrf_protection.ex:316: Plug.CSRFProtection.call/2
    (sorgenfri 0.1.0) SorgenfriWeb.Router.browser/2
    (sorgenfri 0.1.0) lib/sorgenfri_web/router.ex:1: SorgenfriWeb.Router.__pipe_through1__/1
    (phoenix 1.7.11) lib/phoenix/router.ex:475: Phoenix.Router.__call__/5
    (sorgenfri 0.1.0) lib/sorgenfri_web/endpoint.ex:1: SorgenfriWeb.Endpoint.plug_builder_call/2
    (sorgenfri 0.1.0) deps/plug/lib/plug/debugger.ex:136: SorgenfriWeb.Endpoint."call (overridable 3)"/2
    (sorgenfri 0.1.0) lib/sorgenfri_web/endpoint.ex:1: SorgenfriWeb.Endpoint.call/2
    (phoenix 1.7.11) lib/phoenix/endpoint/sync_code_reload_plug.ex:22: Phoenix.Endpoint.SyncCodeReloadPlug.do_call/4
    (plug_cowboy 2.7.0) lib/plug/cowboy/handler.ex:11: Plug.Cowboy.Handler.init/2
    (cowboy 2.10.0) /home/munksgaard/src/sorgenfri/deps/cowboy/src/cowboy_handler.erl:37: :cowboy_handler.execute/2
    (cowboy 2.10.0) /home/munksgaard/src/sorgenfri/deps/cowboy/src/cowboy_stream_h.erl:306: :cowboy_stream_h.execute/3
    (cowboy 2.10.0) /home/munksgaard/src/sorgenfri/deps/cowboy/src/cowboy_stream_h.erl:295: :cowboy_stream_h.request_process/3
    (stdlib 4.3.1.3) proc_lib.erl:240: :proc_lib.init_p_do_apply/3

I’m pretty sure the problem has to do with the fact that the session cookie has been updated in another tab, but not in this one (since log_in is a liveview process), but I am unsure how best to solve it.

A similar error happens if I log out of one tab and then quickly attempt to log out of another before the disconnected liveview forces a refresh.

I think this issue is related to both User authentication across multiple tabs with LiveView and Log out causes "attempting to reconnect" messages on additional browser tabs - #4 by RobertoSchneiders, and the solution might be similar. However, it seems a bit strange to me that the default behavior does not handle multiple tabs better. How have other people handled this issue?

Most Liked

munksgaard

munksgaard

I created an issue on the Phoenix repository, and SteffenDE was so kind as to provide this workaround.

Where Next?

Popular in Questions Top

RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
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
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
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
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44139 214
New

We're in Beta

About us Mission Statement