Skip to content
Merged
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: 3 additions & 5 deletions services/api/src/middlewares/withAuthenticated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ import { cache } from '@op/cache';
import { getAllowListUser } from '@op/common';
import { TRPCError } from '@trpc/server';

import { createSBAdminClient } from '../supabase/server';
import { getCachedAuthUser } from '../supabase/server';
import type { MiddlewareBuilderBase, TContextWithUser } from '../types';
import { verifyAuthentication } from '../utils/verifyAuthentication';

const withAuthenticated: MiddlewareBuilderBase<TContextWithUser> = async ({
ctx,
next,
}) => {
const supabase = createSBAdminClient(ctx);
const data = await supabase.auth.getUser();
const data = await getCachedAuthUser(ctx);

const user = verifyAuthentication(data);

Expand Down Expand Up @@ -45,8 +44,7 @@ const withAuthenticated: MiddlewareBuilderBase<TContextWithUser> = async ({
export const withAuthenticatedAdmin: MiddlewareBuilderBase<
TContextWithUser
> = async ({ ctx, next }) => {
const supabase = createSBAdminClient(ctx);
const data = await supabase.auth.getUser();
const data = await getCachedAuthUser(ctx);

const user = verifyAuthentication(data, true);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { UnauthorizedError, isUserEmailPlatformAdmin } from '@op/common';

import { createSBAdminClient } from '../supabase/server';
import { getCachedAuthUser } from '../supabase/server';
import type { MiddlewareBuilderBase, TContextWithUser } from '../types';
import { verifyAuthentication } from '../utils/verifyAuthentication';

Expand All @@ -10,8 +10,7 @@ import { verifyAuthentication } from '../utils/verifyAuthentication';
export const withAuthenticatedPlatformAdmin: MiddlewareBuilderBase<
TContextWithUser
> = async ({ ctx, next }) => {
const supabase = createSBAdminClient(ctx);
const data = await supabase.auth.getUser();
const data = await getCachedAuthUser(ctx);

const user = verifyAuthentication(data);

Expand Down
14 changes: 13 additions & 1 deletion services/api/src/supabase/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,26 @@
import { OPURLConfig, cookieOptionsDomain } from '@op/core';
import { logger } from '@op/logging';
import { createServerClient } from '@op/supabase/lib';
import type { CookieOptions } from '@op/supabase/lib';
import type { CookieOptions, UserResponse } from '@op/supabase/lib';
import type { Database } from '@op/supabase/types';
import 'server-only';

import type { TContext } from '../types';

const useUrl = OPURLConfig('APP');

const authUserCache = new WeakMap<TContext, Promise<UserResponse>>();

export function getCachedAuthUser(ctx: TContext): Promise<UserResponse> {
let promise = authUserCache.get(ctx);
if (!promise) {
const supabase = createSBAdminClient(ctx);
promise = supabase.auth.getUser();
authUserCache.set(ctx, promise);
}
return promise;
}

export const createSBAdminClient = (ctx: TContext) => {
return createServerClient<Database>(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
Expand Down
Loading