首页 > 解决方案 > 如果没有 cookie,则 Next.js 重定向

问题描述

有没有办法仅在没有 cookie 的情况下将用户从结帐重定向到加入页面user_token。Next.js 只允许设置字符串和 undefined as value,但我需要验证没有 cookie:

  redirects: async () => [
    {
      source: '/checkout',
      has: [
        {
          type: 'cookie',
          key: 'user_token',
          value: '',
        },
      ],
      permanent: true,
      destination: '/join',
    },
  ],

我尝试将正则表达式用于空字符串,但它不起作用:

  redirects: async () => [
    {
      source: '/checkout',
      has: [
        {
          type: 'cookie',
          key: 'user_token',
          value: '(^$)',
        },
      ],
      permanent: true,
      destination: '/join',
    },
  ],

标签: reactjsregexcookiesnext.js

解决方案


推荐阅读