Described at https://matomo.org/faq/how-to/#faq_187, you should force new visit AFTER user was logged in. As different user will be redirected to different home pages, It would be easier for us to call it everytime the login page is opened and after login on any page rendered we trigger setUserId afterwards.
To track the userId, we make use of customDimension userId as well, because with the described flow, the userId will be a random one in Matomo and not the one sent to Matomo, at the same time it matches 1:1 to the one, which is sent afterwards.
Does this flow and solution is recommended as well, or is there any issue with it?
Btw. it whould be nice to attach some flow diagram about possible options how to setup matomo for user logged-in applications. Right now, information is spreaded across multiple documentation pages.