OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.
fix: error when session expired
Carl-Gerhard Lindesvärdc67f7f957877
diff --git a/apps/api/src/index.ts b/apps/api/src/index.tsindex 7592c9dea..4d2fd8720 100644--- a/apps/api/src/index.ts+++ b/apps/api/src/index.ts@@ -146,15 +146,9 @@ const startServer = async () => {try {const sessionId = decodeSessionToken(req.cookies.session);const session = await runWithAlsSession(sessionId, () =>- sessionId- ? getCache(`validateSession:${sessionId}`, 60 * 5, async () =>- validateSessionToken(req.cookies.session),- )- : validateSessionToken(req.cookies.session),+ validateSessionToken(req.cookies.session),);- if (session.session) {- req.session = session;- }+ req.session = session;} catch (e) {req.session = EMPTY_SESSION;}diff --git a/packages/trpc/src/routers/auth.ts b/packages/trpc/src/routers/auth.tsindex ea4d7f8d7..34a9d19c3 100644--- a/packages/trpc/src/routers/auth.ts+++ b/packages/trpc/src/routers/auth.ts@@ -75,7 +75,6 @@ export const authRouter = createTRPCRouter({deleteSessionTokenCookie(ctx.setCookie);if (ctx.session?.session?.id) {await invalidateSession(ctx.session.session.id);- await deleteCache(`validateSession:${ctx.session.session.id}`);}}),signInOAuth: publicProcedure@@ -226,9 +225,7 @@ export const authRouter = createTRPCRouter({const token = generateSessionToken();const session = await createSession(token, user.id);- console.log('session', session);setSessionTokenCookie(ctx.setCookie, token, session.expiresAt);- console.log('ctx.setCookie', ctx.setCookie);return {type: 'email',};@@ -335,7 +332,6 @@ export const authRouter = createTRPCRouter({const session = await validateSessionToken(token);if (session.session) {- await deleteCache(`validateSession:${session.session.id}`);// Re-set the cookie with updated expirationsetSessionTokenCookie(ctx.setCookie, token, session.session.expiresAt);return {diff --git a/packages/trpc/src/trpc.ts b/packages/trpc/src/trpc.tsindex 1ffd28364..788796e7c 100644--- a/packages/trpc/src/trpc.ts+++ b/packages/trpc/src/trpc.ts@@ -95,7 +95,8 @@ const enforceUserIsAuthed = t.middleware(async ({ ctx, next }) => {// Only used on protected routesconst enforceAccess = t.middleware(async ({ ctx, next, type, getRawInput }) => {- return runWithAlsSession(ctx.session.session?.id, async () => {+ const sessionId = ctx.session?.session?.id ?? null;+ return runWithAlsSession(sessionId, async () => {const rawInput = await getRawInput();if (type === 'mutation' && process.env.DEMO_USER_ID) {throw new TRPCError({@@ -153,7 +154,7 @@ const loggerMiddleware = t.middleware();const sessionScopeMiddleware = t.middleware(async ({ ctx, next }) => {- const sessionId = ctx.session.session?.id ?? null;+ const sessionId = ctx.session?.session?.id ?? null;return runWithAlsSession(sessionId, async () => {return next();});