diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f209d01 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +#Excluding database + +*.sqlite3 + +#Excluding vscode + +.vscode \ No newline at end of file diff --git a/db.sqlite3 b/db.sqlite3 deleted file mode 100644 index e69de29..0000000 diff --git a/email_auth/__pycache__/forms.cpython-36.pyc b/email_auth/__pycache__/forms.cpython-36.pyc new file mode 100644 index 0000000..9028c9b Binary files /dev/null and b/email_auth/__pycache__/forms.cpython-36.pyc differ diff --git a/email_auth/__pycache__/models.cpython-36.pyc b/email_auth/__pycache__/models.cpython-36.pyc index 44f8f8f..1525a8f 100644 Binary files a/email_auth/__pycache__/models.cpython-36.pyc and b/email_auth/__pycache__/models.cpython-36.pyc differ diff --git a/email_auth/__pycache__/tokens.cpython-36.pyc b/email_auth/__pycache__/tokens.cpython-36.pyc new file mode 100644 index 0000000..4cb3020 Binary files /dev/null and b/email_auth/__pycache__/tokens.cpython-36.pyc differ diff --git a/email_auth/__pycache__/urls.cpython-36.pyc b/email_auth/__pycache__/urls.cpython-36.pyc index 25a2ca7..a0709df 100644 Binary files a/email_auth/__pycache__/urls.cpython-36.pyc and b/email_auth/__pycache__/urls.cpython-36.pyc differ diff --git a/email_auth/__pycache__/views.cpython-36.pyc b/email_auth/__pycache__/views.cpython-36.pyc index 50b417e..07106fb 100644 Binary files a/email_auth/__pycache__/views.cpython-36.pyc and b/email_auth/__pycache__/views.cpython-36.pyc differ diff --git a/email_auth/forms.py b/email_auth/forms.py new file mode 100644 index 0000000..ad458ad --- /dev/null +++ b/email_auth/forms.py @@ -0,0 +1,8 @@ +from django import forms +from django.contrib.auth.forms import UserCreationForm +from django.contrib.auth.models import User +class SignupForm(UserCreationForm): + email = forms.EmailField(max_length=200, help_text='Required') + class Meta: + model = User + fields = ('username', 'email', 'password1', 'password2') \ No newline at end of file diff --git a/email_auth/migrations/0001_initial.py b/email_auth/migrations/0001_initial.py new file mode 100644 index 0000000..3373a43 --- /dev/null +++ b/email_auth/migrations/0001_initial.py @@ -0,0 +1,24 @@ +# Generated by Django 2.1.7 on 2019-12-13 08:26 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.CreateModel( + name='MyAppUser', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ], + ), + ] diff --git a/email_auth/migrations/__pycache__/0001_initial.cpython-36.pyc b/email_auth/migrations/__pycache__/0001_initial.cpython-36.pyc new file mode 100644 index 0000000..b386f30 Binary files /dev/null and b/email_auth/migrations/__pycache__/0001_initial.cpython-36.pyc differ diff --git a/email_auth/models.py b/email_auth/models.py index 71a8362..b35273c 100644 --- a/email_auth/models.py +++ b/email_auth/models.py @@ -1,3 +1,7 @@ from django.db import models - +from django.contrib.auth.models import User # Create your models here. + +class MyAppUser (models.Model): + user = models.ForeignKey(User, on_delete=models.CASCADE) + #user = models.OneToOneField(User, on_delete=models.CASCADE) \ No newline at end of file diff --git a/email_auth/tokens.py b/email_auth/tokens.py new file mode 100644 index 0000000..1b6ed6e --- /dev/null +++ b/email_auth/tokens.py @@ -0,0 +1,14 @@ +from django.contrib.auth.tokens import PasswordResetTokenGenerator +#from django.utils import six +#from .models.MyAppUser import user #added here +import six +class TokenGenerator(PasswordResetTokenGenerator): + def _make_hash_value(self, user, timestamp): + return ( + six.text_type(user.pk) + six.text_type(timestamp) + + six.text_type(user.is_active) + ) + #return (six.text_type(user.pk) + six.text_type(timestamp) + six.text_type(user.username)) +account_activation_token = TokenGenerator() +print(account_activation_token) + diff --git a/email_auth/urls.py b/email_auth/urls.py index 461b78c..eac2249 100644 --- a/email_auth/urls.py +++ b/email_auth/urls.py @@ -1,7 +1,12 @@ from django.urls import path +from django.conf.urls import url from .views import * urlpatterns=[ path('',home,name='home'), - path('email',email,name='email1') + #path('email',email,name='email1'), + url('signup', signup, name='signup'), + #path(r'^activate/(?P[0-9A-Za-z_\-]+)/(?P[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',activate, name='activate') + path('activate/(?P\d+)/(?P\d+)/$',activate,name='activate') + ] \ No newline at end of file diff --git a/email_auth/views.py b/email_auth/views.py index 4b30bba..7854a8c 100644 --- a/email_auth/views.py +++ b/email_auth/views.py @@ -1,10 +1,71 @@ -from django.shortcuts import render +from django.http import HttpResponse +from django.shortcuts import render, redirect +from django.contrib.auth import login, authenticate +from .forms import SignupForm +from django.contrib.sites.shortcuts import get_current_site +from django.utils.encoding import force_bytes, force_text +from django.utils.http import urlsafe_base64_encode, urlsafe_base64_decode +from django.template.loader import render_to_string +from .tokens import account_activation_token +from django.contrib.auth.models import User +from django.core.mail import EmailMessage +from . import models +def signup(request): + if request.method == 'POST': + form = SignupForm(request.POST) + if form.is_valid(): + user = form.save(commit=False) + user.is_active = False + user.save() + current_site = get_current_site(request) + mail_subject = 'Activate your blog account.' + message = render_to_string('acc_active_email.html', { + 'user': user, + 'domain': current_site.domain, + 'uid':urlsafe_base64_encode(force_bytes(user.pk)), + 'token':account_activation_token.make_token(user) + }) + to_email = form.cleaned_data.get('email') + email = EmailMessage( + mail_subject, message, to=[to_email] + ) + email.send() + return HttpResponse('Please confirm your email address to complete the registration') + else: + form = SignupForm() + return render(request, 'signup.html', {'form': form}) + +def activate(request, uidb64, token): + #print(user)#added here + try: + uid = force_text(urlsafe_base64_decode(uidb64)) + print(uid) + user = User.objects.get(pk=uid) + #user=models.MyAppUser.objects.get(pk=uid) + #print(user)#added here + #except (TypeError, ValueError, OverflowError, User.DoesNotExist): + except(ValueError): + #print(user) + user = None + print(user)#added here + if user is not None and account_activation_token.check_token(user, token): + user.is_active = True + user.save() + #login(request, user) + # return redirect('home') + return HttpResponse('Thank you for your email confirmation. Now you can login your account.') + else: + return HttpResponse('Activation link is invalid!') + + + + # Create your views here. def home(request): return render(request,'home.html') -def email(request): - email=request.POST['email'] - print(email) \ No newline at end of file +#def email(request): + #email=request.POST['email'] + #print(email) \ No newline at end of file diff --git a/email_authentication/__pycache__/settings.cpython-36.pyc b/email_authentication/__pycache__/settings.cpython-36.pyc index 24ab9b2..ac395d2 100644 Binary files a/email_authentication/__pycache__/settings.cpython-36.pyc and b/email_authentication/__pycache__/settings.cpython-36.pyc differ diff --git a/email_authentication/settings.py b/email_authentication/settings.py index 9b23dd6..edb34f3 100644 --- a/email_authentication/settings.py +++ b/email_authentication/settings.py @@ -63,13 +63,20 @@ 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', - ], + ],`` }, }, ] WSGI_APPLICATION = 'email_authentication.wsgi.application' +#EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' +EMAIL_USE_TLS = True +EMAIL_HOST = 'smtp.gmail.com' +EMAIL_HOST_USER = 'email@example.com' +EMAIL_HOST_PASSWORD = 'password' +EMAIL_PORT = 587 + # Database # https://docs.djangoproject.com/en/2.1/ref/settings/#databases @@ -81,6 +88,9 @@ } } +#AUTHENTICATION_BACKENDS = ( + # ('django.contrib.auth.backends.ModelBackend'), +#) # Password validation # https://docs.djangoproject.com/en/2.1/ref/settings/#auth-password-validators diff --git a/templates/acc_active_email.html b/templates/acc_active_email.html new file mode 100644 index 0000000..fa5f16d --- /dev/null +++ b/templates/acc_active_email.html @@ -0,0 +1,5 @@ +{% autoescape off %} +Hi {{ user.username }}, +Please click on the link to confirm your registration, +http://{{ domain }}{% url 'activate' uidb64=uid token=token %} +{% endautoescape %} \ No newline at end of file diff --git a/templates/home.html b/templates/home.html index 4c0d48b..79fdfed 100644 --- a/templates/home.html +++ b/templates/home.html @@ -3,7 +3,7 @@ {% block content %}

Enter your details

-
+ +Sign Up +Sign In {% endblock %} \ No newline at end of file diff --git a/templates/signup.html b/templates/signup.html new file mode 100644 index 0000000..043699a --- /dev/null +++ b/templates/signup.html @@ -0,0 +1,21 @@ +{% extends 'base.html' %} + +{% block content %} +

Sign up

+
+ {% csrf_token %} + {% for field in form %} +

+ {{ field.label_tag }}
+ {{ field }} + {% if field.help_text %} + {{ field.help_text }} + {% endif %} + {% for error in field.errors %} +

{{ error }}

+ {% endfor %} +

+ {% endfor %} + +
+{% endblock %} \ No newline at end of file