Search code examples
pythondjangodjango-rest-frameworkdjango-rest-framework-simplejwt

How to resend verification email django restframework?


I can send a verification email upon registering the user so that the account can be activated. But how would one go about resending another verification email on an API? Here, I am making an activation link with a token in it and when the user opens the link it takes the token and verifies the user. But how would resending the verification email work?

class RegisterUser(APIView):
    serialzer_class = RegisterSerialzer
    def post(self, request):
        user = request.data
        serializer = self.serialzer_class(data = user)
        serializer.is_valid(raise_exception = True)     
        serializer.save()
        user_data = serializer.data
        # user = User.objects.get(username=serializer.data['username'])
        # print(user.id)
        # token = Token.objects.create(user=user)
        user = User.objects.get(email = user_data['email'])
        token = RefreshToken.for_user(user).access_token
        current_site = get_current_site(request).domain
        relativeLink = reverse('email-verify')
        
        absurl = 'http://'+current_site+relativeLink+"?token="+str(token)
        email_body = 'Hi '+ user.username + 'user the link below to verify your email \n' + absurl

        data = {'email_body':email_body,'to_email':user.email,
                'email_subject':'Verify your email'}
        Util.send_email(data)

class VerifyEmail(APIView):
    def get(self, request):
        token = request.GET.get('token')
        try:
            payload = jwt.decode(token, settings.SECRET_KEY)
            user = User.objects.get(id=payload['user_id'])
            user.is_verified = True
            # user.is_authenticated = True
            user.is_active = True
            # if not user.is_verified:
            user.save()
            return Response({'email':'successfuly activated'}, status=status.HTTP_200_OK)
        # except jwt.ExpiredSignatureError as identifier:
        except jwt.ExpiredSignatureError:
            return Response({'error':'Activation Expired expired'}, status=status.HTTP_400_BAD_REQUEST)
        # except jwt.exceptions.DecodeError as identifier:
        except jwt.exceptions.DecodeError:
            return Response({'error':'invalid token'}, status=status.HTTP_400_BAD_REQUEST)

Solution

  • My work around is asking for the email from the user or frontend, and then query the user to get email address and send it to the email.

    class ResendVerifyEmail(APIView):
        serializer_class = RegisterSerialzer
        def post(self, request):
            data = request.data
            # email = data.get('email')
            email = data['email']
            print(email)
            try:
                user = User.objects.get(email=email)
           
                print('hello')
                if user.is_verified:
                    return Response({'msg':'User is already verified'})
                print (user.username)
                token = RefreshToken.for_user(user).access_token
                current_site= get_current_site(request).domain
                relativeLink = reverse('email-verify')
                
                absurl = 'http://'+current_site+relativeLink+"?token="+str(token)
                email_body = 'Hi '+ user.username + ' this is the resent link to verify your email \n' + absurl
    
                data = {'email_body':email_body,'to_email':user.email,
                        'email_subject':'Verify your email'}
                Util.send_email(data)
                return Response({'msg':'The verification email has been sent'}, status=status.HTTP_201_CREATED)
            except User.DoesNotExist:
                return Response({'msg':'No such user, register first'})