From ba080744ce61419bdb31168f7a18b8216f2a5016 Mon Sep 17 00:00:00 2001 From: Rahul Mishra Date: Tue, 27 May 2025 11:36:01 +0530 Subject: [PATCH] fix: make some endpoints public and return default values (#1110) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Description `settings.get` and `connection.getDefault` are now public endpoints but they return default data and `null` in case of logged out users, this will fix the issue of UNAUTHORIZED logs being spammed for users who are just on homepage --- ## Type of Change Please delete options that are not relevant. - [ ] 🐛 Bug fix (non-breaking change which fixes an issue) - [ ] ✨ New feature (non-breaking change which adds functionality) - [ ] 💥 Breaking change (fix or feature with breaking changes) - [ ] 📝 Documentation update - [ ] 🎨 UI/UX improvement - [ ] 🔒 Security enhancement - [ ] ⚡ Performance improvement ## Areas Affected Please check all that apply: - [ ] Email Integration (Gmail, IMAP, etc.) - [ ] User Interface/Experience - [ ] Authentication/Authorization - [ ] Data Storage/Management - [ ] API Endpoints - [ ] Documentation - [ ] Testing Infrastructure - [ ] Development Workflow - [ ] Deployment/Infrastructure ## Testing Done Describe the tests you've done: - [ ] Unit tests added/updated - [ ] Integration tests added/updated - [ ] Manual testing performed - [ ] Cross-browser testing (if UI changes) - [ ] Mobile responsiveness verified (if UI changes) ## Security Considerations For changes involving data or authentication: - [ ] No sensitive data is exposed - [ ] Authentication checks are in place - [ ] Input validation is implemented - [ ] Rate limiting is considered (if applicable) ## Checklist - [ ] I have read the [CONTRIBUTING](../CONTRIBUTING.md) document - [ ] My code follows the project's style guidelines - [ ] I have performed a self-review of my code - [ ] I have commented my code, particularly in complex areas - [ ] I have updated the documentation - [ ] My changes generate no new warnings - [ ] I have added tests that prove my fix/feature works - [ ] All tests pass locally - [ ] Any dependent changes are merged and published ## Additional Notes Add any other context about the pull request here. ## Screenshots/Recordings Add screenshots or recordings here if applicable. --- _By submitting this pull request, I confirm that my contribution is made under the terms of the project's license._ --- apps/mail/app/root.tsx | 2 +- apps/mail/hooks/use-connections.ts | 4 ++-- apps/server/src/trpc/routes/connections.ts | 5 +++-- apps/server/src/trpc/routes/settings.ts | 6 ++++-- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/apps/mail/app/root.tsx b/apps/mail/app/root.tsx index 788614d8d..2428edf0d 100644 --- a/apps/mail/app/root.tsx +++ b/apps/mail/app/root.tsx @@ -42,7 +42,7 @@ export async function loader({ request }: Route.LoaderArgs) { const connectionId = await trpc.connections.getDefault .query() - .then((res) => res?.id) + .then((res) => res?.id ?? null) .catch(() => null); return { diff --git a/apps/mail/hooks/use-connections.ts b/apps/mail/hooks/use-connections.ts index 4c58f8812..2242e0e38 100644 --- a/apps/mail/hooks/use-connections.ts +++ b/apps/mail/hooks/use-connections.ts @@ -3,12 +3,12 @@ import { useQuery } from '@tanstack/react-query'; export const useConnections = () => { const trpc = useTRPC(); - const connectionsQuery = useQuery(trpc.connections.list.queryOptions(void 0)); + const connectionsQuery = useQuery(trpc.connections.list.queryOptions()); return connectionsQuery; }; export const useActiveConnection = () => { const trpc = useTRPC(); - const connectionsQuery = useQuery(trpc.connections.getDefault.queryOptions(void 0)); + const connectionsQuery = useQuery(trpc.connections.getDefault.queryOptions()); return connectionsQuery; }; diff --git a/apps/server/src/trpc/routes/connections.ts b/apps/server/src/trpc/routes/connections.ts index 532057097..862be5852 100644 --- a/apps/server/src/trpc/routes/connections.ts +++ b/apps/server/src/trpc/routes/connections.ts @@ -1,4 +1,4 @@ -import { createRateLimiterMiddleware, privateProcedure, router } from '../trpc'; +import { createRateLimiterMiddleware, privateProcedure, publicProcedure, router } from '../trpc'; import { getActiveConnection } from '../../lib/server-utils'; import { connection, user as user_ } from '../../db/schema'; import { Ratelimit } from '@upstash/ratelimit'; @@ -77,7 +77,8 @@ export const connectionsRouter = router({ if (connectionId === activeConnection.id) await db.update(user_).set({ defaultConnectionId: null }); }), - getDefault: privateProcedure.query(async () => { + getDefault: publicProcedure.query(async ({ ctx }) => { + if (!ctx.session) return null; const connection = await getActiveConnection(); return connection; }), diff --git a/apps/server/src/trpc/routes/settings.ts b/apps/server/src/trpc/routes/settings.ts index 96ea09e43..ffc6901af 100644 --- a/apps/server/src/trpc/routes/settings.ts +++ b/apps/server/src/trpc/routes/settings.ts @@ -1,11 +1,11 @@ +import { createRateLimiterMiddleware, privateProcedure, publicProcedure, router } from '../trpc'; import { defaultUserSettings, userSettingsSchema, type UserSettings } from '../../lib/schemas'; -import { createRateLimiterMiddleware, privateProcedure, router } from '../trpc'; import { Ratelimit } from '@upstash/ratelimit'; import { userSettings } from '../../db/schema'; import { eq } from 'drizzle-orm'; export const settingsRouter = router({ - get: privateProcedure + get: publicProcedure .use( createRateLimiterMiddleware({ limiter: Ratelimit.slidingWindow(60, '1m'), @@ -13,6 +13,8 @@ export const settingsRouter = router({ }), ) .query(async ({ ctx }) => { + if (!ctx.session) return { settings: defaultUserSettings }; + const { db, session } = ctx; const [result] = await db .select()