AuthTokenMiddleware class handle this problem.
import os
from django.core.asgi import get_asgi_application
from myapp.routing import ws_urlpatterns
from channels.routing import ProtocolTypeRouter , URLRouter
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "core.settings")
from channels.auth import AuthMiddlewareStack
from channels.auth import AuthTokenMiddleware
application = ProtocolTypeRouter({
"http": get_asgi_application(),
"websocket": AuthMiddlewareStack(AuthTokenMiddleware(URLRouter(ws_urlpatterns)))
})
class CustomConsumer(WebsocketConsumer):
def connect(self):
self.accept()
user = self.scope['user']
thanks for your attention!