-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add next.js external storage (#951)
- Loading branch information
Showing
21 changed files
with
380 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
The SDK uses cookies to store encrypted session data by default. This approach is secure, requires no additional infrastructure, and works especially well in serverless environments like Vercel. | ||
|
||
However, there are times when you might need to store session data externally. For instance, when your session data grows too large for cookies, especially when you need to maintain multiple active organization sessions simultaneously. In these cases, you can implement external session storage using the `sessionWrapper` option: | ||
|
||
```ts | ||
import { MemorySessionWrapper } from './storage'; | ||
|
||
export const config = { | ||
// ... | ||
sessionWrapper: new MemorySessionWrapper(), | ||
}; | ||
``` | ||
|
||
```ts | ||
import { randomUUID } from 'node:crypto'; | ||
|
||
import { type SessionWrapper, type SessionData } from '@logto/next'; | ||
|
||
export class MemorySessionWrapper implements SessionWrapper { | ||
private readonly storage = new Map<string, unknown>(); | ||
|
||
async wrap(data: unknown, _key: string): Promise<string> { | ||
const sessionId = randomUUID(); | ||
this.storage.set(sessionId, data); | ||
return sessionId; | ||
} | ||
|
||
async unwrap(value: string, _key: string): Promise<SessionData> { | ||
if (!value) { | ||
return {}; | ||
} | ||
|
||
const data = this.storage.get(value); | ||
return data ?? {}; | ||
} | ||
} | ||
``` | ||
|
||
The above implementation uses a simple in-memory storage. In a production environment, you might want to use a more persistent storage solution, such as Redis or a database. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
...s-plugin-content-docs/current/quick-starts/framework/next/_external-storage.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
Das SDK verwendet standardmäßig Cookies, um verschlüsselte Sitzungsdaten zu speichern. Dieser Ansatz ist sicher, erfordert keine zusätzliche Infrastruktur und funktioniert besonders gut in serverlosen Umgebungen wie Vercel. | ||
|
||
Es gibt jedoch Zeiten, in denen du Sitzungsdaten extern speichern musst. Zum Beispiel, wenn deine Sitzungsdaten zu groß für Cookies werden, insbesondere wenn du mehrere aktive Organisation-Sitzungen gleichzeitig aufrechterhalten musst. In diesen Fällen kannst du eine externe Sitzungspeicherung mit der Option `sessionWrapper` implementieren: | ||
|
||
```ts | ||
import { MemorySessionWrapper } from './storage'; | ||
|
||
export const config = { | ||
// ... | ||
sessionWrapper: new MemorySessionWrapper(), | ||
}; | ||
``` | ||
|
||
```ts | ||
import { randomUUID } from 'node:crypto'; | ||
|
||
import { type SessionWrapper, type SessionData } from '@logto/next'; | ||
|
||
export class MemorySessionWrapper implements SessionWrapper { | ||
private readonly storage = new Map<string, unknown>(); | ||
|
||
async wrap(data: unknown, _key: string): Promise<string> { | ||
const sessionId = randomUUID(); | ||
this.storage.set(sessionId, data); | ||
return sessionId; | ||
} | ||
|
||
async unwrap(value: string, _key: string): Promise<SessionData> { | ||
if (!value) { | ||
return {}; | ||
} | ||
|
||
const data = this.storage.get(value); | ||
return data ?? {}; | ||
} | ||
} | ||
``` | ||
|
||
Die obige Implementierung verwendet einen einfachen In-Memory-Speicher. In einer Produktionsumgebung möchtest du möglicherweise eine beständigere Speicherlösung verwenden, wie Redis oder eine Datenbank. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
...s-plugin-content-docs/current/quick-starts/framework/next/_external-storage.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
El SDK utiliza cookies para almacenar datos de sesión cifrados por defecto. Este enfoque es seguro, no requiere infraestructura adicional y funciona especialmente bien en entornos sin servidor como Vercel. | ||
|
||
Sin embargo, hay ocasiones en las que podrías necesitar almacenar los datos de sesión externamente. Por ejemplo, cuando tus datos de sesión crecen demasiado para las cookies, especialmente cuando necesitas mantener múltiples sesiones activas de organización simultáneamente. En estos casos, puedes implementar un almacenamiento de sesión externo utilizando la opción `sessionWrapper`: | ||
|
||
```ts | ||
import { MemorySessionWrapper } from './storage'; | ||
|
||
export const config = { | ||
// ... | ||
sessionWrapper: new MemorySessionWrapper(), | ||
}; | ||
``` | ||
|
||
```ts | ||
import { randomUUID } from 'node:crypto'; | ||
|
||
import { type SessionWrapper, type SessionData } from '@logto/next'; | ||
|
||
export class MemorySessionWrapper implements SessionWrapper { | ||
private readonly storage = new Map<string, unknown>(); | ||
|
||
async wrap(data: unknown, _key: string): Promise<string> { | ||
const sessionId = randomUUID(); | ||
this.storage.set(sessionId, data); | ||
return sessionId; | ||
} | ||
|
||
async unwrap(value: string, _key: string): Promise<SessionData> { | ||
if (!value) { | ||
return {}; | ||
} | ||
|
||
const data = this.storage.get(value); | ||
return data ?? {}; | ||
} | ||
} | ||
``` | ||
|
||
La implementación anterior utiliza un almacenamiento en memoria simple. En un entorno de producción, podrías querer usar una solución de almacenamiento más persistente, como Redis o una base de datos. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.