Search code examples
mysqldjangodatabaseauthenticationlegacy

How to imitate request.user with legacy database in Django?


I have to make a simple login functionality with Django working with legacy database where the password is not encrypted. So I am using Table.objects.filter(login=username,password=password).exists() to figure out if that particular username password combo exists in a table.

After logging in, I want to use something like request.user so that I can use that to access other information related to the user who just logged in to the system.

Is there any way that I can carry a variable across different pages in the system like request.user?


Solution

  • All you need is a custom authentication backend. With that, you can use your existing table, and have all the benefits of the standard Django user model.

    However I would urge you very strongly to try and move your user data to have properly hashed passwords. In your case, for example, you could run a data migration to copy the data to a new table and hash the passwords on the way.