logto-io / js

πŸ€“ Logto JS SDKs.
https://docs.logto.io/quick-starts/
MIT License
69 stars 41 forks source link

await cookies #851

Open Dakuan opened 5 days ago

Dakuan commented 5 days ago

Hi! πŸ‘‹

Firstly, thanks for your work on this project! πŸ™‚

Today I used patch-package to patch @logto/next@3.7.1 for the project I'm working on.

Here is the diff that solved my problem:

diff --git a/node_modules/@logto/next/lib/server-actions/client.js b/node_modules/@logto/next/lib/server-actions/client.js
index 09129a6..d44bdd9 100644
--- a/node_modules/@logto/next/lib/server-actions/client.js
+++ b/node_modules/@logto/next/lib/server-actions/client.js
@@ -64,13 +64,13 @@ class LogtoClient extends LogtoNextBaseClient {
             encryptionKey: this.config.cookieSecret,
             cookieKey: `logto:${this.config.appId}`,
             isSecure: this.config.cookieSecure,
-            getCookie: (...args) => {
-                return cookies().get(...args)?.value ?? '';
+            getCookie: async (...args) => {
+                return (await cookies()).get(...args)?.value ?? '';
             },
-            setCookie: (...args) => {
+            setCookie: async (...args) => {
                 // In server component (RSC), it is not allowed to modify cookies, see https://nextjs.org/docs/app/api-reference/functions/cookies#cookiessetname-value-options.
                 if (!ignoreCookieChange) {
-                    cookies().set(...args);
+                    (await cookies()).set(...args);
                 }
             },
         });

This issue body was partially generated by patch-package.

charIeszhao commented 4 days ago

@wangsijie Do you think the await is necessary? Are cookies async?

Dakuan commented 4 days ago

yes

[Error: Route "/" used `cookies().get('logto:jwtsdep1memhjojskp0fk')`. `cookies()` should be awaited before using its value. Learn more: https://nextjs.org/docs/messages/sync-dynamic-apis]

Dakuan commented 4 days ago

terminal looks like this in next 15 πŸ˜‚

image

wangsijie commented 4 days ago

https://github.com/logto-io/js/issues/836

kdy1 commented 3 days ago

Next.js maintainer & logto (personal) user here. This is the change in next@15

See https://nextjs.org/docs/app/building-your-application/upgrading/version-15#async-request-apis-breaking-change for more details.