Submit login form sometimes don't login the customer

Hi, I’m developing an app embed block for my theme app extension. This app embed block reads query parameters in the URL and if there is a specific parameter in the URL, I put some customer credentials in the form and then submit the form to login as a customer. When I do this, the store shows me that I’m already logged in, but sometimes even though it shows me that I’m logged in when I go to the customer profile or when I reload the page it turns out that I’m not actually logged in. I’ve already tried instead of using the form submit to click the form button with javascript and I get the same problem. This doesn’t happen to me all the time but it’s quite common. Any ideas?