axios 从quasar ssr发送cookie到laravel后端

xvw2m8pv  于 2023-10-18  发布在  iOS
关注(0)|答案(2)|浏览(137)

我正在构建一个Web应用程序,前端使用Quasar SSR,后端使用Laravel
对于身份验证,我使用新的Laravel Sanctum包,它使用cookie进行用户身份验证。
在SPA中构建应用程序,我对用户进行身份验证没有问题。不幸的是,在SSR模式下,没有cookie发送到服务器,因此无法进行用户身份验证。
我使用axios来处理一个请求。我把它放在一个 Boot 文件里。
有人能帮我把cookies发送到后台吗?
编辑添加一些精确度。我使用nginx作为Web服务器,我的配置如下:

server {
    listen 80;
    listen [::]:80;
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    ssl_certificate /var/www/certs/www.mysite/fullchain.pem;
    ssl_certificate_key /var/www/certs/www.mysite/privkey.pem;
    ssl_protocols TLSv1.2;

    root /var/www/next/mysite/public;
    index index.php index.html index.htm index.nginx-debian.html;

    server_name next.mysite.net;

    location / {
        #try_files $uri $uri/ /index.php?$args;
    proxy_pass    http://localhost:8080;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
    location /storage/ {
    }
    location /vendor/ {
    }
    location /server {
        try_files $uri $uri/ /index.php?$args;
    }
    location /prequel-api {
        try_files $uri $uri/ /index.php?$args;
    }
    location /api {
        try_files $uri $uri/ /index.php?$args;
    }
    location /graphql {
        try_files $uri $uri/ /index.php?$args;
    }
    location /sanctum {
        try_files $uri $uri/ /index.php?$args;
    }
    location ~ [^/]\.php(/|$) {
        #try_files $uri =404;
        fastcgi_split_path_info ^(.+?\.php)(/.*)$;
        #if (!-f $document_root$fastcgi_script_name) {
        #   return 404;
        #}

        # Mitigate https://httpoxy.org/ vulnerabilities
        fastcgi_param HTTP_PROXY "";

        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_index index.php;

        # include the fastcgi_param setting
        include fastcgi_params;

        # SCRIPT_FILENAME parameter is used for PHP FPM determining
        #  the script name. If it is not set in fastcgi_params file,
        # i.e. /etc/nginx/fastcgi_params or in the parent contexts,
        # please comment off following line:
        fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
    }

    location ~ /\.ht {
        deny all;
    }
}
import Axios from "axios";
import { boot } from "quasar/wrappers";
import { Cookies } from "quasar";
const axios = Axios.create({
 baseUrl: "https://next.mysite.net"
});
axios.defaults.withCredentials = true;
axios.interceptors.response.use(
  response => {
    return response.data;
  },
  error => {
    return Promise.reject(error.response);
  }
);
export default boot(async ({ Vue, ssrContext }) => {
  if (process.env.SERVER) {
    //Check if cookies are available
    var cookies = JSON.stringify(Cookies.parseSSR(ssrContext).getAll()).replace(
      /[{}]/g,
      ""
    );
    console.log("cookies: ", cookies);
  }

  Vue.prototype.$axios = axios;
});

export { axios };
z9gpfhce

z9gpfhce1#

我通过在创建示例时用cookie扩展Axios示例解决了类似的问题
//boot/axios.js(回调函数内部)

if (ssrContext) {
    const cookies = ssrContext.$q.cookies.getAll();

    instance.interceptors.request.use(function (config) {
      const cookiesSet = Object.keys(cookies).
        reduce((prev, curr) => prev + curr + '=' + cookies[curr] + ';', '');

      if (cookiesSet.length > 0) {
        config.headers.Cookie = cookiesSet;
      }

      return config;
    });
  }
nwlqm0z1

nwlqm0z12#

上面的例子将工作,但它将在每个调用上添加一个回调元素,这将导致性能损失和内存泄漏,我会添加一个检查,处理程序只添加一次

相关问题