As for channels 1.x
As already mentioned here, mixin by leonardoo is the easiest way: https://gist.github.com/leonardoo/9574251b3c7eefccd84fc38905110ce4
I think, however, it is somewhat difficult to understand what mixin does and what does not, so I will try to make it clear:
When looking for a way to access message.user using the django decorative channel decorators, you need to implement it as follows:
@channel_session_user_from_http def ws_connect(message): print(message.user) pass @channel_session_user def ws_receive(message): print(message.user) pass @channel_session_user def ws_disconnect(message): print(message.user) pass
Channels do this by authenticating the user, creating an http_session and then converting http_session to channel_session, which uses the response channel instead of cookies to identify the client. All this is done in channel_session_user_from_http . Look at the source code of the channels for more details: https://github.com/django/channels/blob/1.x/channels/sessions.py
leonardoo decorator rest_token_user , however, does not create a channel session, it just saves the user in the message object in ws_connect. Since the token is not sent again to ws_receive, and the message object is also unavailable to get the user in ws_receive and ws_disconnect, you will need to save it in the session yourself. This would be an easy way to do this:
@rest_token_user
source share