From e0b4478cea84761f8bd82c0e54eb9b199fd906c4 Mon Sep 17 00:00:00 2001 From: Pontus Abrahamsson Date: Wed, 30 Oct 2024 08:59:24 +0100 Subject: [PATCH] Use getUser in most cases --- apps/dashboard/src/actions/ai/storage.ts | 12 +++--------- apps/dashboard/src/actions/sign-out-action.ts | 12 +++++------- apps/dashboard/src/app/[locale]/(app)/setup/page.tsx | 2 +- packages/supabase/src/queries/cached-queries.ts | 11 ++++++----- 4 files changed, 15 insertions(+), 22 deletions(-) diff --git a/apps/dashboard/src/actions/ai/storage.ts b/apps/dashboard/src/actions/ai/storage.ts index 87fdbdc4db..d19b23fc5a 100644 --- a/apps/dashboard/src/actions/ai/storage.ts +++ b/apps/dashboard/src/actions/ai/storage.ts @@ -1,7 +1,7 @@ "use server"; import { client as RedisClient } from "@midday/kv"; -import { getSession, getUser } from "@midday/supabase/cached-queries"; +import { getUser } from "@midday/supabase/cached-queries"; import type { Chat, SettingsResponse } from "./types"; export async function getAssistantSettings(): Promise { @@ -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, @@ -128,11 +124,9 @@ export async function getChats() { } export async function getChat(id: string) { - const { - data: { session }, - } = await getSession(); + const user = await getUser(); - const userId = session?.user.id; + const userId = user?.data?.id; const chat = await RedisClient.hgetall(`chat:${id}`); diff --git a/apps/dashboard/src/actions/sign-out-action.ts b/apps/dashboard/src/actions/sign-out-action.ts index c12eb41f8a..6d84141aef 100644 --- a/apps/dashboard/src/actions/sign-out-action.ts +++ b/apps/dashboard/src/actions/sign-out-action.ts @@ -2,24 +2,22 @@ import { LogEvents } from "@midday/events/events"; import { setupAnalytics } from "@midday/events/server"; -import { getSession } from "@midday/supabase/cached-queries"; +import { getUser } from "@midday/supabase/cached-queries"; import { createClient } from "@midday/supabase/server"; import { revalidateTag } from "next/cache"; import { redirect } from "next/navigation"; export async function signOutAction() { const supabase = createClient(); - const { - data: { session }, - } = await getSession(); + const user = await getUser(); await supabase.auth.signOut({ scope: "local", }); const analytics = await setupAnalytics({ - userId: session?.user.id, - fullName: session?.user.user_metadata?.full_name, + userId: user.id, + fullName: user.full_name, }); analytics.track({ @@ -27,7 +25,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/[locale]/(app)/setup/page.tsx b/apps/dashboard/src/app/[locale]/(app)/setup/page.tsx index 556817ef77..8d333950a9 100644 --- a/apps/dashboard/src/app/[locale]/(app)/setup/page.tsx +++ b/apps/dashboard/src/app/[locale]/(app)/setup/page.tsx @@ -1,5 +1,5 @@ import { SetupForm } from "@/components/setup-form"; -import { getSession, getUser } from "@midday/supabase/cached-queries"; +import { getUser } from "@midday/supabase/cached-queries"; import { Icons } from "@midday/ui/icons"; import type { Metadata } from "next"; import Link from "next/link"; diff --git a/packages/supabase/src/queries/cached-queries.ts b/packages/supabase/src/queries/cached-queries.ts index 5606945445..432e09d5a0 100644 --- a/packages/supabase/src/queries/cached-queries.ts +++ b/packages/supabase/src/queries/cached-queries.ts @@ -65,17 +65,18 @@ export const getSession = cache(async () => { }); export const getUser = async () => { + const supabase = createClient(); + const { - data: { session }, - } = await getSession(); - const userId = session?.user?.id; + data: { user }, + } = await supabase.auth.getUser(); + + const userId = user?.id; if (!userId) { return null; } - const supabase = createClient(); - return unstable_cache( async () => { return getUserQuery(supabase, userId);