Comments (4)
A bit tricky to see the issue in your example, because, straight up navigating to the landing page, sets a cookie too. That being said, I think something like this should work:
import { NextRequest, NextResponse } from "next/server";
export async function middleware(request: NextRequest) {
const expires = new Date();
expires.setMinutes(expires.getMinutes() + 60);
const cookie = {
httpOnly: true,
name: "random_string",
value: Math.random().toString(),
expires,
};
request.cookies.set(cookie.name, cookie.value);
let response = NextResponse.next({
request: {
headers: new Headers(request.headers),
},
});
response.cookies.set(cookie);
return response;
}
from next.js.
I mean, I put it together from information present in the docs.
That link documents how to forward modified headers.
And that it is mentioned that you can set cookies on the incoming request:
For incoming requests, cookies comes with the following methods: get, getAll, set, and delete cookies. You can check for the existence of a cookie with has or remove all cookies with clear.
On the same page as the link above.
from next.js.
@icyJoseph It's kind of difficult to reproduce, you have to simulate the "access_token" (random_string) cookie expiring when calling server action, that's why I said to clear the cookies in the first step.
I tested your example and it worked, but is it the right way to do this? Looks like I'm back to the issue #49442
from next.js.
I believe this issue is impacting supabase sever side auth on NextJS supabase/ssr#36
from next.js.
Related Issues (20)
- Document not defined, NextJS cant throw traces HOT 1
- Global error not working if triggered in sub-path in version 13.5.5
- Wrong generated favico metada links HOT 1
- notFound() does not replace robots tag HOT 1
- Web workers do not work when runtime is edge HOT 4
- Google Analytics package in @next/third-parties/google doesn't track data if multiple GA4 streams are added HOT 1
- Docs: `pages/optimizePackageImports` points at `compress` instead HOT 3
- Loading.tsx files dont works with nested routes
- Unable to Fetch Data from Backend Server in Next.js SSR Production with Docker HOT 2
- with-next-translate example broken (html, body missing in layout; 404 pages)
- Stale search parameters when using an anchor/fragment in the `as` property of `Link`
- Docs: 在next14.2.5版本中,怎么监听路由的的变化 HOT 1
- Response.redirect with relative URLs not working for Route Handlers HOT 1
- Error: Automatic publicPath is not supported in this browser
- Form error with server action on route change HOT 2
- Gravatar Error On Build HOT 2
- [Turbopack] [Dev] Error: Could not parse module '/app-router-context.js' with `next-auth`
- Docs: Missing Instructions for Configuring next.config.mjs to Use next/mdx Package HOT 1
- Error instance not preserved in `onRequestError`
- Material UI: Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from next.js.