Integrate MongoDB database functionality into the Next.js application to support user authentication features, including both login and signup processes. Implement secure password storage and retrieval mechanisms. Ensure the database connection is established and managed efficiently, with appropriate error handling for database-related operations. The signup process should validate user inputs, such as email format and password strength, before storing user data. The login process should verify user credentials against the stored data and manage user sessions securely.