Here are some steps you can take to address this issue:
1. **Check Energy Saver Settings**:
- Go to "Apple Menu" > "System Preferences" > "Energy Saver."
- Make sure you've configured the "Computer Sleep" and "Display Sleep" settings to a longer duration, or set them to "Never" if you don't want the computer to sleep or the display to turn off.
2. **Check Screen Saver Settings**:
- Go to "System Preferences" > "Desktop & Screen Saver."
- Click on the "Screen Saver" tab.
- Ensure that the "Start after" slider is set to a duration that you're comfortable with, or set it to "Never."
3. **Check for External Devices**: Sometimes, connected external devices (e.g., external hard drives) can cause the computer to wake or sleep unexpectedly. Try disconnecting any external devices and see if the issue persists.
4. **Check for Scheduled Tasks**:
- Go to "System Preferences" > "Security & Privacy" > "Privacy" tab > "Advanced."
- Make sure there are no scheduled tasks that are causing the computer to lock or sleep.
5. **Check for Third-Party Apps**: Some third-party applications may have settings or features that can cause your Mac to go to sleep or lock automatically. Review your installed apps and their settings to see if any of them are responsible for this behavior.
6. **Reset SMC and NVRAM/PRAM**:
- Resetting the System Management Controller (SMC) and NVRAM/PRAM can sometimes resolve power-related issues. Refer to the previous response for instructions on how to reset these components.
7. **Check for Malware**: Perform a thorough scan for malware or unwanted software that might be affecting your Mac's behavior.
8. **Check for macOS Updates**: Ensure that your macOS is up to date by going to "Apple Menu" > "About This Mac" > "Software Update." Sometimes, software updates include fixes for power management issues.
9. **Create a New User Account**: As a troubleshooting step, create a new user account and see if the issue persists when using that account. If it doesn't, the problem may be related to your user profile.
10. **Hardware Diagnosis**: If none of the above steps resolve the issue, it's possible that there could be a hardware problem with your iMac's power management system. In this case, it's advisable to contact Apple Support or visit an Apple Store for further assistance.
By following these steps, you should be able to identify and resolve the issue causing your iMac's screen to go black and require re-login unexpectedly.