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?
JavaScript
x
42
42
1
class RegisterUser(APIView):
2
serialzer_class = RegisterSerialzer
3
def post(self, request):
4
user = request.data
5
serializer = self.serialzer_class(data = user)
6
serializer.is_valid(raise_exception = True)
7
serializer.save()
8
user_data = serializer.data
9
# user = User.objects.get(username=serializer.data['username'])
10
# print(user.id)
11
# token = Token.objects.create(user=user)
12
user = User.objects.get(email = user_data['email'])
13
token = RefreshToken.for_user(user).access_token
14
current_site = get_current_site(request).domain
15
relativeLink = reverse('email-verify')
16
17
absurl = 'http://'+current_site+relativeLink+"?token="+str(token)
18
email_body = 'Hi '+ user.username + 'user the link below to verify your email n' + absurl
19
20
data = {'email_body':email_body,'to_email':user.email,
21
'email_subject':'Verify your email'}
22
Util.send_email(data)
23
24
class VerifyEmail(APIView):
25
def get(self, request):
26
token = request.GET.get('token')
27
try:
28
payload = jwt.decode(token, settings.SECRET_KEY)
29
user = User.objects.get(id=payload['user_id'])
30
user.is_verified = True
31
# user.is_authenticated = True
32
user.is_active = True
33
# if not user.is_verified:
34
user.save()
35
return Response({'email':'successfuly activated'}, status=status.HTTP_200_OK)
36
# except jwt.ExpiredSignatureError as identifier:
37
except jwt.ExpiredSignatureError:
38
return Response({'error':'Activation Expired expired'}, status=status.HTTP_400_BAD_REQUEST)
39
# except jwt.exceptions.DecodeError as identifier:
40
except jwt.exceptions.DecodeError:
41
return Response({'error':'invalid token'}, status=status.HTTP_400_BAD_REQUEST)
42
Advertisement
Answer
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.
JavaScript
1
28
28
1
class ResendVerifyEmail(APIView):
2
serializer_class = RegisterSerialzer
3
def post(self, request):
4
data = request.data
5
# email = data.get('email')
6
email = data['email']
7
print(email)
8
try:
9
user = User.objects.get(email=email)
10
11
print('hello')
12
if user.is_verified:
13
return Response({'msg':'User is already verified'})
14
print (user.username)
15
token = RefreshToken.for_user(user).access_token
16
current_site= get_current_site(request).domain
17
relativeLink = reverse('email-verify')
18
19
absurl = 'http://'+current_site+relativeLink+"?token="+str(token)
20
email_body = 'Hi '+ user.username + ' this is the resent link to verify your email n' + absurl
21
22
data = {'email_body':email_body,'to_email':user.email,
23
'email_subject':'Verify your email'}
24
Util.send_email(data)
25
return Response({'msg':'The verification email has been sent'}, status=status.HTTP_201_CREATED)
26
except User.DoesNotExist:
27
return Response({'msg':'No such user, register first'})
28