I have a project that used to run under WSGI, and I recently configured it to ASGI. It included code from back then, such as the following function that processes a login request:
def authenticate(req):
...
try:
query = models.User.objects.get(...)
req.session['user'] = query.id #Login successful
...
except:
#No record, reject request
...
What I want to do now is to create a WebsocketConsumer and access the session from within. I have enabled SessionMiddlewareStack. The problem is that the consumer does not even see the session because the session's ID is not attached to the WebSocket request as a header. Is there a way to allow it to access the data?
class SpaceConsumer(WebsocketConsumer):
def connect(self):
#Session object is empty, while HTTP requests see it as populated with the user's ID
if 'user' in self.scope['session']:
self.accept() #Only allow authenticated user
Information online is scarce, and most are written for older versions of Channels. I am using Channels v3.