Added logic to the login process to detect invalid sessions that are supposed to be valid.