When to log in the user during shopping process?

I need to make sure the users are logged in (and thus registered) before they can purchase a product in a game-related platform that I am working on.

My question is if there's the best moment for them to be asked to log in (or register).

I've seen platforms that ask to log in right after clicking the "Buy" button on a particular game page, as well as those on which you can browse and browse and collect items in the cart and then when you want to go to checkout or make any further move from the cart (like "Buy for myself" or "Buy as a gift") you have to log in or register.

I think that the second option is closer to ideal, because the users have more freedom to browse before they are "forced" to log in and probably by this browsing experience they would be more engaged to register if they don't have the account?

I made the flow like this so far:

1) The user is browsing and clicks "Add to cart" button

2) The small pop up shows up saying "You added X to the cart" with options to either view the shopping cart or continue browsing (the previous "Add to cart" button on the site changes to "Go to cart" button)

3) When the user clicks "Go to cart" button (either in the pop up or directly from the site) they are taken to shopping cart, obviously.

4) Here I am thinking that maybe when the user clicks "Checkout" after looking for the cart content and deciding to go through with the purchase I can take them to "Log in/Register" page? Once they've made their mind about the purchase and so on. Or is it going to cause the friction in their ready-to-pay flow?

(And before you tell me I need to make the 3rd option possible - to let users buy as guests - we're not doing this, I asked.)