当我在本地主机上安装应用程序和服务器时,一切正常。但是,由于我将其托管在这些平台上,因此不再设置客户端Cookie。
Server.js (托管在Heroku上的)
var express = require("express");
var bodyParser = require("body-parser");
var logger = require("morgan");
var methodOverride = require("method-override");
var cors = require("cors");
var cookieParser = require("cookie-parser");
var session = require("express-session");
var bcrypt = require("bcrypt-nodejs");
var appURL = "https://xxxxxxxxxxxxx.firebaseapp.com";
var app = express();
app.use(logger("dev"));
app.use(bodyParser.json());
app.use(methodOverride());
app.use(cors({origin: appURL, credentials: true, methods: "GET,POST"}));
app.use(cookieParser());
app.all('*', function(req, res, next) {
res.setHeader("Access-Control-Allow-Origin", appURL);
res.setHeader("Access-Control-Allow-Headers", "X-Requested-With");
res.setHeader("Access-Control-Allow-Credentials", true);
next();
});
app.set("trust proxy",1);
app.use(session({
name: "random_session",
secret: "yryGGeugidx34otGDuSF5sD9R8g0Gü3r8",
resave: false,
saveUninitialized: true,
cookie: {
path: "/",
secure: true,
domain: ".firebaseapp.com",
httpOnly: true
}
}));
Login.ts (我在Firebase App中发出请求的文件)
let data = {
email: this.loginField,
password: this.passwordField
}
var xhr = new XMLHttpRequest();
xhr.open("POST","https://xxxxxxxxxx.herokuapp.com/login", true);
xhr.withCredentials = true;
var change = () => {
if(xhr.readyState == XMLHttpRequest.DONE) {
if(xhr.status != 0) {
if (xhr.status != 401 && xhr.status != 404) {
//Login Successfull
} else {
//Login Fail
}
} else {
//Network error
}
}
}
xhr.onreadystatechange = change;
xhr.setRequestHeader("Access-Control-Allow-Origin","https://xxxxxxxxxx.firebaseapp.com");
xhr.setRequestHeader("Access-Control-Allow-Credentials", "true");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send(JSON.stringify(data));
我没有在客户端设置cookie怎么办?我在Cookie中使用了错误的域还是在Access-Control-Allow-Origin中使用了错误的域?
谢谢!
答案 0 :(得分:0)
我解决了问题:我从Firebase中删除了Ionic App,并且现在也将其托管在Heroku上。这没有解决问题。 Cookie仍未在我的浏览器中设置。
在我的Node.js
中,我删除了域:
app.use(session({
name: "random_session",
secret: "yryGGeugidx34otGDuSF5sD9R8g0Gü3r8",
resave: false,
saveUninitialized: true,
cookie: {
path: "/",
secure: true,
//domain: ".herokuapp.com", REMOVE THIS HELPED ME (I dont use a domain anymore)
httpOnly: true
}
}));
删除域解决了我的问题。
如果您将NodeJS服务器托管在Heroku上,并将PWA也托管在Heroku上,则需要在Safari设置中禁用 Prevent Cross-Site Tracking
。
答案 1 :(得分:0)
对我来说添加 sameSite = 'none';在 cookie 对象中起作用。