You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If your Route Handler needs to both refresh the session **and** return a `NextResponse` you fully control (e.g., to set additional cookies with a `Domain` or `SameSite` attribute), use the explicit `getAccessToken(req, res, options)` signature. This writes the refreshed session directly onto the `NextResponse` you pass, so all `Set-Cookie` headers — session and custom — are consolidated on the one response object you return.
// 3. Set any additional cookies on the same response object.
954
+
res.cookies.set("my-cookie", "value", {
955
+
domain: ".example.com",
956
+
secure: true,
957
+
sameSite: "lax"
958
+
});
959
+
960
+
// 4. Return the single response — it now carries both the refreshed
961
+
// session Set-Cookie headers and your custom cookie.
962
+
returnres;
963
+
}
964
+
```
965
+
966
+
> [!IMPORTANT]
967
+
> Calling `getAccessToken({ refresh: true })` (without `req`/`res`) in a Route Handler writes the refreshed session through Next.js's internal cookie store, **not** onto a `NextResponse` you construct. If you then build a `new NextResponse()` and add cookies to it, that response will be missing the refreshed session cookies. Always pass `req` and `res` explicitly when you need all cookies on the same response object.
968
+
932
969
**Pages Router (getServerSideProps, API Routes):**
933
970
934
971
When calling `getAccessToken` with request and response objects (from `getServerSideProps` context or an API route), the options object is passed as the third argument.
0 commit comments