2

I want to use JWT authentication in my nuxtjs app but it's not refreshing the token. For testing purposes, I set the access token expire time 5 seconds and refresh token for 30 minutes.

Access token expire after 5 seconds but the token is not refreshing after 5 second its logged-out

I'm using the Django rest framework and in nuxt I'm using nuxt auth v5

settings.py

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework_simplejwt.authentication.JWTAuthentication',
    ),
}
SIMPLE_JWT = {
    'AUTH_HEADER_TYPES': ('JWT',),
    'ACCESS_TOKEN_LIFETIME': timedelta(seconds=5),
    'REFRESH_TOKEN_LIFETIME': timedelta(minutes=30),
}

nuxt.config.js

auth: {
    localStorage: false,
    strategies: {
      local: {
        scheme: 'refresh',
        
        token: {
          property: 'access',
          type: 'JWT',
          required: true
        },
        refreshToken: {
          property: 'refresh',
  
          
        },
        user: {
          property: false,
          autoFetch: true
        },
        endpoints: {
          login: { url: '/jwt/create/', method: 'post',},
          refresh: { url: '/jwt/refresh/', method: 'post',},
          user: {url: '/users/me/', method: 'get' },
          logout: false
         
        },
      }
    }
  }
}

I'm little new in JWT and nuxt. Correct me if I'm wrong.

My main goal is just to refresh the token automatically when access toke is expired.

Arpit Soni
  • 101
  • 2
  • 5
  • 1
    Hi, did you tried this one: https://auth.nuxtjs.org/schemes/refresh#usage Try to make a button a call it on click, for testing purposes at first. – kissu Jul 13 '21 at 15:11
  • 1
    would be good to have a bit more detail. What do you get in the web inspector when refreshing, what is you urls.py config,... – jkoestinger Jul 13 '21 at 15:26
  • 1
    Possibly, you're missing the `data: 'refresh'` in your refresh config in nuxt, since you're using simplejwt and it's the default key that is used: https://auth.nuxtjs.org/schemes/refresh/#refreshtoken – jkoestinger Jul 13 '21 at 15:30
  • hey @jkoestinger, Yes it's work. Thanks :) and one more thing, how to keep the user logged in until the user logout by themself. – Arpit Soni Jul 13 '21 at 16:38

0 Answers0