Custom Middleware in django using session key

 from django.shortcuts import render,redirect

class SessionAuthenticationMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        print(f"Request path: {request.path}, Session key: {request.session.get('session_key')}")

        print("Middleware executed")  # Debugging line

        session_key = request.session.get('session_key')

        if not session_key:
            print("Session key missing, redirecting...")  # Debugging line

            # Allow access to the login page (which is at '/') and the signup page (which is at '/signup')
            allowed_paths = ['/', '/sign','/signup', '/otpcenter', '/resend_otp', '/validate-otp', '/Loginsubmit']
            if request.path in allowed_paths:
                return self.get_response(request)  # Allow access to login and signup pages

            return redirect('/')  # Redirect to login page

        return self.get_response(request)



Using Redirect is better

Comments

Popular posts from this blog

React Js

python mail sent using thread