Wait for ajax before do login things

There were some weird errors around user logout and login, so I have put wait for ajax there to protect it.