mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-07-15 10:05:21 +00:00
login in only single device at a time
This commit is contained in:
36
account/custom_middleware.py
Normal file
36
account/custom_middleware.py
Normal file
@ -0,0 +1,36 @@
|
||||
"""middleware file"""
|
||||
"""Django import"""
|
||||
from rest_framework import status
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.renderers import JSONRenderer
|
||||
"""App django"""
|
||||
from account.utils import custom_error_response
|
||||
from account.models import UserDeviceDetails
|
||||
from base.messages import ERROR_CODE, SUCCESS_CODE
|
||||
|
||||
"""Custom middleware
|
||||
when user login with
|
||||
multiple device simultaneously"""
|
||||
class CustomMiddleware:
|
||||
"""Custom middleware"""
|
||||
def __init__(self, get_response):
|
||||
"""response"""
|
||||
self.get_response = get_response
|
||||
|
||||
def __call__(self, request):
|
||||
# Code to be executed before the view is called
|
||||
response = self.get_response(request)
|
||||
# Code to be executed after the view is called
|
||||
device_id = request.META['HTTP_DEVICE_ID']
|
||||
if request.user.is_authenticated:
|
||||
"""device details"""
|
||||
device_details = UserDeviceDetails.objects.filter(user=request.user, device_id=device_id).last()
|
||||
if not device_details:
|
||||
custom_error = custom_error_response(ERROR_CODE['2037'], response_status=status.HTTP_404_NOT_FOUND)
|
||||
response = Response(custom_error.data, status=status.HTTP_404_NOT_FOUND)
|
||||
# Set content type header to "application/json"
|
||||
response['Content-Type'] = 'application/json'
|
||||
# Render the response as JSON
|
||||
renderer = JSONRenderer()
|
||||
response.content = renderer.render(response.data)
|
||||
return response
|
Reference in New Issue
Block a user