0

I'm currently working on a Django app in which I'm trying to make a login page. I have used a custom db to store the input into. I have successfully been able to make a signup page, but for some reason I cant authenticate the user every time I try to login. the authenticate function returns None as a return every time and I don't know where I have made a mistake.

There are many similar topics like this and I have searched most of them. they didn't seem to solve my problem. Here's my code:

views.py


def signin(request):
    if request.method == "POST":
        username = request.POST["username"]
        pass1 = request.POST["pass1"]
        
        token = ld.login_user(username, pass1)# This is a custom login function which i made for my db. This returns a token which i havent made a use of yet
        if User.is_active==False:
            messages.error(request, "User Inactive!")
            return redirect("home")
 
        user = authenticate(request,username=username)
        if user is not None :
            login(request,user) 
            fname = user.first_name
            return render(request, "authentication/index.html", {"fname": fname})
        else:
            messages.error(request, "Bad credentials!")
            return redirect("home")
        

    return render(request, "authentication/signin.html")

models.py

I don't know if this is a necessary piece of code. I just put it in in case if its necessary

@staticmethod
    def login_user(username, password):
        """Authenticate a user based on their username and password."""

        conn = sqlite3.connect(filename)
        cursor = conn.cursor()

        # Retrieve the hashed password and token for the given username
        cursor.execute('SELECT password FROM {} WHERE username = ?'.format(Table_name), (username,))
        result = cursor.fetchone()

        if result:
            hashed_password = result[0]
            if hashed_password == login_db.hash_password(password):
                token = login_db.generate_token()     
                # Add the 'token' column to the 'users' table if it doesn't exist
                cursor.execute("PRAGMA table_info(users)")
                columns = cursor.fetchall()
                column_names = [column[1] for column in columns]
                if 'token' not in column_names:

                    cursor.execute("ALTER TABLE {} ADD COLUMN token TEXT".format(Table_name))

                # Update the user's token in the database
                cursor.execute('UPDATE {} SET token = ? WHERE username = ?'.format(Table_name), (token, username))
                conn.commit()
                conn.close()
                return token

        conn.close()
        return None

Every time I debug the code and check the user, it always returns None no matter what i do to the user model I have. I'm using Django's user model but I'm not using create_user() that Django provides because I found that it isn't necessary to make a user from Django's own function. I just need to have the data available for use.

I've been struggling on for hours now. Any help is appreciated and if any more snippets of my code is needed, feel free to ask. I will provide them. Thanks in advance!!

  • Does a user with that name exist in the database? – mousetail Jun 08 '23 at 07:16
  • yes it exists. I made a user just to check that single function. the thing is that when i user `user = User.objects.get(username=username)` to get the user, it returns a user, but the auth function returns None – Shreeyash Shrestha Jun 08 '23 at 07:31

0 Answers0