added verify token in custom user model

This commit is contained in:
Chneemann 2024-08-05 15:44:42 +02:00
parent 537519a3dd
commit 3b4e1634ab
5 changed files with 27 additions and 11 deletions

View file

@ -53,7 +53,9 @@ class RegisterView(APIView):
def send_email(self, user): def send_email(self, user):
merge_data = { merge_data = {
'name': user.username 'name': user.username,
'email': user.email,
'token': user.verify_email
} }
html_body = render_to_string("mail.html", merge_data) html_body = render_to_string("mail.html", merge_data)
@ -64,4 +66,8 @@ class RegisterView(APIView):
to=['andre.kempf.dev@gmail.com'] to=['andre.kempf.dev@gmail.com']
) )
message.attach_alternative(html_body, "text/html") message.attach_alternative(html_body, "text/html")
message.send(fail_silently=False) message.send(fail_silently=False)
class VerifyEmailView(APIView):
def post(self, request):
pass

View file

@ -40,7 +40,7 @@
Thank you for registering with Videoflix. To complete your registration Thank you for registering with Videoflix. To complete your registration
and verify your email address, please click the link below: and verify your email address, please click the link below:
</p> </p>
<a class="btn" href="">Activate account</a> <a class="btn" href="http://localhost:4200/verify-email?token={{ token }}&email={{ email }}">Activate account</a>
<p> <p>
If you did not create an account with us, please disregard this email. If you did not create an account with us, please disregard this email.
</p> </p>

View file

@ -13,9 +13,7 @@ class CustomUserAdmin(UserAdmin):
'Individual data', 'Individual data',
{ {
'fields': ( 'fields': (
'custom', 'verify_email',
'phone',
'address',
) )
} }
) )

View file

@ -1,7 +1,17 @@
from django.contrib.auth.models import AbstractUser import secrets
import string
from django.db import models from django.db import models
from django.contrib.auth.models import AbstractUser
class CustomUser(AbstractUser): class CustomUser(AbstractUser):
custom = models.CharField(max_length=1000, blank=True) verify_email = models.CharField(max_length=15, blank=True)
phone = models.CharField(max_length=20, blank=True)
address = models.CharField(max_length=150, blank=True) def save(self, *args, **kwargs):
if not self.verify_email:
self.verify_email = self.generate_verification_token()
super().save(*args, **kwargs)
@staticmethod
def generate_verification_token(length=15):
characters = string.ascii_letters + string.digits
return ''.join(secrets.choice(characters) for _ in range(length))

View file

@ -17,7 +17,7 @@ Including another URLconf
from django.contrib import admin from django.contrib import admin
from django.urls import path, include from django.urls import path, include
from users import views from users import views
from auth.views import LoginView, RegisterView from auth.views import LoginView, RegisterView, VerifyEmailView
urlpatterns = [ urlpatterns = [
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
@ -25,4 +25,6 @@ urlpatterns = [
path('users/<int:id>/', views.user_detail), path('users/<int:id>/', views.user_detail),
path('auth/login/', LoginView.as_view()), path('auth/login/', LoginView.as_view()),
path('auth/register/', RegisterView.as_view()), path('auth/register/', RegisterView.as_view()),
path('auth/verify-email/', VerifyEmailView.as_view()),
] ]