diff --git a/apps/dashboard/src/actions/ai/storage.ts b/apps/dashboard/src/actions/ai/storage.ts index 87fdbdc4db..152496972c 100644 --- a/apps/dashboard/src/actions/ai/storage.ts +++ b/apps/dashboard/src/actions/ai/storage.ts @@ -39,10 +39,6 @@ export async function setAssistantSettings({ userId, teamId, }: SetAassistant) { - const { - data: { session }, - } = await getSession(); - return RedisClient.set(`assistant:${teamId}:user:${userId}:settings`, { ...settings, ...params, @@ -129,10 +125,10 @@ export async function getChats() { export async function getChat(id: string) { const { - data: { session }, + data: { user }, } = await getSession(); - const userId = session?.user.id; + const userId = user.id; const chat = await RedisClient.hgetall(`chat:${id}`); diff --git a/apps/dashboard/src/actions/institutions/create-plaid-link.ts b/apps/dashboard/src/actions/institutions/create-plaid-link.ts index 2d91cd4203..cadd7b5234 100644 --- a/apps/dashboard/src/actions/institutions/create-plaid-link.ts +++ b/apps/dashboard/src/actions/institutions/create-plaid-link.ts @@ -5,11 +5,11 @@ import { getSession } from "@midday/supabase/cached-queries"; export const createPlaidLinkTokenAction = async (accessToken?: string) => { const { - data: { session }, + data: { user }, } = await getSession(); const { data } = await engine.auth.plaid.link({ - userId: session?.user.id, + userId: user.id, accessToken, }); diff --git a/apps/dashboard/src/actions/sign-out-action.ts b/apps/dashboard/src/actions/sign-out-action.ts index c12eb41f8a..2bc8574b19 100644 --- a/apps/dashboard/src/actions/sign-out-action.ts +++ b/apps/dashboard/src/actions/sign-out-action.ts @@ -10,7 +10,7 @@ import { redirect } from "next/navigation"; export async function signOutAction() { const supabase = createClient(); const { - data: { session }, + data: { user }, } = await getSession(); await supabase.auth.signOut({ @@ -18,8 +18,8 @@ export async function signOutAction() { }); const analytics = await setupAnalytics({ - userId: session?.user.id, - fullName: session?.user.user_metadata?.full_name, + userId: user.id, + fullName: user.user_metadata?.full_name, }); analytics.track({ @@ -27,7 +27,7 @@ export async function signOutAction() { channel: LogEvents.SignOut.channel, }); - revalidateTag(`user_${session?.user.id}`); + revalidateTag(`user_${user.id}`); return redirect("/login"); } diff --git a/apps/dashboard/src/app/api/auth/callback/route.ts b/apps/dashboard/src/app/api/auth/callback/route.ts index 93b6790684..e177865a3c 100644 --- a/apps/dashboard/src/app/api/auth/callback/route.ts +++ b/apps/dashboard/src/app/api/auth/callback/route.ts @@ -36,15 +36,15 @@ export async function GET(req: NextRequest) { await supabase.auth.exchangeCodeForSession(code); const { - data: { session }, + data: { user }, } = await getSession(); - if (session) { - const userId = session.user.id; + if (user) { + const userId = user.id; const analytics = await setupAnalytics({ userId, - fullName: session?.user?.user_metadata?.full_name, + fullName: user?.user_metadata?.full_name, }); await analytics.track({ diff --git a/apps/dashboard/src/app/api/gocardless/reconnect/route.ts b/apps/dashboard/src/app/api/gocardless/reconnect/route.ts index 824fe72782..8b44d1b917 100644 --- a/apps/dashboard/src/app/api/gocardless/reconnect/route.ts +++ b/apps/dashboard/src/app/api/gocardless/reconnect/route.ts @@ -9,10 +9,10 @@ export const preferredRegion = ["fra1", "sfo1", "iad1"]; export async function GET(req: NextRequest) { const { - data: { session }, + data: { user }, } = await getSession(); - if (!session) { + if (!user) { return NextResponse.redirect(new URL("/", req.url)); } diff --git a/apps/dashboard/src/app/api/proxy/route.ts b/apps/dashboard/src/app/api/proxy/route.ts index 20ddd54ec0..c7a532c045 100644 --- a/apps/dashboard/src/app/api/proxy/route.ts +++ b/apps/dashboard/src/app/api/proxy/route.ts @@ -1,13 +1,15 @@ -import { getSession } from "@midday/supabase/cached-queries"; +import { createClient } from "@midday/supabase/server"; import { type NextRequest, NextResponse } from "next/server"; export async function GET(req: NextRequest) { const requestUrl = new URL(req.url); const filePath = requestUrl.searchParams.get("filePath"); + const supabase = createClient(); + const { data: { session }, - } = await getSession(); + } = await supabase.auth.getSession(); if (!session) { return new NextResponse("Unauthorized", { status: 401 }); diff --git a/packages/supabase/src/queries/cached-queries.ts b/packages/supabase/src/queries/cached-queries.ts index 136199948b..f02b24067a 100644 --- a/packages/supabase/src/queries/cached-queries.ts +++ b/packages/supabase/src/queries/cached-queries.ts @@ -61,23 +61,26 @@ export const getTransactions = async ( }, ["transactions", teamId], { - revalidate: 180, + revalidate: 600, tags: [`transactions_${teamId}`], }, )(params); }; +// Cache per request export const getSession = cache(async () => { const supabase = createClient(); - return supabase.auth.getSession(); + return supabase.auth.getUser(); }); -export const getUser = async () => { +// Cache per request and revalidate every 30 minutes +export const getUser = cache(async () => { const { - data: { session }, + data: { user }, } = await getSession(); - const userId = session?.user?.id; + + const userId = user?.id; if (!userId) { return null; @@ -92,10 +95,11 @@ export const getUser = async () => { ["user", userId], { tags: [`user_${userId}`], - revalidate: 180, + // 30 minutes, jwt expires in 1 hour + revalidate: 1800, }, - )(userId); -}; + )(); +}); export const getTeamUser = async () => { const supabase = createClient(); @@ -111,7 +115,7 @@ export const getTeamUser = async () => { ["team", "user", data.id], { tags: [`team_user_${data.id}`], - revalidate: 180, + revalidate: 1800, }, )(data.id); }; @@ -349,7 +353,7 @@ export const getTrackerProjects = async ( ["tracker_projects", teamId], { tags: [`tracker_projects_${teamId}`], - revalidate: 180, + revalidate: 3600, }, )(params); }; @@ -372,7 +376,7 @@ export const getTrackerRecordsByRange = async ( ["tracker_entries", teamId], { tags: [`tracker_entries_${teamId}`], - revalidate: 180, + revalidate: 3600, }, )(params); }; @@ -597,7 +601,7 @@ export const getTags = async () => { ["tags", teamId], { tags: [`tags_${teamId}`], - revalidate: 180, + revalidate: 3600, }, )(); };