How can I make it work correctly before logging in?
It is working correctly. Your built in screen is the main screen and during boot all boot/console messages are sent (they’re usually hidden by the Apple logo). The login screen is set to display on the main screen so until you login to the GUI, there’s nothing to display on the secondary display.
But, based on your comments, what you want to do is use your Mac in closed lid or closed clamshell mode:
I have the displays mirrored so I was hoping to see the logon screen. The MacBook sits at the back of my desk partially hidden by the external monitor and I use the external monitor as my main screen so it’s a little inconvenient that I have to move it to see the screen better just to log in
If the display is partially hidden/obstructed and you have the contents mirrored and not as an extended display, this is a perfect scenario to use the Mac in closed clamshell mode. The secondary/external display will become the main display and boot and login screens will appear there.