add remember me checkbox to login view and set session expire, #77

This commit is contained in:
simon 2021-12-16 15:17:58 +07:00
parent e858bba944
commit c9373eee15
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4
4 changed files with 18 additions and 5 deletions

View File

@ -16,6 +16,7 @@ class CustomAuthForm(AuthenticationForm):
password = forms.CharField( password = forms.CharField(
widget=PasswordInput(attrs={"placeholder": "Password"}), label=False widget=PasswordInput(attrs={"placeholder": "Password"}), label=False
) )
remember_me = forms.BooleanField(required=False)
class UserSettingsForm(forms.Form): class UserSettingsForm(forms.Form):

View File

@ -29,9 +29,9 @@
{% endif %} {% endif %}
<form action="/login/" method="POST" name="login"> <form action="/login/" method="POST" name="login">
{% csrf_token %} {% csrf_token %}
{% for field in form %} {{ form.username }}<br>
{{ field }}<br> {{ form.password }}<br>
{% endfor %} <p>Remember me: {{ form.remember_me }}</p>
<input type="hidden" name="next" value="{{ request.GET.next }}" /> <input type="hidden" name="next" value="{{ request.GET.next }}" />
<button type="submit">Login</button> <button type="submit">Login</button>
</form> </form>

View File

@ -173,6 +173,8 @@ class LoginView(View):
Greeting and login page Greeting and login page
""" """
SEC_IN_DAY = 60 * 60 * 24
@staticmethod @staticmethod
def get(request): def get(request):
"""handle get requests""" """handle get requests"""
@ -182,11 +184,17 @@ class LoginView(View):
context = {"colors": colors, "form": form, "form_error": failed} context = {"colors": colors, "form": form, "form_error": failed}
return render(request, "home/login.html", context) return render(request, "home/login.html", context)
@staticmethod def post(self, request):
def post(request):
"""handle login post request""" """handle login post request"""
form = AuthenticationForm(data=request.POST) form = AuthenticationForm(data=request.POST)
if form.is_valid(): if form.is_valid():
remember_me = request.POST.get("remember_me") or False
if remember_me == "on":
request.session.set_expiry(self.SEC_IN_DAY * 365)
else:
request.session.set_expiry(self.SEC_IN_DAY * 2)
print(f"expire session in {request.session.get_expiry_age()} secs")
next_url = request.POST.get("next") or "home" next_url = request.POST.get("next") or "home"
user = form.get_user() user = form.get_user()
login(request, user) login(request, user)

View File

@ -543,6 +543,10 @@ button:hover {
min-width: 200px; min-width: 200px;
} }
#id_remember_me {
min-width: unset;
}
.login-page button, .login-page button,
.login-page .danger-zone { .login-page .danger-zone {
width: 210px; width: 210px;