Replies: 15 comments 17 replies
|
Hi, A couple of follow up questions:
|
|
yes i believe you are right because i copied and pasted a lot of this code from another codebase I deployed on vercel without issue the login id admin / password |
|
Ok I figured out the issue. I think this must be a bug. The page I forward to after I authenticate has a link to logout:
For some reason nextjs is causing the browser to load that link in the background, thus logging the person out which results in clearing cookie values and setting the max-age to 0. That's why when I looked at my cookies they had no value and the expire time was "when browser session ends". I discovered this by looking at my _middleware.js output statements and seeing that the path /logout was being executed automatically! Of course this only happens on vercel and not my local host. When I remove the link tag it has no issue. I did some quick googling and this seems to be the reason: |
|
nope not at all. where do i specify prefetch to false? |
|
I have the same issue. I'm setting a cookie in an isolated API, in localhost the cookie is created succesfully but it doesn't work in vercel app. I'm using next version 12.5. I set http only and secure cookie and all is working in secure environments. |
|
have you found any solution? |
|
so for me, it was a while since i got back into webdev. i was authorizing using GET instead of POST, and as a result the responses were cached with GET resulting in no cookie being set at all |
|
have you found any solution? |
|
I had the same problem but once deployed both backend and client to use HTTPS it worked fine. |
|
This ended up being a caching issue for me as well. I was letting Vercel cache the page which was preventing the cookie from reaching my server if the cookie was set after the page was already cached. |
|
Use POST route insted of GET route for logout, issue will be resolved |
|
guys , so if you are using cookies in production , need to change some of the things in the products code. given below this thing should be their in the code and make sure both the client and server side code is in production and also make sure the cors should be added in the backend code example given below- app.use(cors({credentials: true,origin: |
|
I did this thing for generating and using httpOnly Cookie app.set('trust proxy', 1); app.use( |
|
My problem is that the browser sees that my cooike as third party cooike how can i solve it without change in browser setting |
|
If you're hitting this issue — cookies work on localhost but disappear in production — check if you have a Root cause: Next.js The fix: Make logout POST-only (not GET). // POST only — never GET
export async function POST(request: Request) {
const res = NextResponse.redirect(new URL('/login', request.url), 303);
clearSessionCookie(res);
return res;
}// <form> instead of <Link>
<form action="/logout" method="POST">
<button type="submit">Logout</button>
</form>Full write-up with debugging timeline: #60581 (comment) |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
UPDATE:
On localhost, I have no problems. I tried with http and https on localhost, the result is the same.
On Vercel, it is another story. It actually seems to set the cookie the first time I visit the domain. However, even this first time the expiration is "When the browser session ends" even though I set 'max-age'. After I close the browser, the cookies disappear and , I am not able to set cookies again.
I set my cookies like this:
res.setHeader("set-cookie", [serialisedAT, serialisedRT]);Here is the value of the serialized cookies that I print out right after I set via res.getHeaders()
["AccessToken=eyJhbGciOiJIUzI1NiJ9.eyJ1c2VySWQiOiI4ODhhZDY3Ni0zNDFhLTQ4YzQtOWQxMi0wMTE5M2Q3ZDJlZDAiLCJpYXQiOjE2NTEwMDk0ODEsImV4cCI6MjU5MjAwMDAwMH0.VkbkHaOcf2ou0A40FNTg3zI8quA9bLBpn2PJRBxuYDA; Max-Age=2592000; Path=/; HttpOnly; Secure; SameSite=Strict","RefreshToken=eyJhbGciOiJIUzI1NiJ9.eyJ1c2VySWQiOiI4ODhhZDY3Ni0zNDFhLTQ4YzQtOWQxMi0wMTE5M2Q3ZDJlZDAiLCJpYXQiOjE2NTEwMDk0ODEsImV4cCI6NTE4NDAwMDAwMH0.vVhmjnpTYTknAnkHXAHtA_yVeIGiYvOpsCg0E6nSB5s; Max-Age=5184000; Path=/; HttpOnly; Secure; SameSite=Strict"]
All reactions