Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

2 Answers

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.

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).
like image 26
mirek Avatar answered Sep 13 '25 11:09

mirek