Your email has been successfully verified! You can now log in.
+ To the login + } @else { +
+ There was an issue verifying your email.
Please try again or
+ contact support for assistance.
+
{{ errors["error"] }}
+diff --git a/backend/videoflix/auth/views.py b/backend/videoflix/auth/views.py index a55c365..f80407b 100644 --- a/backend/videoflix/auth/views.py +++ b/backend/videoflix/auth/views.py @@ -9,6 +9,7 @@ from django.utils.html import strip_tags from django.core.mail import EmailMultiAlternatives from django.template.loader import render_to_string from users.models import CustomUser +from django.db.models import Q class LoginView(APIView): serializer_class = LoginSerializer @@ -55,7 +56,7 @@ class RegisterView(APIView): merge_data = { 'name': user.username, 'email': user.email, - 'token': user.verify_email + 'token': user.verify_email_token } html_body = render_to_string("mail.html", merge_data) @@ -70,4 +71,19 @@ class RegisterView(APIView): class VerifyEmailView(APIView): def post(self, request): - pass \ No newline at end of file + email = request.data.get('email') + token = request.data.get('token') + + if not email or not token: + return Response({"error": "Email and token are required."}, status=status.HTTP_400_BAD_REQUEST) + + user = CustomUser.objects.filter(Q(email=email) & Q(verify_email_token=token)).first() + if user: + if user.is_active: + return Response({"error": "User has already been activated"}, status=status.HTTP_409_CONFLICT) + + user.is_active = True + user.save() + return Response(status=status.HTTP_200_OK) + else: + return Response({"error": "Invalid email or token."}, status=status.HTTP_400_BAD_REQUEST) diff --git a/backend/videoflix/templates/mail.html b/backend/videoflix/templates/mail.html index a4ab908..240233d 100644 --- a/backend/videoflix/templates/mail.html +++ b/backend/videoflix/templates/mail.html @@ -40,7 +40,7 @@ Thank you for registering with Videoflix. To complete your registration and verify your email address, please click the link below:
- Activate account + Activate accountIf you did not create an account with us, please disregard this email.
diff --git a/backend/videoflix/users/admin.py b/backend/videoflix/users/admin.py index 12ef327..d228978 100644 --- a/backend/videoflix/users/admin.py +++ b/backend/videoflix/users/admin.py @@ -13,7 +13,7 @@ class CustomUserAdmin(UserAdmin): 'Individual data', { 'fields': ( - 'verify_email', + 'verify_email_token', ) } ) diff --git a/backend/videoflix/users/models.py b/backend/videoflix/users/models.py index 5378cdc..b8a922d 100644 --- a/backend/videoflix/users/models.py +++ b/backend/videoflix/users/models.py @@ -1,17 +1,17 @@ -import secrets import string -from django.db import models +import secrets from django.contrib.auth.models import AbstractUser +from django.db import models class CustomUser(AbstractUser): - verify_email = models.CharField(max_length=15, blank=True) + verify_email_token = models.CharField(max_length=20, blank=True, null=True) def save(self, *args, **kwargs): - if not self.verify_email: - self.verify_email = self.generate_verification_token() + if not self.verify_email_token: + self.verify_email_token = self.generate_verification_token() super().save(*args, **kwargs) @staticmethod - def generate_verification_token(length=15): + def generate_verification_token(length=20): characters = string.ascii_letters + string.digits return ''.join(secrets.choice(characters) for _ in range(length)) diff --git a/frontend/src/app/app.routes.ts b/frontend/src/app/app.routes.ts index ccf07ff..1ed769b 100644 --- a/frontend/src/app/app.routes.ts +++ b/frontend/src/app/app.routes.ts @@ -9,6 +9,7 @@ export const routes: Routes = [ { path: 'login', component: HomeComponent }, { path: 'register', component: HomeComponent }, { path: 'forgot-password', component: HomeComponent }, + { path: 'verify-email', component: HomeComponent }, { path: 'imprint', component: ImprintComponent }, { path: 'privacy-policy', component: PrivacyPolicyComponent }, { path: 'browse', component: BrowseComponent }, diff --git a/frontend/src/app/components/auth/verify-email/verify-email.component.html b/frontend/src/app/components/auth/verify-email/verify-email.component.html new file mode 100644 index 0000000..5de9b15 --- /dev/null +++ b/frontend/src/app/components/auth/verify-email/verify-email.component.html @@ -0,0 +1,21 @@ +Your email has been successfully verified! You can now log in.
+ To the login + } @else { +
+ There was an issue verifying your email.
Please try again or
+ contact support for assistance.
+
{{ errors["error"] }}
+