from django.conf import settings
from django.contrib.auth import login
from accounts.models import CustomUser
from django.contrib.auth.models import User
class AutoLoginMiddleware:
    """
    In development, automatically logs in as a predefined user if the request is anonymous.
    """
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        # Only perform auto-login if in DEBUG mode and user is not authenticated.
        if settings.DEBUG and not request.user.is_authenticated and request.host in ['pocket-trade.fly.dev', 'localhost', '127.0.0.1']:
            user = CustomUser.objects.get(email='rob@badblocks.email')
            login(request, user, backend='django.contrib.auth.backends.ModelBackend')

        response = self.get_response(request)
        return response