├── README.md ├── db.sqlite3 ├── manage.py ├── otp_app ├── __init__.py ├── __pycache__ │ ├── __init__.cpython-39.pyc │ ├── admin.cpython-39.pyc │ ├── apps.cpython-39.pyc │ ├── forms.cpython-39.pyc │ ├── models.cpython-39.pyc │ ├── signals.cpython-39.pyc │ ├── urls.cpython-39.pyc │ └── views.cpython-39.pyc ├── admin.py ├── apps.py ├── forms.py ├── migrations │ ├── 0001_initial.py │ ├── 0002_otptoken.py │ ├── 0003_alter_otptoken_otp_code.py │ ├── 0004_alter_otptoken_otp_code_and_more.py │ ├── 0005_alter_otptoken_otp_code.py │ ├── __init__.py │ └── __pycache__ │ │ ├── 0001_initial.cpython-39.pyc │ │ ├── 0002_otptoken.cpython-39.pyc │ │ ├── 0003_alter_otptoken_otp_code.cpython-39.pyc │ │ ├── 0004_alter_otptoken_otp_code_and_more.cpython-39.pyc │ │ ├── 0005_alter_otptoken_otp_code.cpython-39.pyc │ │ └── __init__.cpython-39.pyc ├── models.py ├── signals.py ├── templates │ ├── base.html │ ├── index.html │ ├── login.html │ ├── resend_otp.html │ ├── signup.html │ └── verify_token.html ├── tests.py ├── urls.py └── views.py └── otp_validation ├── __init__.py ├── __pycache__ ├── __init__.cpython-39.pyc ├── settings.cpython-39.pyc ├── urls.cpython-39.pyc └── wsgi.cpython-39.pyc ├── asgi.py ├── settings.py ├── urls.py └── wsgi.py /README.md: -------------------------------------------------------------------------------- 1 | # email_verification_django 2 | This is a course on verifying users's email on signup using an OTP, that expires at every 5 minutes. 3 | -------------------------------------------------------------------------------- /db.sqlite3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeWithClinton/email_verification_django/ac724fe15675eb83e6ba69fa44a37a9de3da407e/db.sqlite3 -------------------------------------------------------------------------------- /manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Django's command-line utility for administrative tasks.""" 3 | import os 4 | import sys 5 | 6 | 7 | def main(): 8 | """Run administrative tasks.""" 9 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'otp_validation.settings') 10 | try: 11 | from django.core.management import execute_from_command_line 12 | except ImportError as exc: 13 | raise ImportError( 14 | "Couldn't import Django. Are you sure it's installed and " 15 | "available on your PYTHONPATH environment variable? Did you " 16 | "forget to activate a virtual environment?" 17 | ) from exc 18 | execute_from_command_line(sys.argv) 19 | 20 | 21 | if __name__ == '__main__': 22 | main() 23 | -------------------------------------------------------------------------------- /otp_app/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeWithClinton/email_verification_django/ac724fe15675eb83e6ba69fa44a37a9de3da407e/otp_app/__init__.py -------------------------------------------------------------------------------- /otp_app/__pycache__/__init__.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeWithClinton/email_verification_django/ac724fe15675eb83e6ba69fa44a37a9de3da407e/otp_app/__pycache__/__init__.cpython-39.pyc -------------------------------------------------------------------------------- /otp_app/__pycache__/admin.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeWithClinton/email_verification_django/ac724fe15675eb83e6ba69fa44a37a9de3da407e/otp_app/__pycache__/admin.cpython-39.pyc -------------------------------------------------------------------------------- /otp_app/__pycache__/apps.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeWithClinton/email_verification_django/ac724fe15675eb83e6ba69fa44a37a9de3da407e/otp_app/__pycache__/apps.cpython-39.pyc -------------------------------------------------------------------------------- /otp_app/__pycache__/forms.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeWithClinton/email_verification_django/ac724fe15675eb83e6ba69fa44a37a9de3da407e/otp_app/__pycache__/forms.cpython-39.pyc -------------------------------------------------------------------------------- /otp_app/__pycache__/models.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeWithClinton/email_verification_django/ac724fe15675eb83e6ba69fa44a37a9de3da407e/otp_app/__pycache__/models.cpython-39.pyc -------------------------------------------------------------------------------- /otp_app/__pycache__/signals.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeWithClinton/email_verification_django/ac724fe15675eb83e6ba69fa44a37a9de3da407e/otp_app/__pycache__/signals.cpython-39.pyc -------------------------------------------------------------------------------- /otp_app/__pycache__/urls.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeWithClinton/email_verification_django/ac724fe15675eb83e6ba69fa44a37a9de3da407e/otp_app/__pycache__/urls.cpython-39.pyc -------------------------------------------------------------------------------- /otp_app/__pycache__/views.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeWithClinton/email_verification_django/ac724fe15675eb83e6ba69fa44a37a9de3da407e/otp_app/__pycache__/views.cpython-39.pyc -------------------------------------------------------------------------------- /otp_app/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | from .models import CustomUser, OtpToken 3 | from django.contrib.auth.admin import UserAdmin 4 | # Register your models here. 5 | 6 | class CustomUserAdmin(UserAdmin): 7 | add_fieldsets = ( 8 | (None, { 9 | 'classes': ('wide',), 10 | 'fields': ('username', 'email', 'password1', 'password2')} 11 | ), 12 | ) 13 | 14 | 15 | class OtpTokenAdmin(admin.ModelAdmin): 16 | list_display = ("user", "otp_code") 17 | 18 | 19 | admin.site.register(OtpToken, OtpTokenAdmin) 20 | admin.site.register(CustomUser, CustomUserAdmin) 21 | 22 | -------------------------------------------------------------------------------- /otp_app/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class OtpAppConfig(AppConfig): 5 | default_auto_field = 'django.db.models.BigAutoField' 6 | name = 'otp_app' 7 | 8 | def ready(self): 9 | import otp_app.signals 10 | -------------------------------------------------------------------------------- /otp_app/forms.py: -------------------------------------------------------------------------------- 1 | from django import forms 2 | from django.contrib.auth.forms import UserCreationForm 3 | from django.contrib.auth import get_user_model 4 | 5 | 6 | class RegisterForm(UserCreationForm): 7 | email=forms.CharField(widget=forms.EmailInput(attrs={"placeholder": "Enter email-address", "class": "form-control"})) 8 | username=forms.CharField(widget=forms.TextInput(attrs={"placeholder": "Enter email-username", "class": "form-control"})) 9 | password1=forms.CharField(label="Password", widget=forms.PasswordInput(attrs={"placeholder": "Enter password", "class": "form-control"})) 10 | password2=forms.CharField(label="Confirm Password", widget=forms.PasswordInput(attrs={"placeholder": "Confirm password", "class": "form-control"})) 11 | 12 | class Meta: 13 | model = get_user_model() 14 | fields = ["email", "username", "password1", "password2"] -------------------------------------------------------------------------------- /otp_app/migrations/0001_initial.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.0.2 on 2023-12-30 12:44 2 | 3 | import django.contrib.auth.models 4 | import django.contrib.auth.validators 5 | from django.db import migrations, models 6 | import django.utils.timezone 7 | 8 | 9 | class Migration(migrations.Migration): 10 | 11 | initial = True 12 | 13 | dependencies = [ 14 | ('auth', '0012_alter_user_first_name_max_length'), 15 | ] 16 | 17 | operations = [ 18 | migrations.CreateModel( 19 | name='CustomUser', 20 | fields=[ 21 | ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 22 | ('password', models.CharField(max_length=128, verbose_name='password')), 23 | ('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')), 24 | ('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')), 25 | ('username', models.CharField(error_messages={'unique': 'A user with that username already exists.'}, help_text='Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.', max_length=150, unique=True, validators=[django.contrib.auth.validators.UnicodeUsernameValidator()], verbose_name='username')), 26 | ('first_name', models.CharField(blank=True, max_length=150, verbose_name='first name')), 27 | ('last_name', models.CharField(blank=True, max_length=150, verbose_name='last name')), 28 | ('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')), 29 | ('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')), 30 | ('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')), 31 | ('email', models.EmailField(max_length=254, unique=True)), 32 | ('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.Group', verbose_name='groups')), 33 | ('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.Permission', verbose_name='user permissions')), 34 | ], 35 | options={ 36 | 'verbose_name': 'user', 37 | 'verbose_name_plural': 'users', 38 | 'abstract': False, 39 | }, 40 | managers=[ 41 | ('objects', django.contrib.auth.models.UserManager()), 42 | ], 43 | ), 44 | ] 45 | -------------------------------------------------------------------------------- /otp_app/migrations/0002_otptoken.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.0.2 on 2023-12-30 15:25 2 | 3 | from django.conf import settings 4 | from django.db import migrations, models 5 | import django.db.models.deletion 6 | 7 | 8 | class Migration(migrations.Migration): 9 | 10 | dependencies = [ 11 | ('otp_app', '0001_initial'), 12 | ] 13 | 14 | operations = [ 15 | migrations.CreateModel( 16 | name='OtpToken', 17 | fields=[ 18 | ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 19 | ('otp_code', models.CharField(default='fad512', max_length=6, unique=True)), 20 | ('tp_created_at', models.DateTimeField(auto_now_add=True)), 21 | ('otp_expires_at', models.DateTimeField()), 22 | ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='otps', to=settings.AUTH_USER_MODEL)), 23 | ], 24 | ), 25 | ] 26 | -------------------------------------------------------------------------------- /otp_app/migrations/0003_alter_otptoken_otp_code.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.0.2 on 2023-12-30 15:27 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('otp_app', '0002_otptoken'), 10 | ] 11 | 12 | operations = [ 13 | migrations.AlterField( 14 | model_name='otptoken', 15 | name='otp_code', 16 | field=models.CharField(default='d2449f', max_length=6, unique=True), 17 | ), 18 | ] 19 | -------------------------------------------------------------------------------- /otp_app/migrations/0004_alter_otptoken_otp_code_and_more.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.0.2 on 2023-12-30 15:29 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('otp_app', '0003_alter_otptoken_otp_code'), 10 | ] 11 | 12 | operations = [ 13 | migrations.AlterField( 14 | model_name='otptoken', 15 | name='otp_code', 16 | field=models.CharField(default='7cefa7', max_length=6, unique=True), 17 | ), 18 | migrations.AlterField( 19 | model_name='otptoken', 20 | name='otp_expires_at', 21 | field=models.DateTimeField(blank=True, null=True), 22 | ), 23 | ] 24 | -------------------------------------------------------------------------------- /otp_app/migrations/0005_alter_otptoken_otp_code.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.0.2 on 2023-12-30 16:15 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('otp_app', '0004_alter_otptoken_otp_code_and_more'), 10 | ] 11 | 12 | operations = [ 13 | migrations.AlterField( 14 | model_name='otptoken', 15 | name='otp_code', 16 | field=models.CharField(default='a9cc9a', max_length=6), 17 | ), 18 | ] 19 | -------------------------------------------------------------------------------- /otp_app/migrations/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeWithClinton/email_verification_django/ac724fe15675eb83e6ba69fa44a37a9de3da407e/otp_app/migrations/__init__.py -------------------------------------------------------------------------------- /otp_app/migrations/__pycache__/0001_initial.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeWithClinton/email_verification_django/ac724fe15675eb83e6ba69fa44a37a9de3da407e/otp_app/migrations/__pycache__/0001_initial.cpython-39.pyc -------------------------------------------------------------------------------- /otp_app/migrations/__pycache__/0002_otptoken.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeWithClinton/email_verification_django/ac724fe15675eb83e6ba69fa44a37a9de3da407e/otp_app/migrations/__pycache__/0002_otptoken.cpython-39.pyc -------------------------------------------------------------------------------- /otp_app/migrations/__pycache__/0003_alter_otptoken_otp_code.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeWithClinton/email_verification_django/ac724fe15675eb83e6ba69fa44a37a9de3da407e/otp_app/migrations/__pycache__/0003_alter_otptoken_otp_code.cpython-39.pyc -------------------------------------------------------------------------------- /otp_app/migrations/__pycache__/0004_alter_otptoken_otp_code_and_more.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeWithClinton/email_verification_django/ac724fe15675eb83e6ba69fa44a37a9de3da407e/otp_app/migrations/__pycache__/0004_alter_otptoken_otp_code_and_more.cpython-39.pyc -------------------------------------------------------------------------------- /otp_app/migrations/__pycache__/0005_alter_otptoken_otp_code.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeWithClinton/email_verification_django/ac724fe15675eb83e6ba69fa44a37a9de3da407e/otp_app/migrations/__pycache__/0005_alter_otptoken_otp_code.cpython-39.pyc -------------------------------------------------------------------------------- /otp_app/migrations/__pycache__/__init__.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeWithClinton/email_verification_django/ac724fe15675eb83e6ba69fa44a37a9de3da407e/otp_app/migrations/__pycache__/__init__.cpython-39.pyc -------------------------------------------------------------------------------- /otp_app/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | from django.contrib.auth.models import AbstractUser 3 | from django.contrib.auth import get_user_model 4 | from django.conf import settings 5 | import secrets 6 | # Create your models here. 7 | 8 | class CustomUser(AbstractUser): 9 | email = models.EmailField(unique=True) 10 | 11 | USERNAME_FIELD = ("email") 12 | REQUIRED_FIELDS = ["username"] 13 | 14 | def _str__(self): 15 | return self.email 16 | 17 | 18 | class OtpToken(models.Model): 19 | user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="otps") 20 | otp_code = models.CharField(max_length=6, default=secrets.token_hex(3)) 21 | tp_created_at = models.DateTimeField(auto_now_add=True) 22 | otp_expires_at = models.DateTimeField(blank=True, null=True) 23 | 24 | 25 | def __str__(self): 26 | return self.user.username 27 | -------------------------------------------------------------------------------- /otp_app/signals.py: -------------------------------------------------------------------------------- 1 | from django.db.models.signals import post_save 2 | from django.conf import settings 3 | from django.dispatch import receiver 4 | from .models import OtpToken 5 | from django.core.mail import send_mail 6 | from django.utils import timezone 7 | 8 | 9 | 10 | @receiver(post_save, sender=settings.AUTH_USER_MODEL) 11 | def create_token(sender, instance, created, **kwargs): 12 | if created: 13 | if instance.is_superuser: 14 | pass 15 | 16 | else: 17 | OtpToken.objects.create(user=instance, otp_expires_at=timezone.now() + timezone.timedelta(minutes=5)) 18 | instance.is_active=False 19 | instance.save() 20 | 21 | 22 | # email credentials 23 | otp = OtpToken.objects.filter(user=instance).last() 24 | 25 | 26 | subject="Email Verification" 27 | message = f""" 28 | Hi {instance.username}, here is your OTP {otp.otp_code} 29 | it expires in 5 minute, use the url below to redirect back to the website 30 | http://127.0.0.1:8000/verify-email/{instance.username} 31 | 32 | """ 33 | sender = "clintonmatics@gmail.com" 34 | receiver = [instance.email, ] 35 | 36 | 37 | 38 | # send email 39 | send_mail( 40 | subject, 41 | message, 42 | sender, 43 | receiver, 44 | fail_silently=False, 45 | ) 46 | 47 | -------------------------------------------------------------------------------- /otp_app/templates/base.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | OTP Validation 7 | 8 | 9 | 10 | 15 | 16 | 17 | 18 | {% for message in messages %} 19 | 20 | 23 | 24 | {% endfor %} 25 | 26 | 27 | 28 | {% block content %} 29 | 30 |

Hello World

31 | 32 | {% endblock content %} 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /otp_app/templates/index.html: -------------------------------------------------------------------------------- 1 | {% extends "base.html" %} 2 | 3 | 4 | {% block content %} 5 |

6 |
7 |

Email Verification with OTP

8 |
9 | 10 | {% endblock content %} 11 | 12 | 13 | -------------------------------------------------------------------------------- /otp_app/templates/login.html: -------------------------------------------------------------------------------- 1 | {% extends "base.html" %} 2 | 3 | 4 | 5 | {% block content %} 6 | 7 | 8 | 25 | 26 | 27 |
28 | {% csrf_token %} 29 |

Login Form

30 |
31 | 32 | 33 |
34 | 35 |
36 | 37 | 38 |
39 | 40 | 41 |

Don't have an account? Register

42 | 43 |
44 | {% endblock content %} -------------------------------------------------------------------------------- /otp_app/templates/resend_otp.html: -------------------------------------------------------------------------------- 1 | {% extends "base.html" %} 2 | 3 | 4 | 5 | {% block content %} 6 | 7 | 8 | 25 | 26 | 27 |
28 | {% csrf_token %} 29 |

Resend OTP

30 |
31 | 32 | 33 |
34 | 35 | 36 | {% comment %}

Need a new OTP? Resend OTP

{% endcomment %} 37 | 38 |
39 | {% endblock content %} 40 | -------------------------------------------------------------------------------- /otp_app/templates/signup.html: -------------------------------------------------------------------------------- 1 | {% extends "base.html" %} 2 | 3 | 4 | 5 | {% block content %} 6 | 7 | 8 | 30 | 31 | 32 |
33 | {% csrf_token %} 34 |

Signup Form

35 | 36 | 37 | {% for field in form %} 38 | 39 | 40 | {% if field.errors %} 41 | 42 |
43 | 44 | {{field}} 45 | {{field.errors}} 46 |
47 | 48 | {% else %} 49 | 50 |
51 | 52 | {{field}} 53 |
54 | 55 | 56 | {% endif %} 57 | 58 | 59 | 60 | 61 | {% endfor %} 62 | 63 | 64 | 65 | 66 |

Already have an account? Login

67 | 68 |
69 | {% endblock content %} 70 | -------------------------------------------------------------------------------- /otp_app/templates/verify_token.html: -------------------------------------------------------------------------------- 1 | {% extends "base.html" %} 2 | 3 | 4 | 5 | {% block content %} 6 | 7 | 8 | 25 | 26 | 27 |
28 | {% csrf_token %} 29 |

Verify Email

30 |
31 | 32 | 33 |
34 | 35 | 36 |

Need a new OTP? Resend OTP

37 | 38 |
39 | {% endblock content %} 40 | -------------------------------------------------------------------------------- /otp_app/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | # Create your tests here. 4 | -------------------------------------------------------------------------------- /otp_app/urls.py: -------------------------------------------------------------------------------- 1 | from django.urls import path 2 | from . import views 3 | 4 | urlpatterns = [ 5 | path("", views.index, name="index"), 6 | path("register", views.signup, name="register"), 7 | path("verify-email/", views.verify_email, name="verify-email"), 8 | path("resend-otp", views.resend_otp, name="resend-otp"), 9 | path("login", views.signin, name="signin") 10 | ] 11 | -------------------------------------------------------------------------------- /otp_app/views.py: -------------------------------------------------------------------------------- 1 | from django.shortcuts import render, redirect 2 | from .forms import RegisterForm 3 | from .models import OtpToken 4 | from django.contrib import messages 5 | from django.contrib.auth import get_user_model 6 | from django.utils import timezone 7 | from django.core.mail import send_mail 8 | from django.contrib.auth import authenticate, login, logout 9 | 10 | 11 | # Create your views here. 12 | 13 | def index(request): 14 | return render(request, "index.html") 15 | 16 | 17 | 18 | def signup(request): 19 | form = RegisterForm() 20 | if request.method == 'POST': 21 | form = RegisterForm(request.POST) 22 | if form.is_valid(): 23 | form.save() 24 | messages.success(request, "Account created successfully! An OTP was sent to your Email") 25 | return redirect("verify-email", username=request.POST['username']) 26 | context = {"form": form} 27 | return render(request, "signup.html", context) 28 | 29 | 30 | 31 | 32 | def verify_email(request, username): 33 | user = get_user_model().objects.get(username=username) 34 | user_otp = OtpToken.objects.filter(user=user).last() 35 | 36 | 37 | if request.method == 'POST': 38 | # valid token 39 | if user_otp.otp_code == request.POST['otp_code']: 40 | 41 | # checking for expired token 42 | if user_otp.otp_expires_at > timezone.now(): 43 | user.is_active=True 44 | user.save() 45 | messages.success(request, "Account activated successfully!! You can Login.") 46 | return redirect("signin") 47 | 48 | # expired token 49 | else: 50 | messages.warning(request, "The OTP has expired, get a new OTP!") 51 | return redirect("verify-email", username=user.username) 52 | 53 | 54 | # invalid otp code 55 | else: 56 | messages.warning(request, "Invalid OTP entered, enter a valid OTP!") 57 | return redirect("verify-email", username=user.username) 58 | 59 | context = {} 60 | return render(request, "verify_token.html", context) 61 | 62 | 63 | 64 | 65 | def resend_otp(request): 66 | if request.method == 'POST': 67 | user_email = request.POST["otp_email"] 68 | 69 | if get_user_model().objects.filter(email=user_email).exists(): 70 | user = get_user_model().objects.get(email=user_email) 71 | otp = OtpToken.objects.create(user=user, otp_expires_at=timezone.now() + timezone.timedelta(minutes=5)) 72 | 73 | 74 | # email variables 75 | subject="Email Verification" 76 | message = f""" 77 | Hi {user.username}, here is your OTP {otp.otp_code} 78 | it expires in 5 minute, use the url below to redirect back to the website 79 | http://127.0.0.1:8000/verify-email/{user.username} 80 | 81 | """ 82 | sender = "clintonmatics@gmail.com" 83 | receiver = [user.email, ] 84 | 85 | 86 | # send email 87 | send_mail( 88 | subject, 89 | message, 90 | sender, 91 | receiver, 92 | fail_silently=False, 93 | ) 94 | 95 | messages.success(request, "A new OTP has been sent to your email-address") 96 | return redirect("verify-email", username=user.username) 97 | 98 | else: 99 | messages.warning(request, "This email dosen't exist in the database") 100 | return redirect("resend-otp") 101 | 102 | 103 | context = {} 104 | return render(request, "resend_otp.html", context) 105 | 106 | 107 | 108 | 109 | def signin(request): 110 | if request.method == 'POST': 111 | username = request.POST['username'] 112 | password = request.POST['password'] 113 | user = authenticate(request, username=username, password=password) 114 | 115 | if user is not None: 116 | login(request, user) 117 | messages.success(request, f"Hi {request.user.username}, you are now logged-in") 118 | return redirect("index") 119 | 120 | else: 121 | messages.warning(request, "Invalid credentials") 122 | return redirect("signin") 123 | 124 | return render(request, "login.html") 125 | -------------------------------------------------------------------------------- /otp_validation/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeWithClinton/email_verification_django/ac724fe15675eb83e6ba69fa44a37a9de3da407e/otp_validation/__init__.py -------------------------------------------------------------------------------- /otp_validation/__pycache__/__init__.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeWithClinton/email_verification_django/ac724fe15675eb83e6ba69fa44a37a9de3da407e/otp_validation/__pycache__/__init__.cpython-39.pyc -------------------------------------------------------------------------------- /otp_validation/__pycache__/settings.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeWithClinton/email_verification_django/ac724fe15675eb83e6ba69fa44a37a9de3da407e/otp_validation/__pycache__/settings.cpython-39.pyc -------------------------------------------------------------------------------- /otp_validation/__pycache__/urls.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeWithClinton/email_verification_django/ac724fe15675eb83e6ba69fa44a37a9de3da407e/otp_validation/__pycache__/urls.cpython-39.pyc -------------------------------------------------------------------------------- /otp_validation/__pycache__/wsgi.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeWithClinton/email_verification_django/ac724fe15675eb83e6ba69fa44a37a9de3da407e/otp_validation/__pycache__/wsgi.cpython-39.pyc -------------------------------------------------------------------------------- /otp_validation/asgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | ASGI config for otp_validation project. 3 | 4 | It exposes the ASGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.0/howto/deployment/asgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.asgi import get_asgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'otp_validation.settings') 15 | 16 | application = get_asgi_application() 17 | -------------------------------------------------------------------------------- /otp_validation/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for otp_validation project. 3 | 4 | Generated by 'django-admin startproject' using Django 4.0.2. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.0/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/4.0/ref/settings/ 11 | """ 12 | 13 | from pathlib import Path 14 | 15 | # Build paths inside the project like this: BASE_DIR / 'subdir'. 16 | BASE_DIR = Path(__file__).resolve().parent.parent 17 | 18 | 19 | # Quick-start development settings - unsuitable for production 20 | # See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/ 21 | 22 | # SECURITY WARNING: keep the secret key used in production secret! 23 | SECRET_KEY = 'django-insecure-n9z7m@y8)f31h7=qgw@qgjj-qp&ndr%$l%t0axl^9s*_m5qob%' 24 | 25 | # SECURITY WARNING: don't run with debug turned on in production! 26 | DEBUG = True 27 | 28 | ALLOWED_HOSTS = [] 29 | 30 | 31 | # Application definition 32 | 33 | INSTALLED_APPS = [ 34 | 'django.contrib.admin', 35 | 'django.contrib.auth', 36 | 'django.contrib.contenttypes', 37 | 'django.contrib.sessions', 38 | 'django.contrib.messages', 39 | 'django.contrib.staticfiles', 40 | 'otp_app.apps.OtpAppConfig' 41 | ] 42 | 43 | MIDDLEWARE = [ 44 | 'django.middleware.security.SecurityMiddleware', 45 | 'django.contrib.sessions.middleware.SessionMiddleware', 46 | 'django.middleware.common.CommonMiddleware', 47 | 'django.middleware.csrf.CsrfViewMiddleware', 48 | 'django.contrib.auth.middleware.AuthenticationMiddleware', 49 | 'django.contrib.messages.middleware.MessageMiddleware', 50 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', 51 | ] 52 | 53 | ROOT_URLCONF = 'otp_validation.urls' 54 | 55 | TEMPLATES = [ 56 | { 57 | 'BACKEND': 'django.template.backends.django.DjangoTemplates', 58 | 'DIRS': [], 59 | 'APP_DIRS': True, 60 | 'OPTIONS': { 61 | 'context_processors': [ 62 | 'django.template.context_processors.debug', 63 | 'django.template.context_processors.request', 64 | 'django.contrib.auth.context_processors.auth', 65 | 'django.contrib.messages.context_processors.messages', 66 | ], 67 | }, 68 | }, 69 | ] 70 | 71 | WSGI_APPLICATION = 'otp_validation.wsgi.application' 72 | 73 | 74 | # Database 75 | # https://docs.djangoproject.com/en/4.0/ref/settings/#databases 76 | 77 | DATABASES = { 78 | 'default': { 79 | 'ENGINE': 'django.db.backends.sqlite3', 80 | 'NAME': BASE_DIR / 'db.sqlite3', 81 | } 82 | } 83 | 84 | 85 | # Password validation 86 | # https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators 87 | 88 | AUTH_PASSWORD_VALIDATORS = [ 89 | { 90 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 91 | }, 92 | { 93 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 94 | }, 95 | { 96 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 97 | }, 98 | { 99 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 100 | }, 101 | ] 102 | 103 | 104 | # Internationalization 105 | # https://docs.djangoproject.com/en/4.0/topics/i18n/ 106 | 107 | LANGUAGE_CODE = 'en-us' 108 | 109 | TIME_ZONE = 'UTC' 110 | 111 | USE_I18N = True 112 | 113 | USE_TZ = True 114 | 115 | 116 | # Static files (CSS, JavaScript, Images) 117 | # https://docs.djangoproject.com/en/4.0/howto/static-files/ 118 | 119 | STATIC_URL = 'static/' 120 | 121 | # Default primary key field type 122 | # https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field 123 | 124 | DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' 125 | AUTH_USER_MODEL = 'otp_app.CustomUser' 126 | 127 | 128 | EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' 129 | EMAIL_HOST = 'smtp.gmail.com' 130 | EMAIL_PORT = 587 131 | EMAIL_HOST_USER = 'clintonmatics@gmail.com' 132 | EMAIL_HOST_PASSWORD = 'ejmz hkpw mlnf ljji' 133 | EMAIL_USE_TLS = True 134 | EMAIL_USE_SSL = False -------------------------------------------------------------------------------- /otp_validation/urls.py: -------------------------------------------------------------------------------- 1 | """otp_validation URL Configuration 2 | 3 | The `urlpatterns` list routes URLs to views. For more information please see: 4 | https://docs.djangoproject.com/en/4.0/topics/http/urls/ 5 | Examples: 6 | Function views 7 | 1. Add an import: from my_app import views 8 | 2. Add a URL to urlpatterns: path('', views.home, name='home') 9 | Class-based views 10 | 1. Add an import: from other_app.views import Home 11 | 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') 12 | Including another URLconf 13 | 1. Import the include() function: from django.urls import include, path 14 | 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 15 | """ 16 | from django.contrib import admin 17 | from django.urls import path, include 18 | 19 | urlpatterns = [ 20 | path('admin/', admin.site.urls), 21 | path("", include("otp_app.urls")) 22 | ] 23 | -------------------------------------------------------------------------------- /otp_validation/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for otp_validation project. 3 | 4 | It exposes the WSGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.0/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.wsgi import get_wsgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'otp_validation.settings') 15 | 16 | application = get_wsgi_application() 17 | --------------------------------------------------------------------------------