Fix for login with redirection [fixes #3032 and spent 1.5 hours on #3032]