Are you sure that userPrincipalName return emai? Why not to use mail instead?

From what I see on the first screenshot however, the issue is before that. you have an error description code in the url (and probably more information after that related to the error), this should help to find the issue.