Django-NuxtJs: не удается получить cookie из запроса

  • Я использую django и nuxt js для создания веб-приложения, но у меня возникла проблема с получением данных пользователя с сервера после аутентификации.

  • я пробовал использовать куки, но пока не получилось. сервер не может получить куки из запроса, хотя я установил credential: true. буду благодарен за любую помощь

    .

Views.py:

nuxt.config.js

export default {
  // Global page headers: https://go.nuxtjs.dev/config-head
  head: {
    title: 'client',
    htmlAttrs: {
      lang: 'en'
    },
    meta: [
      { charset: 'utf-8' },
      { name: 'viewport', content: 'width=device-width, initial-scale=1' },
      { hid: 'description', name: 'description', content: '' },
      { name: 'format-detection', content: 'telephone=no' }
    ],
    link: [
      { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }
    ]
  },

  // Global CSS: https://go.nuxtjs.dev/config-css
  css: [
  ],

  // Plugins to run before rendering page: https://go.nuxtjs.dev/config-plugins
  plugins: [
  ],

  // Auto import components: https://go.nuxtjs.dev/config-components
  components: true,

  // Modules for dev and build (recommended): https://go.nuxtjs.dev/config-modules
  buildModules: [
    // https://go.nuxtjs.dev/eslint
    '@nuxtjs/eslint-module',
  ],

  // Modules: https://go.nuxtjs.dev/config-modules
  modules: [
    // https://go.nuxtjs.dev/bootstrap
    'bootstrap-vue/nuxt',
    // https://go.nuxtjs.dev/axios
    '@nuxtjs/axios',
    '@nuxtjs/auth-next'
  ],
  

  // Axios module configuration: https://go.nuxtjs.dev/config-axios
  axios: {
    baseURL: "http://127.0.0.1:8000/",
    credentials: true
  },
  auth: {
    strategies: {
      local: {
        scheme: 'refresh',
        localStorage: {
          prefix: 'auth.'
        },
        email: {
          prefix: 'email.',
          property: 'email' 
        },
        token: {
          prefix: 'access.',
          property: 'access',
          maxAge: 60*5,
          type: 'Bearer'
        },
        refreshToken: {
          prefix: 'refresh.',
          property: 'refresh',
          data: 'refresh',
          maxAge: 60 * 60
        },
        user: {
          property: 'user',
          autoFetch: true
        },
        endpoints: {
          login: { url: '/login/', method: 'post'},
          refresh: { url: 'api/token/refresh/', method: 'post' },
          user: { url: '/user/', method: 'get' },
          logout: { url: '/logout/', method: 'post'}
        },
      }
    }
  },

  // Build Configuration: https://go.nuxtjs.dev/config-build
  build: {
  }
}

Login.vue:

<script>
  export default {
    name: 'Login',
    components: {
      Notification,
    },
    layout: 'clean',

    data() {
      return {
        user: {
          username: '',
          password: ''
        },
        error: null
      }
    },

    methods: {
      async login() {
          await this.$auth.loginWith('local', { data: this.user });
   
      }
    }
  }
</script>

Попробуйте это :

@api_view(['GET'])
@permission_classes([IsAuthenticated])
def userview(request):
    try:
        # can't get cookies :(
        username= request.COOKIES["user"]["username"] // change here
    except:
        return Response({'mess':'ops'})
........

Вернуться на верх