Skip to content

Commit

Permalink
Update auth
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusab committed Nov 19, 2024
1 parent e027ace commit abd086f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
4 changes: 2 additions & 2 deletions packages/supabase/src/client/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { NextRequest, NextResponse } from "next/server";

export async function updateSession(
request: NextRequest,
response: NextResponse
response: NextResponse,
) {
const supabase = createServerClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
Expand All @@ -22,7 +22,7 @@ export async function updateSession(
response.cookies.set({ name, value: "", ...options });
},
},
}
},
);

await supabase.auth.getUser();
Expand Down
27 changes: 15 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,25 @@ 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 +94,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 +114,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 +352,7 @@ export const getTrackerProjects = async (
["tracker_projects", teamId],
{
tags: [`tracker_projects_${teamId}`],
revalidate: 180,
revalidate: 3600,
},
)(params);
};
Expand All @@ -372,7 +375,7 @@ export const getTrackerRecordsByRange = async (
["tracker_entries", teamId],
{
tags: [`tracker_entries_${teamId}`],
revalidate: 180,
revalidate: 3600,
},
)(params);
};
Expand Down Expand Up @@ -597,7 +600,7 @@ export const getTags = async () => {
["tags", teamId],
{
tags: [`tags_${teamId}`],
revalidate: 180,
revalidate: 3600,
},
)();
};

0 comments on commit abd086f

Please sign in to comment.