Login form via modal window

I’m a complete newbie to webdev, so thank you for your patience.

Want my users to login with the pop-up modal window (using the default Login plugin) instead of detached page.

Now I have such a window with the html.twig template in it. Like the default login page template it has form with user/pw inputs, button with name “task” and value “login.login” and messages.twig.html for result.

It works, but the modal closes after submitting. Stack here with the js/ajax and need your help please. Algorithm is: 1) if username/password correct show “success” message in modal for a second, close the modal and refresh the main page; 2) else just show “login failed” message and keep the modal window opened.