窗口不是从NUXT auth-next oauth2scheme.logout()function定义的错误

发布于 2025-02-13 04:53:30 字数 3090 浏览 0 评论 0原文

在我的项目中,我使用nuxt auth-next。为了验证当前保存的cookie,我正在检查NuxtServerinit中的以下条件。

export const actions = {
  async nuxtServerInit({ commit }, { app }) {
    if (app.$auth.loggedIn) {
      const token = app.$auth.strategies.social.token.get()
      commit('SET_TOKEN', token)
    } else {
      await app.$auth.logout('social')
    }
  },
}

一旦调用此注销方法,就会发生以下错误。 如果我评论此行,“未定义”的错误将消失,

} else {
         // await app.$auth.logout('social')
        }

但是在我的情况下,如果令牌无效,我想从系统中注销用户。 我的NUXT配置如下,

export default {

  loading: '~/components/loaders/Loader.vue',

  // Global CSS: https://go.nuxtjs.dev/config-css
  css: ['~/assets/css/main.css'],

  // Plugins to run before rendering page: https://go.nuxtjs.dev/config-plugins
  plugins: [
    '~/plugins/axios.js',
    '~/plugins/loader.js',
    '~/plugins/vue-tooltip.js',
    {
      src: '@/plugins/notifications.js',
      mode: 'client',
    },

    {
      src: '@/plugins/v-date-picker.js',
      mode: 'client',
    },
  ],

  // 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',
    // https://go.nuxtjs.dev/tailwindcss
    '@nuxtjs/tailwindcss',
  ],

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

  router: {
    // middleware: ['auth', 'servey-middleware', 'routing-middleware'],
    middleware: ['auth', 'servey-middleware'],
  },

  auth: {
    redirect: {
      login: '/',
      callback: '/',
      logout: `/`,
      home: '/surveys',
    },
    strategies: {
      azureAD: {
        scheme: 'oauth2',
        endpoints: {
          authorization: `${process.env.SIGNIN_POLICY}`,
          token: `${process.env.SIGNIN_POLICY}`,
          userInfo: '/',
          logout: `${process.env.SIGNIN_POLICY}`,
        },
        token: {
          property: 'access_token',
          type: 'Bearer',
          maxAge: 1800,
        },
        refreshToken: {
          property: 'refresh_token',
          maxAge: 60 * 60 * 24 * 30,
        },
        responseType: 'code',
        grantType: 'authorization_code',
        redirectUri: `${process.env.REDIRECT_URI}`,
        logoutRedirectUri: `${process.env.LOGOUT_URL}`,
        clientId: `${process.env.CLIENT_ID}`,
        scope: ['openid'],
        autoLogout: true,
        prompt: 'login',
      },
    },
  },

  // Axios module configuration: https://go.nuxtjs.dev/config-axios
  axios: {
    baseURL: `${process.env.API_HOST}/api/v1/cont-mgt`,
  },

  // Build Configuration: https://go.nuxtjs.dev/config-build
  build: {
    transpile: ['v-date-picker', 'vue-tooltip'],
  },
}

这里可能是什么问题或如何解决此问题?

In my project, I use nuxt auth-next. To validate the current saved cookie I'm checking the following conditions in nuxtServerInit.

export const actions = {
  async nuxtServerInit({ commit }, { app }) {
    if (app.$auth.loggedIn) {
      const token = app.$auth.strategies.social.token.get()
      commit('SET_TOKEN', token)
    } else {
      await app.$auth.logout('social')
    }
  },
}

once this logout method is called, the following error will occur.
enter image description here
"window is not defined" error will be gone if I comment this line,

} else {
         // await app.$auth.logout('social')
        }

But in my case, I want to log out the user from the system if the token is not valid.
my nuxt configuration is as below,

export default {

  loading: '~/components/loaders/Loader.vue',

  // Global CSS: https://go.nuxtjs.dev/config-css
  css: ['~/assets/css/main.css'],

  // Plugins to run before rendering page: https://go.nuxtjs.dev/config-plugins
  plugins: [
    '~/plugins/axios.js',
    '~/plugins/loader.js',
    '~/plugins/vue-tooltip.js',
    {
      src: '@/plugins/notifications.js',
      mode: 'client',
    },

    {
      src: '@/plugins/v-date-picker.js',
      mode: 'client',
    },
  ],

  // 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',
    // https://go.nuxtjs.dev/tailwindcss
    '@nuxtjs/tailwindcss',
  ],

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

  router: {
    // middleware: ['auth', 'servey-middleware', 'routing-middleware'],
    middleware: ['auth', 'servey-middleware'],
  },

  auth: {
    redirect: {
      login: '/',
      callback: '/',
      logout: `/`,
      home: '/surveys',
    },
    strategies: {
      azureAD: {
        scheme: 'oauth2',
        endpoints: {
          authorization: `${process.env.SIGNIN_POLICY}`,
          token: `${process.env.SIGNIN_POLICY}`,
          userInfo: '/',
          logout: `${process.env.SIGNIN_POLICY}`,
        },
        token: {
          property: 'access_token',
          type: 'Bearer',
          maxAge: 1800,
        },
        refreshToken: {
          property: 'refresh_token',
          maxAge: 60 * 60 * 24 * 30,
        },
        responseType: 'code',
        grantType: 'authorization_code',
        redirectUri: `${process.env.REDIRECT_URI}`,
        logoutRedirectUri: `${process.env.LOGOUT_URL}`,
        clientId: `${process.env.CLIENT_ID}`,
        scope: ['openid'],
        autoLogout: true,
        prompt: 'login',
      },
    },
  },

  // Axios module configuration: https://go.nuxtjs.dev/config-axios
  axios: {
    baseURL: `${process.env.API_HOST}/api/v1/cont-mgt`,
  },

  // Build Configuration: https://go.nuxtjs.dev/config-build
  build: {
    transpile: ['v-date-picker', 'vue-tooltip'],
  },
}

What could be wrong here or how can I solve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文