websocket authentication

This commit is contained in:
jonaswinkler 2021-02-03 20:44:10 +01:00
parent 9ad9a99086
commit 441c8b28f0

View File

@ -1,14 +1,19 @@
import json
from asgiref.sync import async_to_sync
from channels.exceptions import DenyConnection, AcceptConnection
from channels.generic.websocket import WebsocketConsumer
class StatusConsumer(WebsocketConsumer):
def connect(self):
self.accept()
async_to_sync(self.channel_layer.group_add)(
'status_updates', self.channel_name)
if not self.scope['user'].is_authenticated:
raise DenyConnection()
else:
async_to_sync(self.channel_layer.group_add)(
'status_updates', self.channel_name)
raise AcceptConnection()
def disconnect(self, close_code):
async_to_sync(self.channel_layer.group_discard)(