i put my page online on vercel hosting. I noticed i can't set cookies on safari, my page works on opera and chrome but on safari im getting 401 when im trying to login. I tried every tip i've found but nothing works. At the beggining i've had
res.cookie('AuthToken', token, {
maxAge: 1800 * 1000,
httpOnly: true,
sameSite: 'none',
secure: true,
})
and it was working on opera and chrome, then i found Safari not include COOKIE to second CORS request so i change my code to:
const token = generateAccessToken(user.toJSON())
res.setHeader('Set-Cookie',
`AuthToken=${token}; max-age=43200; httpOnly; sameSite=none; expires=Wed, 15 Mar 2023 21:17:55 GMT; secure; Domain=xxx.vercel.app`)
and it still doesnt work on safari. I tried lower-case letters and Capital letters, like SameSite=None, Secure, Max-Age. My main file server.ts looks like this:
config()
const app = express()
const mongoConnectionString = mongoDb.url
const localhost = process.env.ORIGIN
const corsOptions = {
origin: localhost,
credentials: true,
}
mongoose.connect(mongoConnectionString, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
})
const database = mongoose.connection
database.on('error', (error) => {
console.log(error)
})
database.once('connected', () => {
console.log('Database Connected')
})
app.set('Access-Control-Allow-Credentials', true)
app.set('Access-Control-Allow-Origin', true)
app.use(cookieParser())
app.use(cors(corsOptions))
app.use(express.json())
app.use(userAccountRouter)
app.use(authenticateToken)
app.use(apiRoute)
The main problem here is i don't have macOs and i cant see devtools on safari, im checking if page is working on my phone. The solution is probably easy but after a few days i have enough. My friend after cloning my repo add this:
res.setHeader('Set-Cookie', `AuthToken=${token}; HttpOnly; max-age: 43200; SameSite=None; Secure`)
and it has started working on his safari locally, when i push this code it still doesnt work on online page.
If you want more information about code etc. please type. I wanted to start looking for a job as a junior and wanted to put my code online but this is blocking me indeed.