Skip to content

Commit

Permalink
Use POST for logout requests
Browse files Browse the repository at this point in the history
Using the GET method for logging out was deprecated in Django 4.1 and
removed in 5.1. To retain styling, some modifications to the scss were
needed, and a new CSRF middleware added to avoid an "Invalid" error on
clicking the new buttons.

Fixes #2463
  • Loading branch information
tienne-B committed Sep 23, 2024
1 parent 50ba239 commit d6b79df
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 35 deletions.
1 change: 1 addition & 0 deletions tabbycat/settings/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
MIDDLEWARE = [
'django.middleware.gzip.GZipMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
# User language preferences; must be after Session
Expand Down
12 changes: 7 additions & 5 deletions tabbycat/templates/nav/admin_nav.html
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,13 @@
{% endfor %}

<div class="list-group-item d-inline-block">
<a href="{% url 'logout' %}" data-parent="#sidebar"
class="collapsed">
<i data-feather="log-out"></i>
<span class="d-none d-md-inline">{% trans "Log Out" %}</span>
</a>
<form id="logout-form" action="{% url 'logout' %}" data-parent="#sidebar" method="post" class="collapsed">
{% csrf_token %}
<button type="submit" class="btn btn-link">
<i data-feather="log-out"></i>
<span class="d-none d-md-inline">{% trans "Log Out" %}</span>
</button>
</form>
</div>

</div>
9 changes: 6 additions & 3 deletions tabbycat/templates/nav/top_nav_base.html
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,12 @@
<ul class="navbar-nav navbar-my-lg-0">
<li class="nav-item">
{% if user.is_authenticated %}
<a class="nav-link" href="{% url 'logout' %}">
{% trans "Log Out" %} ({{ user }})
</a>
<form id="logout-form" action="{% url 'logout' %}" method="post">
{% csrf_token %}
<button type="submit" class="btn btn-link nav-link">
{% trans "Log Out" %} ({{ user }})
</button>
</form>
{% else %}
<a class="nav-link" href="{% url 'login' %}">
{% trans "Login" %}
Expand Down
2 changes: 1 addition & 1 deletion tabbycat/templates/scss/modules/forms.scss
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
}

// Fix bad inheritance
.list-group-item .btn .feather {
.list-group-item .btn:not(.btn-link) .feather {
margin-right: 0;
}

Expand Down
55 changes: 32 additions & 23 deletions tabbycat/templates/scss/modules/nav.scss
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@
border-right: 0;
padding: 0;

a {
a,
form > button {
color: $sidebar-muted-text;
display: block;

Expand Down Expand Up @@ -223,16 +224,20 @@
// Applies just to tablets
@include media-breakpoint-up(md) {

.admin-sidebar .list-group-item a {
font-size: 12px;
padding: 0.5rem 0.5rem;
.admin-sidebar .list-group-item {

.feather {
width: 12px;
height: 12px;
padding-right: 2px;
margin-right: 0;
padding-bottom: 2px;
a,
form > button {
font-size: 12px;
padding: 0.5rem 0.5rem;

.feather {
width: 12px;
height: 12px;
padding-right: 2px;
margin-right: 0;
padding-bottom: 2px;
}
}
}

Expand All @@ -258,20 +263,24 @@
// Applies just to screens
@include media-breakpoint-up(lg) {

.admin-sidebar .list-group-item a {
font-size: $font-size-base;
padding: 0.5rem 1rem;

.feather {
width: 20px;
height: 16px;
padding-right: 4px;
}
.admin-sidebar .list-group-item {

.feather-chevron-down,
.feather-chevron-up {
margin-top: 2px;
margin-right: 0;
a,
form > button {
font-size: $font-size-base;
padding: 0.5rem 1rem;

.feather {
width: 20px;
height: 16px;
padding-right: 4px;
}

.feather-chevron-down,
.feather-chevron-up {
margin-top: 2px;
margin-right: 0;
}
}
}

Expand Down
21 changes: 18 additions & 3 deletions tabbycat/tournaments/templates/site_index.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,24 @@
{% url 'password_change' as url %}
{% include "components/item-action.html" with icon="rotate-cw" %}

{% blocktrans asvar text %}Log Out ({{ user }}){% endblocktrans %}
{% url 'logout' as url %}
{% include "components/item-action.html" with icon="log-out" %}
<form id="logout-link-form" method="post" action="{% url 'logout' %}" class="list-group-item list-group-item-action text-primary">
{% csrf_token %}
<button type="submit" class="btn btn-link p-0 list-group-item-action text-primary">
<div class="row align-items-center">
<div class="col-auto pr-1">
<i data-feather="log-out"></i>
</div>

<div class="col pl-0 pr-0">
{% blocktrans %}Log Out ({{ user }}){% endblocktrans %}
</div>

<div class="col-auto pr-1">
<i data-feather="chevron-right"></i>
</div>
</div>
</button>
</form>

{% else %}

Expand Down

0 comments on commit d6b79df

Please sign in to comment.