I am using AsyncJsonWebsocketConsumer
I did all the configuration like adding daphne in INSTALLED_APPS, changing to as an application, and adding the channels layer.
I used the Django pre-built authentication class view to authenticate the user.
Are you sure you’re authenticating before the websocket gets opened? (That’s a common mistake. You don’t want to open the websocket on your initial page or the login page itself. You want to open the websocket on the landing page retreived after the login has been completed.)
Are you using a JavaScript framework for the front end?
Please provide more details about the specifics of how you’re opening the websocket and doing the authentication.
HTTP GET /accounts/login/?next=/ 200 [0.16, 127.0.0.1:60223]
HTTP POST /accounts/login/?next=/ 302 [0.92, 127.0.0.1:60223]
HTTP GET / 200 [0.17, 127.0.0.1:60223]
You user: ugthesep
This means that the user is authenticated before the consumer
Checked whether the user is authenticated inside the consumer by accessing the user through the session key(session key is obtained by the Chrome inspect mode application)
So, I created a get user method inside the AsyncJsonWebsocket and passed the session key obtained through the chrome browser current tab
def get_user(self):
session_key = 'zdpagvtfetm7h4gsk996c5tl8ecmoc11'
session = Session.objects.get(session_key=session_key)
session_data = session.get_decoded()
uid = session_data.get('_auth_user_id')
user = User.objects.get(id=uid)
print('User obtained through session key',user)
return user
Screenshot of the session key obtained from chrome browser localhost tab
This JavaScript script at the bottom of your post - in which page is it being loaded? (What template is it in? What view is rendering it and sending it as a response?)
You posted this:
What was happening prior to this? (What requests were made before the login?)
Now let’s talk about this output the view and the template that was used are what I told you that I checked whether user is login or not from the view which called just before the consumer.
So this actually calls a view which fetch the passed group chats and establish the javascript connection with the consumer to talk real-time
In summary:
Javascript use chatroom.html whose url is localhost:8000/chat/str:grpname which loads after the user click on the group from home page and uses ChatRoom view available in Chat application inside the project.
In addition let me provide you the authentication code
So, actually all authentication code are in account app inside the project
Accounts/views.py
from django.shortcuts import render
from .forms import SignupForm
from django.views.generic.edit import CreateView
# Create your views here.
class SignUpView(CreateView):
form_class = SignupForm
success_url='/'
template_name = 'registration/signup.html'
urls.py
from django.urls import path,include
from django.contrib.auth.views import LoginView,LogoutView
from .forms import LoginForm
from .views import SignUpView
urlpatterns = [
path('login/',LoginView.as_view(template_name = 'registration/login.html',authentication_form=LoginForm),name='login'),
path('logout/',LogoutView.as_view(),name='logout'),
path('signup/',SignUpView.as_view(),name='signup')
]
forrms.py
from django import forms
from django.contrib.auth.forms import UserCreationForm,AuthenticationForm
class LoginForm(AuthenticationForm):
username = forms.CharField(widget=forms.TextInput(
attrs={'class': 'form-control', 'placeholder': 'Enter your username'}),label_suffix='')
password = forms.CharField(widget=forms.PasswordInput(
attrs={
'class': 'form-control','placeholder': 'Enter your password'}),label_suffix='')
class SignupForm(UserCreationForm):
username = forms.CharField(widget=forms.TextInput(
attrs={'class': 'form-control', 'placeholder': 'Enter your username'}),label_suffix='')
password1 = forms.CharField(widget=forms.PasswordInput(
attrs={
'class': 'form-control','placeholder': 'Enter your password'}),label_suffix='',label='Password')
password2 = forms.CharField(widget=forms.PasswordInput(
attrs={
'class': 'form-control','placeholder': 'Enter your password'}),label_suffix='',label='Confirm Password')
That helps explain a lot - first thing that jumps out at me is:
You’re showing that the user is entering localhost.
However, within your javascript, you have:
Which is actually not the same host domain from the perspective of the browser. Cookies being returned to you from localhost will not be shared with 127.0.0.1. (Yes, they may generally refer to the same IP address, but the browser doesn’t know that.)