Skip to content

Commit c07a2ab

Browse files
authored
fix(node): reuse session ID in external storage example (#1035)
The previous example generated a new UUID on every wrap() call. This breaks RSC environments where cookies cannot be updated, causing session loss when the cookie still contains the old ID. Updated the RedisSessionWrapper example to store and reuse the session ID from unwrap(), ensuring the cookie value remains stable while external storage data can be updated.
1 parent 03dc395 commit c07a2ab

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

packages/node/README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,16 @@ But the cookie size is limited, so you may need to use external storage like Red
8686
import { CookieStorage } from '@logto/node';
8787

8888
class RedisSessionWrapper implements SessionWrapper {
89+
private currentSessionId?: string;
90+
8991
constructor(private readonly redis: Redis) {}
9092

9193
async wrap(data: unknown, _key: string): Promise<string> {
92-
const sessionId = randomUUID();
94+
// Reuse existing session ID if available, only generate new one for first-time users.
95+
// This is important for environments where cookies cannot be updated (e.g. React Server Components),
96+
// as the session ID in the cookie must remain stable while the data in Redis can be updated.
97+
const sessionId = this.currentSessionId ?? randomUUID();
98+
this.currentSessionId = sessionId;
9399
await this.redis.set(`logto_session_${sessionId}`, JSON.stringify(data));
94100
return sessionId;
95101
}
@@ -99,8 +105,10 @@ class RedisSessionWrapper implements SessionWrapper {
99105
return {};
100106
}
101107

108+
// Store the session ID for potential reuse in wrap()
109+
this.currentSessionId = value;
102110
const data = await this.redis.get(`logto_session_${value}`);
103-
return JSON.parse(data);
111+
return data ? JSON.parse(data) : {};
104112
}
105113
}
106114

0 commit comments

Comments
 (0)