Skip to content

Commit 8115963

Browse files
authored
Merge pull request #274 from RSE-Sheffield/feat/allauth
Use allauth for email address verification
2 parents 815ead3 + 6b4425c commit 8115963

File tree

9 files changed

+158
-2
lines changed

9 files changed

+158
-2
lines changed

SORT/settings.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ def cast_to_boolean(obj: Any) -> bool:
6161
"django.contrib.messages",
6262
"django.contrib.staticfiles",
6363
# Plugin apps
64+
"allauth",
65+
"allauth.account",
6466
"django_bootstrap5",
6567
"django_extensions",
6668
"qr_code",
@@ -86,6 +88,7 @@ def cast_to_boolean(obj: Any) -> bool:
8688
"django.contrib.auth.middleware.AuthenticationMiddleware",
8789
"django.contrib.messages.middleware.MessageMiddleware",
8890
"django.middleware.clickjacking.XFrameOptionsMiddleware",
91+
"allauth.account.middleware.AccountMiddleware",
8992
]
9093
if DEBUG:
9194
MIDDLEWARE.append("debug_toolbar.middleware.DebugToolbarMiddleware")
@@ -196,7 +199,10 @@ def cast_to_boolean(obj: Any) -> bool:
196199
EMAIL_USE_LOCALTIME = cast_to_boolean(os.getenv("DJANGO_EMAIL_USE_LOCALTIME", True))
197200
DEFAULT_FROM_EMAIL = os.getenv("DJANGO_DEFAULT_FROM_EMAIL", "[email protected]")
198201

199-
AUTHENTICATION_BACKENDS = ("django.contrib.auth.backends.ModelBackend",)
202+
AUTHENTICATION_BACKENDS = (
203+
"django.contrib.auth.backends.ModelBackend",
204+
"allauth.account.auth_backends.AuthenticationBackend",
205+
)
200206

201207
# For django-debug-toolbar
202208
INTERNAL_IPS = [
@@ -269,3 +275,10 @@ def cast_to_boolean(obj: Any) -> bool:
269275
INVITATIONS_SIGNUP_REDIRECT = "signup"
270276
INVITATIONS_CONFIRMATION_URL_NAME = "member_invite_accept"
271277
INVITATIONS_EMAIL_SUBJECT_PREFIX = "SORT"
278+
279+
# AllAuth authentication options
280+
# https://docs.allauth.org/en/latest/account/configuration.html
281+
ACCOUNT_SIGNUP_FIELDS = ["email*", "password1*", "password2*"]
282+
ACCOUNT_LOGIN_METHODS = {"email"}
283+
ACCOUNT_USER_MODEL_USERNAME_FIELD = None
284+
ACCOUNT_EMAIL_VERIFICATION="mandatory"

SORT/urls.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
path("admin/", admin.site.urls),
2424
path("", include("home.urls")),
2525
path("", include("survey.urls"), name="survey"),
26+
path("accounts/", include("allauth.urls")),
2627
]
2728

2829
if settings.DEBUG:

home/templates/home/login.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717
<button type="submit" class="btn btn-primary w-100">Login</button>
1818
</form>
1919
<div class="text-center mt-3 small-text" style="font-size: 0.7rem;">
20-
<div class="text-end mt-1 small-text">
20+
<div class="text-center mt-2 small-text" style="font-size: 0.7rem;">
2121
<a href="{% url 'password_reset' %}" class="small">Forgot Password?</a>
2222
</div>
23+
<div class="text-center mt-2 small-text" style="font-size: 0.7rem;">
24+
<a href="{% url 'account_signup' %}" class="small">Don't have an account? Sign up</a>
25+
</div>
2326
</div>
2427

2528
{% endblock %}

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,4 @@ factory-boy==3.3.3
3131
django-crispy-forms==2.4
3232
crispy-bootstrap5==2025.4
3333
django-invitations==2.1.0
34+
django-allauth==65.9.0
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{% extends "login_base.html" %}
2+
{% load static %}
3+
{% load django_bootstrap5 %}
4+
{% load i18n %}
5+
{% load account %}
6+
{% load allauth %}
7+
{% block head_title %}
8+
{% trans "Confirm Email Address" %}
9+
{% endblock head_title %}
10+
{% block content %}
11+
{% element h1 %}
12+
{% trans "Confirm Email Address" %}
13+
{% endelement %}
14+
{% if confirmation %}
15+
{% user_display confirmation.email_address.user as user_display %}
16+
{% if can_confirm %}
17+
{% element p %}
18+
{% blocktrans with confirmation.email_address.email as email %}Please confirm that
19+
<a href="mailto:{{ email }}">{{ email }}</a> is your email address.
20+
{% endblocktrans %}
21+
{% endelement %}
22+
{% url 'account_confirm_email' confirmation.key as action_url %}
23+
{% element form method="post" action=action_url %}
24+
{% slot actions %}
25+
{% csrf_token %}
26+
{{ redirect_field }}
27+
{% element button type="submit" %}
28+
📧
29+
{% trans 'Confirm' %}
30+
{% endelement %}
31+
{% endslot %}
32+
{% endelement %}
33+
{% else %}
34+
{% element p %}
35+
{% blocktrans %}Unable to confirm {{ email }} because it is already confirmed by a different account.
36+
{% endblocktrans %}
37+
{% endelement %}
38+
{% endif %}
39+
{% else %}
40+
{% url 'account_email' as email_url %}
41+
{% element p %}
42+
{% blocktrans %}This email confirmation link expired or is invalid. Please
43+
<a href="{{ email_url }}">issue a new email confirmation request</a>.{% endblocktrans %}
44+
{% endelement %}
45+
{% endif %}
46+
{% endblock content %}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{% extends "login_base.html" %}
2+
{% load static %}
3+
{% load django_bootstrap5 %}
4+
5+
{% block title %}Register{% endblock %}
6+
7+
{% block content %}
8+
<form method="POST">
9+
{% csrf_token %}
10+
{% for field in form %}
11+
<div class="mb-3">
12+
<label for="{{ field.id_for_label }}" class="form-label"
13+
style="text-align: right">{{ field.label }}</label>
14+
<input type="{{ field.field.widget.input_type }}"
15+
class="form-control"
16+
id="{{ field.id_for_label }}"
17+
name="{{ field.name }}"
18+
value="{{ field.value|default:'' }}"
19+
{% if field.field.required %} required {% endif %} />
20+
{% if field.errors %}
21+
<div class="text-center alert-danger mt-3 p-2" role="alert" style="font-size: 0.7rem">
22+
{{ field.errors|join:", " }}
23+
</div>
24+
{% endif %}
25+
</div>
26+
{% endfor %}
27+
28+
<div class="form-check mb-3">
29+
<input class="form-check-input" type="checkbox" id="is_manager" name="is_manager" required>
30+
<label class="form-check-label" for="is_manager">
31+
I confirm I am signing up to SORT as an account manager.
32+
</label>
33+
</div>
34+
35+
<button type="submit" class="btn btn-primary w-100">Register</button>
36+
</form>
37+
<div class="text-center mt-2 small-text" style="font-size: 0.7rem;">
38+
<a href="{% url 'login' %}">Already have an account? Login here</a>
39+
</div>
40+
{% endblock %}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{% extends "login_base.html" %}
2+
{% load i18n %}
3+
{% load allauth %}
4+
{% block title %}
5+
{% trans "Verify Your Email Address" %}
6+
{% endblock title %}
7+
{% block content %}
8+
{% element h1 %}
9+
{% trans "Verify Your Email Address" %}
10+
{% endelement %}
11+
{% element p %}
12+
{% blocktrans %}We have sent an email to you for verification. Follow the link provided to finalize the signup process. If you do not see the verification email in your main inbox, check your spam folder. Please contact us if you do not receive the verification email within a few minutes.{% endblocktrans %}
13+
{% endelement %}
14+
{% endblock content %}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{% extends "login_base.html" %}
2+
{% load i18n %}
3+
{% load allauth %}
4+
{% block title %}
5+
{% trans "Verify Your Email Address" %}
6+
{% endblock title %}
7+
{% block content %}
8+
{% element h1 %}
9+
{% trans "Verify Your Email Address" %}
10+
{% endelement %}
11+
{% url 'account_email' as email_url %}
12+
{% element p %}
13+
{% blocktrans %}This part of the site requires us to verify that
14+
you are who you claim to be. For this purpose, we require that you
15+
verify ownership of your email address. {% endblocktrans %}
16+
{% endelement %}
17+
{% element p %}
18+
{% blocktrans %}We have sent an email to you for
19+
verification. Please click on the link inside that email. If you do not see the verification email in your main inbox, check your spam folder. Otherwise
20+
contact us if you do not receive it within a few minutes.{% endblocktrans %}
21+
{% endelement %}
22+
{% element p %}
23+
{% blocktrans %}<strong>Note:</strong> you can still <a href="{{ email_url }}">change your email address</a>.{% endblocktrans %}
24+
{% endelement %}
25+
{% endblock content %}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{% load allauth %}
2+
{% comment %} djlint:off {% endcomment %}
3+
<{% if attrs.href %}a href="{{ attrs.href }}"{% else %}button{% endif %}
4+
{% if attrs.form %}form="{{ attrs.form }}"{% endif %}
5+
{% if attrs.id %}id="{{ attrs.id }}"{% endif %}
6+
{% if attrs.name %}name="{{ attrs.name }}"{% endif %}
7+
{% if attrs.value %}value="{{ attrs.value }}"{% endif %}
8+
{% if attrs.type %}type="{{ attrs.type }}"{% endif %}
9+
class="btn btn-primary"
10+
>
11+
{% slot %}
12+
{% endslot %}
13+
</{% if attrs.href %}a{% else %}button{% endif %}>

0 commit comments

Comments
 (0)