[英]Express session not storing cookies on ios/safari browser, but works fine on android
I have been facing this issue for weeks now, am trying to login to my app on IOS device, it turns out that express-session did not store cookies in the browser, for this reason, I was unable to login.我已经面临这个问题好几个星期了,我试图在 IOS 设备上登录我的应用程序,事实证明 express-session 没有在浏览器中存储 cookies,因此我无法登录。 But I was able to login on my android device and laptop.但我能够登录我的 android 设备和笔记本电脑。 But am still unable to access the app on safari on my android device.但是我仍然无法在我的 android 设备上访问 safari 上的应用程序。
My frontend stack is Nextjs, and Nodejs with express for my backend.我的前端堆栈是 Nextjs,后端是带有 express 的 Nodejs。 Backend was deployed on heroku and frontend was deployed on.netlify后端部署在heroku,前端部署在.netlify
Pls how do I solve this problem.请问我该如何解决这个问题。 Here is what my code looks like.这是我的代码的样子。 I did like my app to work on all browser.我确实希望我的应用程序可以在所有浏览器上运行。
============================Server.js===================================== ============================Server.js=================== ==================
import express from "express"
import rootRoute from "./src/root_Route.js"
import cookieParser from "cookie-parser"
import passport from "passport"
import connectPgSimple from "connect-pg-simple"
import session from "express-session"
import dotenv from "dotenv"
import cors from "cors"
import "./src/LIB/DB-Client.js"
import "./src/PASSPORT_STRATEGY/google-auth-strategy.js"
import "./src/PASSPORT_STRATEGY/facebook-auth-strategy.js"
import { scheduleJob } from "node-schedule"
import pool from "./src/LIB/DB-Client.js"
dotenv.config()
const app = express()
const connection = process.env.PRODUCTION !== "production" ? process.env.DEV_DATABASE_URL : process.env.DATABASE_URL
app.use(
cors({
origin: ["http://localhost:3000", "https://nairaonly-frontend.netlify.app"],
credentials: true,
methods: "GET, PUT, POST, DELETE",
optionsSuccessStatus: 200,
})
)
const PgStore = connectPgSimple(session)
const store = new PgStore({ conString: connection, schemaName: "hidden", createTableIfMissing: true })
app.use(express.json())
app.use(cookieParser())
app.set("trust proxy", 1)
app.use(
session({
store: store,
secret: process.env.SESSION_SECRET,
saveUninitialized: false,
resave: true,
proxy: true,
cookie: {
maxAge: 1000 * 60 * 60 * 24,
httpOnly: true,
sameSite: process.env.NODE_ENV === "production" ? 'none' : 'lax',
secure: process.env.NODE_ENV === "production" ? true : false,
},
})
)
app.get("/", (req, res) => {
res.send("API Running...")
})
app.use(passport.initialize())
app.use(passport.session())
rootRoute(app)
const PORT = process.env.PORT || 4000
app.listen(PORT, (req, res) => console.log(`Server running on PORT:${PORT}...`))
I get this solved by registering a domain name, and added a subdomain which was used for my backend and the domain itself for my frontend.我通过注册域名解决了这个问题,并添加了一个用于我的后端的子域和用于我的前端的域本身。 I then point the DNS to each other.然后我将 DNS 指向对方。 This solve the issue这解决了问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.