Skip to content

Commit

Permalink
Use getUser in most cases
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusab committed Oct 30, 2024
1 parent 1fab08d commit e0b4478
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 22 deletions.
12 changes: 3 additions & 9 deletions apps/dashboard/src/actions/ai/storage.ts
Original file line number Diff line number Diff line change
@@ -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<SettingsResponse> {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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>(`chat:${id}`);

Expand Down
12 changes: 5 additions & 7 deletions apps/dashboard/src/actions/sign-out-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,30 @@

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({
event: LogEvents.SignOut.name,
channel: LogEvents.SignOut.channel,
});

revalidateTag(`user_${session?.user.id}`);
revalidateTag(`user_${user.id}`);

return redirect("/login");
}
2 changes: 1 addition & 1 deletion apps/dashboard/src/app/[locale]/(app)/setup/page.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
11 changes: 6 additions & 5 deletions packages/supabase/src/queries/cached-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit e0b4478

Please sign in to comment.