cookies doesn't set after deploying it to render and my react app on vercel #5423
Replies: 19 comments 24 replies
-
It seems like you're having trouble with setting cookies in your Express.js application. Here are a few things you could check: Secure Flag: You're setting the secure flag to true in your cookie options. This means the cookie will only be sent over HTTPS. If you're testing locally over HTTP, the cookie will not be set. Try setting secure to false for local testing. SameSite Attribute: You're setting sameSite to "none", which means the cookie will be sent in all contexts, i.e in responses to both first-party and cross-origin requests. If your site isn't served over HTTPS, this setting can cause cookies to be rejected. Try setting sameSite to "lax" or "strict" for testing. Domain Attribute: The domain attribute in the cookie determines which hosts are allowed to receive the cookie. If you're testing locally, this could be the issue. Try removing the domain attribute for testing. Browser Settings: Ensure your browser is configured to accept cookies. Some browsers have settings that can block cookies, especially third-party ones. Check CORS Settings: Make sure your CORS settings are correct. You need to allow credentials in your CORS middleware, which you're already doing. But also ensure the client is making requests with credentials: 'include'. Here's how you can modify your cookie options for local testing: const options = {
httpOnly: true,
secure: false, // set to false for local testing
sameSite: "lax", // try "lax" or "strict" for testing
// remove domain for local testing
} Remember to revert these changes for production. |
Beta Was this translation helpful? Give feedback.
-
@pradeepbgs did you find any solution for this.. |
Beta Was this translation helpful? Give feedback.
-
@pradeepbgs I am facing the exact same issue. Did you find any solution? |
Beta Was this translation helpful? Give feedback.
-
@iamrommy , @phamtronghieu2002 try doing like this res.cookie('accessToken', token, { i added this 2 things secure:true, sameSite:'none', and it worked :). |
Beta Was this translation helpful? Give feedback.
-
Consider setting samesite to lax. None is pretty liberal. https://stackoverflow.com/questions/59990864/what-is-the-difference-between-samesite-lax-and-samesite-strict#59995877 |
Beta Was this translation helpful? Give feedback.
-
Hey folks! Did you ask vercel for support on this? They are a for profit company with a paid support team. This forum is run by unpaid volunteers. I see that this discussion is getting a lot of folks in it recently and I don't have time to investigate but if your problem is on vercel then I would start with asking them. |
Beta Was this translation helpful? Give feedback.
-
Does somebody know how to get render support on it. Because the problem is clearly with the backend. But when i set cookie with domain attribute then the cookie is not even set ( or may be it is being set but for wrong Url). Like this :- |
Beta Was this translation helpful? Give feedback.
-
Did anyone find the solution , this is my cookie options- const cookieOptions = { |
Beta Was this translation helpful? Give feedback.
-
Httponly = true means the cookie will be sent on requests, but the front end JavaScript code won’t be able to access it. how do you know the cookie is not being sent? Are you not seeing it on the backend in subsequent requests? Or are you trying to read document.cookie in the front end? |
Beta Was this translation helpful? Give feedback.
-
I am facing the same issue. I have deployed the API on render but the token is not setting in cookies when I have deployed the frontend application on render as well. The token is set in cookies when running locally. Please explain as to why this happens & what can I do? @phamtronghieu2002 @Vyomrana02 This is the login controller: `
` This is the index.js code : ` app.use(cors()); app.get('/',(req,res)=>{ `import React, { useState } from 'react' const Login = () => { const navigate = useNavigate(); const domain = "localhost"; const handleLogin = async () => { |
Beta Was this translation helpful? Give feedback.
-
As a solution what u can do is build your project and serve it as static files in your backend .This should resolve cookies issue as I didn't had much time that's why i just used this method. |
Beta Was this translation helpful? Give feedback.
-
I recommend searching for "cookies not set using Render", since everyone here seems to be having problems with Render. |
Beta Was this translation helpful? Give feedback.
-
I just figuired it out the issue in my code Problem Statement: When I try to logout it is logging me in automitaclly, I was not able to replace the tooken with empty string Solution: I tried adding
I added same code when saving the auth token in cookie |
Beta Was this translation helpful? Give feedback.
-
Thank you for the solution. i spend 6 hours to find the solution all over the internet.Now Just Simple sameSite: "none" solve all the problem.. 😢😢 |
Beta Was this translation helpful? Give feedback.
-
I had the same issue, I had backend server in express js deployed in AWS and react frontend deployed in Vercel, cookies were working fine in localhost but in production they couldn't be sent to frontend when I login. |
Beta Was this translation helpful? Give feedback.
-
I was having the same issue, after reading this thread I came to know about the Basically the problem in my case was that, if the Thanks for your insights🙌 |
Beta Was this translation helpful? Give feedback.
-
Here is my code I am I am getting the same issue |
Beta Was this translation helpful? Give feedback.
-
I didn't find the solution but I did tackle this problem. This solution is just for those using Next js for the front end.
import { NextRequest, NextResponse } from "next/server";
import { cookies } from "next/headers";
export async function POST(req: NextRequest) {
const cookieStore = cookies();
cookieStore.delete("cookie-name"); // you can delete or create cookie
return NextResponse.json({
success: true,
status: 200,
message: "user logged out successfully",
});
}
|
Beta Was this translation helpful? Give feedback.
-
app.use(session({ Reasons why cookies cannot be set on the client side reactjs deploy vercel -> unable to send connect.sid from client to server to authenticate user -> add secure: true, sameSite: 'none' -> works |
Beta Was this translation helpful? Give feedback.
-
so here is my app.js where you can see how i have set CORS
app.use(cors({ origin: 'https://fsvideo.vercel.app', credentials: true, }))
and here is my login controller -
`const loginUser = asyncHandler(async(req, res) => {
const {username, email, password} = req.body
const {accesToken, refreshToken} = await generateAccessAndRefreshToken(user._id)
const loggedInUser = await User.findById(user._id)
.select("-password -refreshToken")
const options = {
httpOnly: true,
secure: true,
sameSite: "none",
domain: "https://fsvideo.vercel.app"
}
return res.status(200)
.cookie("accessToken", accesToken, options)
.cookie("refreshToken", refreshToken, options)
.json(
new apiResponse(
200,
{
user: loggedInUser,
accessToken: accesToken,
refreshToken: refreshToken,
},
"User logged in successfully"
)
)
})
`
im not able to set cookie on browser , when i log in it shows cookie on application panel and then goes away.
please help
Beta Was this translation helpful? Give feedback.
All reactions