
Django Authentication with Custom Login-Registration-ForgetPassword-AutoLogout
Published Date: Aug. 29, 2024
Mastering Django Authentication with Custom Login, Registration, Forget Password, and Auto-Logout. In the realm of web development, Django stands tall as a robust framework for building powerful and secure applications. However, harnessing its full potential requires a deep understanding of its authentication system. This comprehensive guide delves into crafting a bespoke authentication experience, encompassing custom login mechanisms, seamless registration flows, foolproof forget password functionalities, and automated logout features. By mastering these intricacies, developers can fortify their Django projects with enhanced security and user experience, paving the path for scalable and reliable web applications.
A. Registratoin System:
To register in django system, first we need to render a regestration page. Below is the view.py
def admin_login(request):
return render(request, 'commonfile/admin/admin_login_page.html')
Below is the registration view function.
def register_user(request): # connect this function to signup button
first_name = request.POST.get('fname')
last_name = request.POST.get('lname')
email = request.POST.get('email')
password = request.POST.get('password')
# name,domain = email.split('@'). it is used to split name from email address.
pattern = r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}"
if len(email) == 0 or len(password) == 0 or len(first_name) == 0 or len(last_name) == 0:
messages.error("Empty Field not accepted")
elif Admin_login.objects.filter(email=email, act_status=1).exists():
messages.error("Email already exists. Choose a different one.")
else:
if len(email)<4 or len(password)<4:
messages.error("Minimum length of email and password is 4")
elif not re.match(pattern, email):
messages.error("Invalid email")
else:
# encrypt the password
encrypted_data = make_password(password) # encrypt the password by hashers.
user_obj = Admin_login()
act_code, link = email_generate(last_name) # Email generator function is below.
Admin_login.objects.create(first_name=first_name, last_name=last_name, email=email, password=encrypted_data,act_code=act_code, act_status = 0)
email_sub = 'Confirmation of Admin Registration for PythonShikhi Blog'
send_mail(email_sub ,link,'pythonshikhi@gmail.com',[email],html_message=link)
messages.success("Success")
There is function called email_generator() which alreay used inside of register_user() function. This email_generator will generate an email.
def email_generate(name):
current_time = datetime.now().strftime("%H:%M:%S")
h, m, s = map(int, current_time.split(':'))
t_s = h*3600 + m*60 + s
t_s = str(t_s)
random_number = random.choices('123456790',k=4)
random_number = ''.join(random_number)
v_c = t_s + random_number
signer = Signer()
encrypted_value = signer.sign(v_c)
encrypted_value1 = signer.sign(v_c).split(":")[1]
# decrypted_value = signer.unsign(encrypted_value)
link = f"<p>Dear {name}, <br> I am pleased to inform you that your registration as an administrator of the PythonShikhi Blog has been successful. To finalize the registration process and activate your account, kindly click on the activation link provided below: </p><a href='http://127.0.0.1:8000/admin/email_verification/"+encrypted_value1+"' target='_blank' [Activation Link]</a> <br> <br> Thank you for joining our team as an administrator. We look forward to your valuable contributions to the PythonShikhi Blog." formatted_link = format_html(link)
return encrypted_value1,formatted_link