Try using a wired USB keyboard.
With some laptops I have seen the keyboard act this way.....or sometimes the other way where it won't work before logging into macOS, but works fine within macOS after login. Could be an issue with the built-in keyboard or the Logic Board, but there is no way to be certain which one without swapping parts.
If this is an Intel Mac, then you can try an SMC Reset and a PRAM Reset (hold the PRAM Reset for at least two chimes if possible...three on the older Magsafe models). You must use the built-in keyboard for the SMC Reset, but an external wired USB keyboard can be used for the PRAM Reset.
You can also try running the Apple Diagnostics to see if any hardware issues are detected. The only useful diagnostic is one which produces an error.
I highly recommend you perform a clean install of macOS in order to ensure the OS is clean & fully functional and that the previous owner did not leave something nasty behind. This should always be done whenever acquiring a used device. Here is an Apple article showing what the previous owner should have done before selling the Mac:
What to do before you sell, give away, or trade in your Mac - Apple Support
A clean install is when the drive is erased, followed by reinstalling the OS, so make sure to back up all your data first. I recommend creating a bootable macOS USB installer so you have more options in case something goes wrong.
Create a bootable installer for macOS - Apple Support
FYI, it is always best to provide the exact model of the Mac and version of macOS being used since different hardware & software can have their own issues & quirks, plus we may be able to provide a more specific answer instead of generalizations. You can get this information by clicking the Apple menu and selecting "About This Mac".