Changed to email instead of username, added first and last name.

This commit is contained in:
kououken 2019-02-05 15:38:34 -08:00
parent 72ab00dd50
commit 6e8478cf67
5 changed files with 60 additions and 2 deletions

Binary file not shown.

View file

@ -45,6 +45,7 @@ INSTALLED_APPS = [
'rest_framework.authtoken', 'rest_framework.authtoken',
'allauth', 'allauth',
'allauth.account', 'allauth.account',
'allauth.socialaccount',
'rest_auth', 'rest_auth',
'rest_auth.registration', 'rest_auth.registration',
'corsheaders', 'corsheaders',
@ -159,3 +160,14 @@ STATIC_URL = '/static/'
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
SITE_ID = 1 SITE_ID = 1
# Registration
ACCOUNT_USER_MODEL_USERNAME_FIELD = 'username'
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_AUTHENTICATION_METHOD = 'email'
REST_AUTH_REGISTER_SERIALIZERS = {
'REGISTER_SERIALIZER': 'users.serializers.RegisterSerializer',
}

View file

@ -14,5 +14,6 @@ urlpatterns = [
path('api/v1/', include("backend.urls")), path('api/v1/', include("backend.urls")),
path('api/v1/account/', include('rest_auth.urls')), path('api/v1/account/', include('rest_auth.urls')),
path('api/v1/account/register/', include('rest_auth.registration.urls')), path('api/v1/account/register/', include('rest_auth.registration.urls')),
# path('api/v1/account/register/', NameRegistrationView.as_view()),
path('api-auth/', include('rest_framework.urls')), path('api-auth/', include('rest_framework.urls')),
] ]

47
back/users/serializers.py Normal file
View file

@ -0,0 +1,47 @@
from rest_framework import serializers
from allauth.account import app_settings as allauth_settings
from allauth.utils import email_address_exists
from allauth.account.adapter import get_adapter
from allauth.account.utils import setup_user_email
from django.utils.translation import gettext as _
class RegisterSerializer(serializers.Serializer):
email = serializers.EmailField(required=allauth_settings.EMAIL_REQUIRED)
first_name = serializers.CharField(required=True, write_only=True)
last_name = serializers.CharField(required=True, write_only=True)
password1 = serializers.CharField(required=True, write_only=True)
password2 = serializers.CharField(required=True, write_only=True)
def validate_email(self, email):
email = get_adapter().clean_email(email)
if allauth_settings.UNIQUE_EMAIL:
if email and email_address_exists(email):
raise serializers.ValidationError(
_("A user is already registered with this e-mail address."))
return email
def validate_password1(self, password):
return get_adapter().clean_password(password)
def validate(self, data):
if data['password1'] != data['password2']:
raise serializers.ValidationError(
_("The two password fields didn't match."))
return data
def get_cleaned_data(self):
return {
'first_name': self.validated_data.get('first_name', ''),
'last_name': self.validated_data.get('last_name', ''),
'password1': self.validated_data.get('password1', ''),
'email': self.validated_data.get('email', ''),
}
def save(self, request):
adapter = get_adapter()
user = adapter.new_user(request)
self.cleaned_data = self.get_cleaned_data()
adapter.save_user(request, user, self)
setup_user_email(request, user, [])
user.save()
return user

View file

@ -1,3 +1 @@
from django.shortcuts import render from django.shortcuts import render
# Create your views here.