Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. //views.py
  2. class PasswordResetConfirmSerializer(serializers.Serializer):
  3. new_password1 = serializers.CharField(max_length=128)
  4. new_password2 = serializers.CharField(max_length=128)
  5. uid = serializers.CharField()
  6. token = serializers.CharField()
  7.  
  8. set_password_form_class = SetPasswordForm
  9. def custom_validation(self, attrs):
  10. pass
  11. def validate(self, attrs):
  12. self._errors = {}
  13. try:
  14. self.user = UserModel._default_manager.get(pk=attrs['uid'])
  15. except (TypeError, ValueError, OverflowError, UserModel.DoesNotExist):
  16. raise ValidationError({'uid': ['Invalid value']})
  17. self.custom_validation(attrs)
  18. self.set_password_form = self.set_password_form_class(
  19. user=self.user, data=attrs
  20. )
  21. if not self.set_password_form.is_valid():
  22. raise serializers.ValidationError(self.set_password_form.errors)
  23. return attrs
  24. def save(self):
  25. return self.set_password_form.save()
  26.  
  27. // serializers.py
  28. class PasswordResetConfirmView(GenericAPIView):
  29. serializer_class = PasswordResetConfirmSerializer
  30. permission_classes = (AllowAny,)
  31.  
  32. @sensitive_post_parameters_m
  33. def dispatch(self, *args, **kwargs):
  34. return super(PasswordResetConfirmView, self).dispatch(*args, **kwargs)
  35.  
  36. def post(self, request, *args, **kwargs):
  37. serializer = self.get_serializer(data=request.data)
  38. serializer.is_valid(raise_exception=True)
  39. serializer.save()
  40. return Response(
  41. {"detail": ("Password has been reset with the new password.")}
  42. )
  43.  
  44. //urls.py
  45. path('api/passwordreset/confirm/',views.PasswordResetConfirmView.as_view(), name = 'password_reset_confirm')
  46.  
  47. // React
  48. const config = {
  49. headers: {
  50. "Content-Type": "application/json"
  51. }
  52. };
  53.  
  54. const body = JSON.stringify({
  55. new_password1: this.state.new_password,
  56. new_password2: this.state.confirm_password,
  57. uid: this.state.fetched_data.pk,
  58. token: this.state.fetched_data.token
  59. })
  60.  
  61. axios.post(API_URL + 'users/api/passwordreset/confirm/', body, config)
  62.  
  63. // views.py
  64. class CustomObtainAuthToken(ObtainAuthToken):
  65. def post(self, request, *args, **kwargs):
  66. response = super(CustomObtainAuthToken, self).post(request, *args, **kwargs)
  67. token = Token.objects.get(key=response.data['token'])
  68. return Response({'token': token.key, 'id': token.user_id})
  69.  
  70. // urls.py
  71. path('api/authenticate/', CustomObtainAuthToken.as_view())
  72.  
  73. // React
  74. const body = {
  75. password: 'mike',
  76. username: 'Abcd1234'
  77. }
  78.  
  79. await axios.post(API_URL + 'users/api/authenticate/', body)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement