4

I'm unable to activate users via the activation link.

How can I correctly configure the path to process their request?

My config:

DJOSER = {
    'PASSWORD_RESET_CONFIRM_URL': 'api/v1/auth/users/password/reset/confirm/{uid}/{token}',
    'USERNAME_RESET_CONFIRM_URL': 'api/v1/auth/users/username/reset/confirm/{uid}/{token}',
    'ACTIVATION_URL': 'api/v1/auth/users/activate/{uid}/{token}',
    'SEND_ACTIVATION_EMAIL': True,
    'SEND_CONFIRMATION_EMAIL': True,
    'SERIALIZERS': {},
    # 'USER_ID_FIELD': '',
    'LOGIN_FIELD': 'username',
    'USER_CREATE_PASSWORD_RETYPE': True,
    'SET_USERNAME_RETYPE': True,
    'USERNAME_RESET_CONFIRM_RETYPE': True,
    'SOCIAL_AUTH_ALLOWED_REDIRECT_URIS': [],
    'HIDE_USERS': True,
    'EMAIL': {
        'activation': 'api.email.ActivationEmail',
        'confirmation': 'api.email.ConfirmationEmail',
        'password_reset': 'api.email.PasswordResetEmail',
        'password_changed_confirmation': 'api.email.PasswordChangedConfirmationEmail',
        'username_changed_confirmation': 'api.email.UsernameChangedConfirmationEmail',
        'username_reset': 'api.email.UsernameResetEmail',
    }
}


   path('auth/', include('djoser.urls')),
   path('auth/', include('djoser.urls.jwt')),
   path('auth/', include('djoser.urls.authtoken')),

2 Answers2

5

I had to specificy an endpoint where the uid and token and would be included in the parameters. This endpoint directed to a view that would handle these parameters. Then send a post request to the djoser activation endpoint. we cannot directly use the url given by djoser because it expects a post request whereas the user will submit a get request by clicking the link in the email. setting:

DJOSER = {
 'ACTIVATION_URL': 'account-activate/{uid}/{token}/',
}

view:

class ActivateUser(GenericAPIView):

    def get(self, request, uid, token, format = None):
        payload = {'uid': uid, 'token': token}

        url = "http://localhost:8000/api/v1/auth/users/activation/"
        response = requests.post(url, data = payload)

        if response.status_code == 204:
            return Response({}, response.status_code)
        else:
            return Response(response.json())

a similar solution here but djoser updated since they posted their answer and I had to make some changes.

  • How to redirect to frontend application immediately after this process? – Imtiaz Ahmed Jun 14 '22 at 18:05
  • The get method is not getting called. Clicking the url from the activation mail just requests a get request, but this get method is not getting called. Any help? – Imtiaz Ahmed Jun 14 '22 at 18:25
  • @Imtiaz "get method not called": Do you have in `urls.py` the url `account-activate/` connected to `ActivateUser.as_view()` ? – mirek Feb 07 '23 at 10:20
  • @Imtiaz, a note to my previous comment: You need catch `uid` and `token` parameters in url definition or use `request.GET.get('uid', None)` – mirek Feb 07 '23 at 10:26
0

If I understand it properly, we have 3 ways to go.

  1. re-design djoser's POST endpoint into Backend-GET endpoint and use it in settings.DJOSER['ACTIVATION_URL']; GET must return a web page; not recommended and not clean solution, because GET will update a database - read https://github.com/sunscrapers/djoser/issues/14
  2. create Backend-GET endpoint which calls djoser's POST endpoint via requests; again GET should return a web page - read the answer here above
  3. (recommended by Djoser docs): Create a Frontend-GET endpoint which will call the djoser's POST via axios (or FetchAPI). In axios response we can redirect (probably to login page).
mirek
  • 1,140
  • 11
  • 10