PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, September 9, 2022

[FIXED] How to solve add key and value in cookies in the developer tools using django?

 September 09, 2022     ajax, django, javascript, jquery, python     No comments   

Issue

This is a Social network website. It's built-in python Django. I need to add the user login section to the values stored in the local storage section.

  • in this website have 2 login method one is end-user and another is companies

  • the main setting is needed user is login that time the key and value is needed to store the local storage

  • this is needed for cross-site login for users for example Facebook users have joined in through Instagram.

  • please help me with the solution? I need to fix the set cookies in the session also

I added the codes below section models.py

  # Custom user
    class CustomUser(AbstractUser):
        """ Custom user model"""
        email = models.EmailField(unique=True, validators=[EmailValidator])
        is_company = models.BooleanField(default=False)
        is_employee = models.BooleanField(default=False)
        is_enduser = models.BooleanField(default=False)
        is_staff = models.BooleanField(default=False)
        slug = AutoSlugField(populate_from='username')
        objects = CustomUserManager()
    
        def __str__(self):
            return f"{self.username}"
    
        @property
        def group_name(self):
            """
            Returns a group name based on the user's id to be used by Django Channels.
            Example usage:
            user = User.objects.get(pk=1)
            group_name = user.group_name
            """
            return "user_%s" % self.id

*urls.py

app_name = 'account' urlpatterns = [

path('login/company/', views.u_login, name='c_login'),
path('login/employee/', views.c_login, name='u_login'),

path('logout/', views.user_logout, name='logout'),
path('user-signup/', user_signup_view, name="user_signup"),
path('switch/<int:id>/', switch_user, name="user_switch"),
path('activate/<slug:uidb64>/<slug:token>/',views.activate_account, name='activate'),
path('change-pwd/<int:id>/', views.set_password, name='set_pwd'),

]

#employee copy login

 def c_login(request):
     print("runned")
    if request.method == 'POST':

        form = LoginForm(request.POST)
        if form.is_valid():

            cd = form.cleaned_data
            print("cd",cd)
             user = authenticate(request,
                                username=cd['username'],
                                 password=cd['password'],
                                 remember_me=cd['remember_me'])
             if user is not None:
                if user.is_active:
                     login(request, user)

                     return HttpResponseRedirect(reverse('feed:user_feed', args=[request.user.profile.slug]))
                 else:
                     return HttpResponse('Disabled account')
             else:
                 messages.error(request, 'Invalid username or password')
        return render(request, 'account/u_login.html', {'form': form})

     else:
         form = LoginForm
     return render(request, 'account/u_login.html', {'form': form})

Login session check through cookies and set encrypted token with (Unique ID, Name, E-mail) According to the given flow.

Set details in Cookies with Encrypted Token With (ID,Name,Email )


Solution

first install these packages and the settings include session also.

https://docs.djangoproject.com/en/3.2/topics/http/sessions/

install this package and session need to added also

pip install djangorestframework-simplejwt

SESSION_ENGINE = 'django.contrib.sessions.backends.cached_db'
SESSION_COOKIE_SECURE = False
ENCRYPTED_COOKIE_SERIALIZER = 'json'
CSRF_COOKIE_SECURE = True
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SAMESITE = 'None'
SESSION_COOKIE_SAMESITE = 'None'
COMPRESS_ENCRYPTED_COOKIE = True
ENCRYPTED_COOKIE_COMPRESSION_LEVEL = 1


Answered By - Bhavya Bibi
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing