Skip to content

Rework email confirmations #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 20 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{% load i18n demotags %}
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body style="background-color: #F7F8FA;">
<div>

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

<p>
{% trans "Or send an API request to simulate a front-end application:" %}<br>
<code>HTTP POST {{ base_url }}{% url "auth:confirm" %} email="{{ user.email }}" token="{{ confirmation.external_id }}"</code>
</p>

</div>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% autoescape off %}
{% load i18n demotags %}

{% trans "Follow this link to validate your email:" %}
{% frontend_base_url as base_url %}
{{ base_url }}{% url "pages:confirm-email" token=confirmation.external_id %}

{% trans "Or send an API request to simulate a front-end application:" %}
HTTP POST {{ base_url }}{% url "auth:confirm" %} email="{{ user.email }}" token="{{ confirmation.external_id }}"

{% endautoescape %}
Empty file.
10 changes: 10 additions & 0 deletions demo/demo/accounts/templatetags/demotags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django import template


register = template.Library()


@register.simple_tag(takes_context=True)
def frontend_base_url(context):
request = context['request']
return request.build_absolute_uri('/')[:-1]
10 changes: 0 additions & 10 deletions demo/demo/pages/auth_urls.py

This file was deleted.

13 changes: 13 additions & 0 deletions demo/demo/pages/templates/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% load i18n %}
{% get_current_language as LANGUAGE_CODE %}
<!doctype html>
<html lang="{{ LANGUAGE_CODE }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{% block title %}{% endblock %}</title>
</head>
<body>
{% block body %}{% endblock %}
</body>
</html>
16 changes: 4 additions & 12 deletions demo/demo/pages/templates/error.html
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
{% extends "base.html" %}
{% load i18n %}
{% get_current_language as LANGUAGE_CODE %}
<!doctype html>
<html lang="{{ LANGUAGE_CODE }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{{ site_name }}</title>
</head>
<body>
{% block title %}Error! {{ site_name }}{% endblock %}

{% block body %}
<div>
<h1>{% trans "Error!" %}</h1>
<p>{{ error }}</p>
</div>

</body>
</html>
{% endblock %}
19 changes: 6 additions & 13 deletions demo/demo/pages/templates/index.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
{% load i18n %}
{% get_current_language as LANGUAGE_CODE %}
<!doctype html>
<html lang="{{ LANGUAGE_CODE }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{{ site_name }}</title>
</head>
<body>
{% extends "base.html" %}
{% block title %}{{ site_name }}{% endblock %}

{% block body %}
<script>
window.fbAsyncInit = function() {
FB.init({appId: "{{ fb_app_id }}", xfbml: true, version: "v2.9"});
Expand All @@ -34,12 +28,11 @@
<div id="fb-root"></div>

<div>
<h1>Hello!</h1>
<h1>Hello world!</h1>
<button onclick="fb_login()">Login with Facebook</button>
</div>

<script src="https://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous"></script>
</body>
</html>
{% endblock %}
10 changes: 10 additions & 0 deletions demo/demo/pages/templates/welcome.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% extends "base.html" %}
{% load i18n %}
{% block title %}Success! {{ site_name }}{% endblock %}

{% block body %}
<div>
<h1>{% trans "Success!" %}</h1>
<p>Your address {{ email }} is now confirmed.</p>
</div>
{% endblock %}
1 change: 1 addition & 0 deletions demo/demo/pages/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@

urlpatterns = [
path('', views.index, name='root'),
path('welcome/<token>/', views.confirm_email, name='confirm-email'),
]
10 changes: 7 additions & 3 deletions demo/demo/pages/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ def index(request):
return render(request, 'index.html', context=ctx)


def email_view(request, external_id):
def confirm_email(request, token):
"""Landing page for links in confirmation emails."""
error = None

try:
confirmation = EmailConfirmation.objects.get(external_id=external_id)
confirmation = EmailConfirmation.objects.get(external_id=token)
confirmation.confirm()
except EmailConfirmation.DoesNotExist:
error = _('Invalid link')
Expand All @@ -33,4 +33,8 @@ def email_view(request, external_id):
}
return render(request, 'error.html', context=ctx)
else:
return index(request)
ctx = {
'site_name': 'Demo',
'email': confirmation.user.email,
}
return render(request, 'welcome.html', context=ctx)
1 change: 0 additions & 1 deletion demo/demo/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,5 @@
REST_AUTH_TOOLKIT = {
'email_confirmation_class': 'demo.accounts.models.EmailConfirmation',
'email_confirmation_from': 'auth-demo@localhost',
'email_confirmation_lookup_field': 'external_id',
'api_token_class': 'demo.accounts.models.APIToken',
}
10 changes: 8 additions & 2 deletions demo/demo/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
from rest_framework.documentation import include_docs_urls
from rest_framework.renderers import DocumentationRenderer

from rest_auth_toolkit.views import FacebookLoginView, LoginView, LogoutView, SignupView
from rest_auth_toolkit.views import (
EmailConfirmationView,
FacebookLoginView,
LoginView,
LogoutView,
SignupView,
)


class GoAwayRenderer(DocumentationRenderer):
Expand All @@ -16,6 +22,7 @@ class GoAwayRenderer(DocumentationRenderer):

auth_urlpatterns = [
path('signup/', SignupView.as_view(), name='signup'),
path('confirm/', EmailConfirmationView.as_view(), name='confirm'),
path('login/', LoginView.as_view(), name='login'),
path('logout/', LogoutView.as_view(), name='logout'),
path('fb-login/', FacebookLoginView.as_view(), name='fb-login'),
Expand All @@ -32,5 +39,4 @@ class GoAwayRenderer(DocumentationRenderer):
path('admin/', admin.site.urls),
path('api/', include(api_urlpatterns)),
path('', include('demo.pages.urls')),
path('', include('demo.pages.auth_urls')),
]
2 changes: 1 addition & 1 deletion rest_auth_toolkit/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class RestAuthToolkitConfig(AppConfig):
"""Default app config for RATK.

This installs a signal handler to set user.is_active when
email_confirmed is emitted.
email_confirmed is emitted by EmailConfirmationView.
"""
name = 'rest_auth_toolkit'

Expand Down
28 changes: 28 additions & 0 deletions rest_auth_toolkit/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from rest_framework import serializers
from rest_framework.exceptions import ValidationError

from .utils import get_object_from_setting

try:
import facepy
except ImportError:
Expand All @@ -17,6 +19,7 @@


User = get_user_model()
EmailConfirmation = get_object_from_setting('email_confirmation_class')


class SignupDeserializer(serializers.ModelSerializer):
Expand Down Expand Up @@ -54,6 +57,31 @@ def create(self, validated_data):
)


class EmailConfirmationDeserializer(serializers.Serializer):
email = serializers.EmailField()
token = serializers.CharField()

def validate(self, data):
msg = None

try:
confirmation = EmailConfirmation.objects.get(
external_id=data['token'],
user__email=data['email'],
)
confirmation.confirm()
except EmailConfirmation.DoesNotExist:
msg = _('Invalid link')
except EmailConfirmation.IsExpired:
# FIXME it's not possible to register with the same email
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

address this

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should check for expired confirmation, but it shouldn't be an error related to a duplicate email, tho? A user shouldn't be able to create an account with an email related to another email, tho ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that’s the problem with saving the User/Account before confirmation :(

After #19 and #30 this will be cleaner, but here I’m looking for something simpler but correct for the 0.10 version.

msg = _('Email expired, please register again')

if msg:
raise ValidationError({'errors': [msg]})

return {'user': confirmation.user}


class LoginDeserializer(serializers.Serializer):
"""Deserializer to find a user from credentials."""

Expand Down

This file was deleted.

This file was deleted.

Loading