Skip to content

Commit e8864b6

Browse files
committed
improve confirmation email link generation #38
1 parent 526fd6a commit e8864b6

File tree

12 files changed

+64
-54
lines changed

12 files changed

+64
-54
lines changed

rest_auth_toolkit/templates/rest_auth_toolkit/email_confirmation.html renamed to demo/demo/accounts/templates/rest_auth_toolkit/email_confirmation.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
<div>
1010

1111
<p>{% trans "Follow this link to validate your email:" %}<br>
12-
<a href="{{ confirmation_url }}">{{ confirmation_url }}</a></p>
12+
{% url "pages:confirm-email" token=confirmation.external_id as confirmation_url %}
13+
<a href="{{ base_url }}{{ confirmation_url }}">{{ base_url }}{{ confirmation_url }}</a></p>
1314

1415
</div>
1516
</body>

rest_auth_toolkit/templates/rest_auth_toolkit/email_confirmation.txt renamed to demo/demo/accounts/templates/rest_auth_toolkit/email_confirmation.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
{% load i18n %}
33

44
{% trans "Follow this link to validate your email:" %}
5-
{{ confirmation_url }}
5+
{{ base_url }}{% url "pages:confirm-email" token=confirmation.external_id %}
66

77
{% endautoescape %}

demo/demo/pages/auth_urls.py

Lines changed: 0 additions & 10 deletions
This file was deleted.

demo/demo/pages/templates/base.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{% load i18n %}
2+
{% get_current_language as LANGUAGE_CODE %}
3+
<!doctype html>
4+
<html lang="{{ LANGUAGE_CODE }}">
5+
<head>
6+
<meta charset="utf-8">
7+
<meta name="viewport" content="width=device-width, initial-scale=1">
8+
<title>{% block title %}{% endblock %}</title>
9+
</head>
10+
<body>
11+
{% block body %}{% endblock %}
12+
</body>
13+
</html>

demo/demo/pages/templates/error.html

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
1+
{% extends "base.html" %}
12
{% load i18n %}
2-
{% get_current_language as LANGUAGE_CODE %}
3-
<!doctype html>
4-
<html lang="{{ LANGUAGE_CODE }}">
5-
<head>
6-
<meta charset="utf-8">
7-
<meta name="viewport" content="width=device-width, initial-scale=1">
8-
<title>{{ site_name }}</title>
9-
</head>
10-
<body>
3+
{% block title %}Error! {{ site_name }}{% endblock %}
114

5+
{% block body %}
126
<div>
137
<h1>{% trans "Error!" %}</h1>
148
<p>{{ error }}</p>
159
</div>
16-
17-
</body>
18-
</html>
10+
{% endblock %}

demo/demo/pages/templates/index.html

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
1-
{% load i18n %}
2-
{% get_current_language as LANGUAGE_CODE %}
3-
<!doctype html>
4-
<html lang="{{ LANGUAGE_CODE }}">
5-
<head>
6-
<meta charset="utf-8">
7-
<meta name="viewport" content="width=device-width, initial-scale=1">
8-
<title>{{ site_name }}</title>
9-
</head>
10-
<body>
1+
{% extends "base.html" %}
2+
{% block title %}{{ site_name }}{% endblock %}
3+
4+
{% block body %}
115
<script>
126
window.fbAsyncInit = function() {
137
FB.init({appId: "{{ fb_app_id }}", xfbml: true, version: "v2.9"});
@@ -34,12 +28,11 @@
3428
<div id="fb-root"></div>
3529

3630
<div>
37-
<h1>Hello!</h1>
31+
<h1>Hello world!</h1>
3832
<button onclick="fb_login()">Login with Facebook</button>
3933
</div>
4034

4135
<script src="https://code.jquery.com/jquery-3.2.1.min.js"
4236
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
4337
crossorigin="anonymous"></script>
44-
</body>
45-
</html>
38+
{% endblock %}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{% extends "base.html" %}
2+
{% load i18n %}
3+
{% block title %}Success! {{ site_name }}{% endblock %}
4+
5+
{% block body %}
6+
<div>
7+
<h1>{% trans "Success!" %}</h1>
8+
<p>Your address {{ email }} is now confirmed.</p>
9+
</div>
10+
{% endblock %}

demo/demo/pages/urls.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@
88

99
urlpatterns = [
1010
url(r'^$', views.index, name='root'),
11+
url(r'^welcome/(?P<token>[^/.]+)$', views.confirm_email, name='confirm-email'),
1112
]

demo/demo/pages/views.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ def index(request):
1414
return render(request, 'index.html', context=ctx)
1515

1616

17-
def email_view(request, external_id):
17+
def confirm_email(request, token):
1818
"""Landing page for links in confirmation emails."""
1919
error = None
2020

2121
try:
22-
confirmation = EmailConfirmation.objects.get(external_id=external_id)
22+
confirmation = EmailConfirmation.objects.get(external_id=token)
2323
confirmation.confirm()
2424
except EmailConfirmation.DoesNotExist:
2525
error = _('Invalid link')
@@ -33,4 +33,8 @@ def email_view(request, external_id):
3333
}
3434
return render(request, 'error.html', context=ctx)
3535
else:
36-
return index(request)
36+
ctx = {
37+
'site_name': 'Demo',
38+
'email': confirmation.user.email,
39+
}
40+
return render(request, 'welcome.html', context=ctx)

demo/demo/settings.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@
102102

103103

104104
STATIC_URL = '/static/'
105-
# STATIC_ROOT = os.path.join(BASE_DIR, 'static')
106105

107106

108107
REST_FRAMEWORK = {
@@ -122,6 +121,5 @@
122121
REST_AUTH_TOOLKIT = {
123122
'email_confirmation_class': 'demo.accounts.models.EmailConfirmation',
124123
'email_confirmation_from': 'auth-demo@localhost',
125-
'email_confirmation_lookup_field': 'external_id',
126124
'api_token_class': 'demo.accounts.models.APIToken',
127125
}

demo/demo/urls.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,4 @@ class GoAwayRenderer(DocumentationRenderer):
3232
path('admin/', admin.site.urls),
3333
path('api/', include(api_urlpatterns)),
3434
path('', include('demo.pages.urls')),
35-
path('', include('demo.pages.auth_urls')),
3635
]

rest_auth_toolkit/views.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from django.contrib.auth import get_user_model
22
from django.core.mail import send_mail
33
from django.template.loader import render_to_string
4-
from django.urls import reverse
54
from django.utils.translation import gettext as _
65

76
from rest_framework import generics, status, views
@@ -26,11 +25,21 @@ class SignupView(generics.GenericAPIView):
2625
2726
If the setting email_confirmation_send_email is true (default),
2827
the function send_email will be called. That function requires
29-
that your app define a route named app-auth:email-confirmation
30-
with an id parameter; the view for this route should get an
31-
email confirmation instance using the ID and call the confirm
32-
method. To use a field that's not named 'id', define the setting
33-
email_confirmation_lookup_param (this will change the URL pattern).
28+
that your project defines defines two email templates:
29+
- rest_auth_toolkit/email_confirmation.txt
30+
- rest_auth_toolkit/email_confirmation.html
31+
32+
The templates will be passed the User and EmailConfirmation instances
33+
(as variables *user* and *confirmation*). To help generating links,
34+
a variable *base_url* with a value like "https://domain" (scheme,
35+
domain and optional port depending on the request, but no path), which
36+
lets you write code like `{{ base_url }}{% url "my-route" %}`.
37+
38+
It is up to your project to define what the link is. The demo app
39+
demonstrates a simple Django view that validates the email validation
40+
token in the URL; for a project with a front-end site (e.g. a JavaScript
41+
app) on a different domain than the Django API, a custom template tag
42+
could be used to generate the right URL for the front-end site.
3443
3544
If the setting is false, the user will be active immediately.
3645
"""
@@ -157,14 +166,14 @@ def send_email(request, user, address, confirmation):
157166
subject = _('Confirm your email address')
158167
from_address = get_setting('email_confirmation_from')
159168

160-
lookup_field = get_setting('email_confirmation_lookup_field', 'id')
161-
confirmation_url = request.build_absolute_uri(
162-
reverse('app-auth:email-confirmation',
163-
kwargs={lookup_field: getattr(confirmation, lookup_field)}))
164169
# The url template tag doesn't include scheme/domain/port, pass a helper
165170
base_url = request.build_absolute_uri('/')[:-1]
166171

167-
context = {'base_url': base_url, 'confirmation_url': confirmation_url}
172+
context = {
173+
'user': user,
174+
'confirmation': confirmation,
175+
'base_url': base_url,
176+
}
168177
txt_content = render_to_string('rest_auth_toolkit/email_confirmation.txt', context)
169178
html_content = render_to_string('rest_auth_toolkit/email_confirmation.html', context)
170179

0 commit comments

Comments
 (0)