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(
widget=PasswordInput(attrs={"placeholder": "Password"}), label=False
)
remember_me = forms.BooleanField(required=False)
class UserSettingsForm(forms.Form):

View File

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

View File

@ -173,6 +173,8 @@ class LoginView(View):
Greeting and login page
"""
SEC_IN_DAY = 60 * 60 * 24
@staticmethod
def get(request):
"""handle get requests"""
@ -182,11 +184,17 @@ class LoginView(View):
context = {"colors": colors, "form": form, "form_error": failed}
return render(request, "home/login.html", context)
@staticmethod
def post(request):
def post(self, request):
"""handle login post request"""
form = AuthenticationForm(data=request.POST)
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"
user = form.get_user()
login(request, user)

View File

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