从quasar ssr向laravel后端发送cookie
我正在构建一个用于前端和后端的web应用程序 对于身份验证,我使用使用cookies进行用户身份验证的新包 在SPA中构建应用程序,我可以对用户进行身份验证。不幸的是,在SSR模式下,不会向服务器发送cookie,因此无法进行用户身份验证 我使用axios处理ajax请求。我在启动文件中设置了它 有人能帮我把cookies发送到后端吗 编辑以添加一些精度。我使用nginx作为web服务器,配置如下:从quasar ssr向laravel后端发送cookie,laravel,vue.js,cookies,axios,server-side-rendering,Laravel,Vue.js,Cookies,Axios,Server Side Rendering,我正在构建一个用于前端和后端的web应用程序 对于身份验证,我使用使用cookies进行用户身份验证的新包 在SPA中构建应用程序,我可以对用户进行身份验证。不幸的是,在SSR模式下,不会向服务器发送cookie,因此无法进行用户身份验证 我使用axios处理ajax请求。我在启动文件中设置了它 有人能帮我把cookies发送到后端吗 编辑以添加一些精度。我使用nginx作为web服务器,配置如下: server { listen 80; listen [::]:80;
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;
}
}
我通过在创建axios实例时使用cookies扩展该实例,解决了类似的问题 //boot/axios.js(内部回调函数)
我通过在创建axios实例时使用cookies扩展该实例,解决了类似的问题 //boot/axios.js(内部回调函数)
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 };
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;
});
}