aboutsummaryrefslogtreecommitdiffhomepage
path: root/frontend/app/.server/session.ts
diff options
context:
space:
mode:
Diffstat (limited to 'frontend/app/.server/session.ts')
-rw-r--r--frontend/app/.server/session.ts36
1 files changed, 35 insertions, 1 deletions
diff --git a/frontend/app/.server/session.ts b/frontend/app/.server/session.ts
index 2d9a652..0edcc35 100644
--- a/frontend/app/.server/session.ts
+++ b/frontend/app/.server/session.ts
@@ -8,9 +8,43 @@ export const cookieOptions = {
secrets: [process.env.ALBATROSS_COOKIE_SECRET ?? "local"],
};
-export const sessionStorage = createCookieSessionStorage({
+const innerSessionStorage = createCookieSessionStorage({
cookie: {
name: "albatross_session",
...cookieOptions,
},
});
+type InnerSessionStorage = typeof innerSessionStorage;
+
+// This class is used to recover from invalid sessions.
+// It may occur if the session had been created before the authentication library was updated.
+class RecoverableSessionStorage {
+ innerStorage: InnerSessionStorage;
+
+ constructor(innerStorage: InnerSessionStorage) {
+ this.innerStorage = innerStorage;
+ }
+
+ // If the session is invalid, return a new session.
+ // It may occur if the session had been created before the authentication library was updated.
+ getSession(...args: Parameters<InnerSessionStorage["getSession"]>) {
+ try {
+ return this.innerStorage.getSession(...args);
+ } catch (e) {
+ void e;
+ return this.innerStorage.getSession();
+ }
+ }
+
+ commitSession(...args: Parameters<InnerSessionStorage["commitSession"]>) {
+ return this.innerStorage.commitSession(...args);
+ }
+
+ destroySession(...args: Parameters<InnerSessionStorage["destroySession"]>) {
+ return this.innerStorage.destroySession(...args);
+ }
+}
+
+export const sessionStorage = new RecoverableSessionStorage(
+ innerSessionStorage,
+);