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

Auth #324

Merged
merged 1 commit into from
Nov 20, 2024
Merged

Auth #324

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
8 changes: 2 additions & 6 deletions apps/dashboard/src/actions/ai/storage.ts
Original file line number Diff line number Diff line change
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 @@ -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>(`chat:${id}`);

Expand Down
4 changes: 2 additions & 2 deletions apps/dashboard/src/actions/institutions/create-plaid-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});

Expand Down
8 changes: 4 additions & 4 deletions apps/dashboard/src/actions/sign-out-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@ import { redirect } from "next/navigation";
export async function signOutAction() {
const supabase = createClient();
const {
data: { session },
data: { user },
} = await getSession();

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.user_metadata?.full_name,
});

analytics.track({
event: LogEvents.SignOut.name,
channel: LogEvents.SignOut.channel,
});

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

return redirect("/login");
}
8 changes: 4 additions & 4 deletions apps/dashboard/src/app/api/auth/callback/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
4 changes: 2 additions & 2 deletions apps/dashboard/src/app/api/gocardless/reconnect/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
6 changes: 4 additions & 2 deletions apps/dashboard/src/app/api/proxy/route.ts
Original file line number Diff line number Diff line change
@@ -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 });
Expand Down
28 changes: 16 additions & 12 deletions packages/supabase/src/queries/cached-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -111,7 +115,7 @@ export const getTeamUser = async () => {
["team", "user", data.id],
{
tags: [`team_user_${data.id}`],
revalidate: 180,
revalidate: 1800,
},
)(data.id);
};
Expand Down Expand Up @@ -349,7 +353,7 @@ export const getTrackerProjects = async (
["tracker_projects", teamId],
{
tags: [`tracker_projects_${teamId}`],
revalidate: 180,
revalidate: 3600,
},
)(params);
};
Expand All @@ -372,7 +376,7 @@ export const getTrackerRecordsByRange = async (
["tracker_entries", teamId],
{
tags: [`tracker_entries_${teamId}`],
revalidate: 180,
revalidate: 3600,
},
)(params);
};
Expand Down Expand Up @@ -597,7 +601,7 @@ export const getTags = async () => {
["tags", teamId],
{
tags: [`tags_${teamId}`],
revalidate: 180,
revalidate: 3600,
},
)();
};