skip to Main Content

I want to make password hashing possible for a django project.

views.py for login.

def asset_login(request):
if request.method == 'POST':
    username = request.POST.get('user_id')
    password = request.POST.get('password')
    try:
        user = UserTable.objects.get(username=username, password=password)
        if user:
            if user.status == 'inactive':
                messages.error(request, 'Your account is inactive.')
                return redirect('asset_login')
            request.session['name'] = user.name
            request.session['role'] =user.role
            if user.role == 'admin':
                return redirect('admin_dashboard')
            elif user.role == 'ciso':
                return redirect('ciso_dashboard')
            elif user.role == 'fnhead':
                return redirect('fnhead_dashboard')
            elif user.role == 'systemadmin':
                return redirect('systemadmin_dashboard')
            elif user.role == 'assetowner':
                return redirect('assetowner_dashboard')
            else:
                messages.error(request, 'Unknown user position')
                return redirect('asset_login')  # Redirect to clear form and message
    except UserTable.DoesNotExist:
        messages.error(request, 'Invalid username or password')
        return redirect('asset_login')  # Redirect to clear form and message
return render(request, 'asset.html')

models.py for username and password

class UserTable(models.Model):
sl_num = models.CharField(max_length=100)
name = models.CharField(max_length=100)
phone_no = models.CharField(max_length=100)
email = models.EmailField(blank=False, null=False)
location = models.CharField(max_length=100)
department = models.CharField(max_length=100)
status = models.CharField(max_length=100)
role=models.CharField(max_length=100)
username=models.CharField(max_length=100)
password=models.CharField(max_length=100)

def __str__(self):
    return self.name

I want to make paasword hasing possible on django project , i am using custom authentication instead of django build in autentication.

2

Answers


  1. Use django’s AbstractBaseUser for hashing utilities.

    Implement a custom authentication backend to handle authentication with hashed password.

    Login or Signup to reply.
  2. referring to django documents you may use custom authentication by changing the AUTHENTICATION_BACKENDS in your settings file to writer your own custom authentication, for example

    from django.contrib.auth.backends import BaseBackend
    
    
    class MyBackend(BaseBackend):
    def authenticate(self, request, username=None, password=None):
        # Check the username/password and return a user.
        ...
    

    then setting the AUTHENTICATION_BACKENDS = ["app.backendfolder.MyBackend"].

    now when calling authenticate() it will use the custom method you wrote.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search