djangodjango-rest-frameworkdjango-viewsdjoser

How to handle activation url with Django Djoser?


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')),

Solution

  • 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.