stale token even for first time user Djoser and DRF

Viewed 233

the newly registered user get email for activation . He clicks on the links and move to an activation page .then he clicks on verify button which take uid and token from the link and post it to auth/users/activation/ and then gets the response stale token for the given user no matter how fast he click on the link on verify link . result is same. I am using djoser for activation and all user related stuff.and redux in the frontend for api calls and also the React as frontend here is my settings.py:

SIMPLE_JWT = {
    'ACCESS_TOKEN_LIFETIME': timedelta(days=30),
    'REFRESH_TOKEN_LIFETIME': timedelta(days=1),

    'AUTH_HEADER_TYPES': ('JWT','Bearer'),

     'USER_AUTHENTICATION_RULE': 'rest_framework_simplejwt.authentication.default_user_authentication_rule' ,
    'AUTH_TOKEN_CLASSES': ('rest_framework_simplejwt.tokens.AccessToken',),

}

DJOSER = {
    'LOGIN_FIELD': 'email',
    
    'USERNAME_CHANGED_EMAIL_CONFIRMATION': True,
    'PASSWORD_CHANGED_EMAIL_CONFIRMATION': True,
    'SEND_CONFIRMATION_EMAIL': True,
    'SET_USERNAME_RETYPE': True,
    'SET_PASSWORD_RETYPE': True,
    'PASSWORD_CHANGED_EMAIL_CONFIRMATION':True,
    'PASSWORD_RESET_CONFIRM_URL': 'password/reset/confirm/{uid}/{token}',
  
    'SEND_ACTIVATION_EMAIL': True,
    'ACTIVATION_URL': 'activate/{uid}/{token}',
    'SOCIAL_AUTH_TOKEN_STRATEGY': 'djoser.social.token.jwt.TokenStrategy',
    'SOCIAL_AUTH_ALLOWED_REDIRECT_URIS': ['https://negoziohub.herokuapp.com/google', 'http://127.0.0.1:8000/facebook'],

    'SERIALIZERS': {
        'user_create': 'base.serializers.UserSerializer',
        'user': 'base.serializers.UserSerializer',
        'user_delete': 'djoser.serializers.UserDeleteSerializer',
    }
}

here is userAction.js:


export const activate = (uid, token) => async (dispatch) => {
    try {
        dispatch({
            type: USER_ACTIVATE_REQUEST
        })

        

        const config = {
            headers: {
                'Content-type': 'application/json',
               
            }
        }
        const body = JSON.stringify({ uid, token });

        const { data } = await axios.post(`/auth/users/activation/`, body,
            config
        )

        dispatch({
            type: USER_ACTIVATE_SUCCESS,
            payload: data
        })
        // dispatch(login())

        localStorage.setItem('userInfo', JSON.stringify(data))


    } catch (error) {
        dispatch({
            type: USER_ACTIVATE_FAIL,
            payload: error.response && error.response.data.detail
                ? error.response.data.detail
                : error.message,
        })
    }
}

Any help would be appreciated. Thanks❤❤❤

1 Answers

I know it's too late to answer your question but I will do it just for other folks out there who might face the same issue in the future.

I had the same problem and I couldn't find anything on the internet, which drove me to dive deep in the source code of Djoser. The issue in my case was that I changed the default behavior to change the state of the user and set user.is_active = True. This was the issue as Djoser sends the stale token error if user.is_active != False.

Hope this helps.

Related