Colibri
Colibri

Reputation: 1195

Why Nuxt making multiple request for the "user" endpoint?

There is an Express server and a Nuxt client. Nuxt version is 2.15.7.

Entire auth configuration:

// nuxt.config.js
auth: {
  plugins: [
    {
      src: '~/plugins/axios',
      ssr: true
    },
    {
      src: '~/plugins/auth'
    }
  ],
  cookie: {
    prefix: 'auth.',
    options: {
      path: '/',
      secure: process.env.NODE_ENV === 'production' ? true : false
    }
  },
  localStorage: {
    prefix: 'auth.'
  },
  vuex: {
    namespace: 'auth'
  },
  strategies: {
    local: {
      scheme: 'refresh',
      token: {
        property: 'accessToken',
        maxAge: 900,
        global: true,
        name: 'Authorization',
        type: 'Bearer'
      },
      refreshToken: {
        property: 'refreshToken',
        data: 'refreshToken',
        maxAge: 5184000
      },
      user: {
        property: 'user',
        autoFetch: false
      },
      endpoints: {
        login: {
          url: '/user/sign_in',
          method: 'post'
        },
        logout: {
          url: '/user/sign_out',
          method: 'delete'
        },
        refresh: {
          url: '/user/refresh',
          method: 'get'
        },
        user: {
          url: '/user/profile',
          method: 'get'
        }
      }
    }
  },
  redirect: {
    login: '/auth/sign_in',
    logout: '/',
    callback: '/auth/sign_in',
    home: '/'
  }
}

When I refresh the page in the browser, I see this in the browser log:

enter image description here

This message comes from here:

// plugins/axios.ts
import { AxiosRequestConfig } from 'axios'

export default function ({ $axios, }: any) {
  $axios.onRequest((config: AxiosRequestConfig) => {
    console.log('Making request to ' + config.url)
  })
}

There are also two requests in the server logs. But in the first request I can get, for example, cookies, and in the second one comes this:

// console.log(req.cookies)

[Object: null prototype] {}

Can you please tell me why there are two requests?

Upvotes: 1

Views: 732

Answers (1)

Colibri
Colibri

Reputation: 1195

The problem was caused by the back end returning this after sign in:

{
  "is": 1
}

And should return this:

{
  "user": {
    "is": 1
  }
}

😬

After I added the "user" object, nuxt auth accepted the information and started working correctly.

Upvotes: 2

Related Questions