I followed your steps and so far this is whati have done also the issue that i am facing.
Firsly, i created a serilaizer to register and verify the email
serailizer.py
class VerifyAccountSerializer(serializers.Serializer):
email = serializers.EmailField()
otp = serializers.CharField()
class EmailSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ['email']
def create(self, validated_data):
user = User.objects.create(
username="Username",
email=validated_data['email'],
)
user.save()
return user
Then i created a view to register the email and send a 'verify your email ’ message
class VerifyEmail(generics.CreateAPIView):
queryset = User.objects.all()
permission_classes = (AllowAny,)
serializer_class = EmailSerializer
def create(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
instance = serializer.save()
send_otp_via_email(instance.email)
headers = self.get_success_headers(serializer.data)
return Response({
'status':201,
'message':'Verify Your Email to continue',
'data': serializer.data,
'headers':headers
})
NOTE: the send otp via email is another function that i created to send the email.
this is also the view to verify the email using an otp
class VerifyOTP(APIView):
def post(self, request):
try:
data = request.data
serializer = VerifyAccountSerializer(data=data)
if serializer.is_valid():
email = serializer.data['email']
otp = serializer.data['otp']
user = User.objects.filter(email=email)
if not user.exists():
return Response({
'status':400,
'message':'Email not found',
'data': "invalid email",
})
if not user[0].otp == otp:
return Response({
'status':400,
'message':'Invalid Otp',
'data': "invalid otp",
})
user = user.first()
user.is_verified = True
user.active = True
user.save()
return Response({
'status':200,
'message':'Account Verified Successfully.',
'data': {},
})
return Response({
'status':500,
'message':'Something went wrong.',
'data': serializer.errors,
})
except Exception as e:
print(e)
So this verifies the email.
The next thing i am finding it difficult to do is, how to then retrieve this user from the database, collect the rest of the information and complete the registration.
This is what i have done.
class RegisterAccountView(generics.RetrieveUpdateAPIView):
queryset = User.objects.all()
permission_classes = (AllowAny,)
serializer_class = RegisterSerializer
def get_object(self, request, *args, **kwargs):
user_id = self.kwargs['user_id']
user = User.objects.get(id=user_id)
# I don't know how to go ahead and get the email from the db
# and also complete the registration
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
instance = serializer.save()
headers = self.get_success_headers(serializer.data)
return Response({
'status':201,
'message':'Account Created Successfully',
'data': serializer.data,
'headers':headers
})
i don’t know if i should overide the create()
or continue with the get_object()
.
Please how do i then retrieve the user using the email they have registered then complete the registration process.
Any help would be really appreciated.