Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use getUser in most cases #287

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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