feat(web): improve session retrieval performance (#7584)
* feat(web): improve session retrieval performance Switch to using `getServerSession` which avoids a HTTP round trip to retrieve session details. Additionally, migrate deprecated `app/lib/auth` calls to to `@calcom/lib` package. * fix: update failing test and lint * Consolidates auth code in features * Update yarn.lock * Update packages/trpc/server/createContext.ts * Oopsie --------- Co-authored-by: zomars <[email protected]>
This commit is contained in:
1 parent
d81d772cdf
commit
2317473545
61 files changed
+1034
-956
No files matched your search
@@ -1,6 +1,5 @@
|
||||
import superjson from "superjson";
|
||||
|
||||
import { httpBatchLink } from "../client/links/httpBatchLink";
|
||||
import { httpLink } from "../client/links/httpLink";
|
||||
import { loggerLink } from "../client/links/loggerLink";
|
||||
import { splitLink } from "../client/links/splitLink";
|
||||
@@ -8,6 +7,7 @@ import { createTRPCNext } from "../next";
|
||||
// ℹ️ Type-only import:
|
||||
// https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html#type-only-imports-and-export
|
||||
import type { TRPCClientErrorLike } from "../react";
|
||||
import { httpBatchLink } from "../react";
|
||||
import type { inferRouterInputs, inferRouterOutputs, Maybe } from "../server";
|
||||
import type { AppRouter } from "../server/routers/_app";
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import type { GetServerSidePropsContext } from "next";
|
||||
import type { GetServerSidePropsContext, NextApiRequest, NextApiResponse } from "next";
|
||||
import type { Session } from "next-auth";
|
||||
import { serverSideTranslations } from "next-i18next/serverSideTranslations";
|
||||
|
||||
import { getSession } from "@calcom/lib/auth";
|
||||
import { getServerSession } from "@calcom/features/auth/lib/getServerSession";
|
||||
import { WEBAPP_URL } from "@calcom/lib/constants";
|
||||
import { defaultAvatarSrc } from "@calcom/lib/defaultAvatarImage";
|
||||
import { getLocaleFromHeaders } from "@calcom/lib/i18n";
|
||||
@@ -108,6 +108,15 @@ type CreateInnerContextOptions = {
|
||||
i18n: Awaited<ReturnType<typeof serverSideTranslations>>;
|
||||
} & Partial<CreateContextOptions>;
|
||||
|
||||
export type GetSessionFn =
|
||||
| ((_options: {
|
||||
req: GetServerSidePropsContext["req"] | NextApiRequest;
|
||||
res: GetServerSidePropsContext["res"] | NextApiResponse;
|
||||
}) => Promise<Session | null>)
|
||||
| (() => Promise<Session | null>);
|
||||
|
||||
const DEFAULT_SESSION_GETTER: GetSessionFn = ({ req, res }) => getServerSession({ req, res });
|
||||
|
||||
/**
|
||||
* Inner context. Will always be available in your procedures, in contrast to the outer context.
|
||||
*
|
||||
@@ -128,9 +137,12 @@ export async function createContextInner(opts: CreateInnerContextOptions) {
|
||||
* Creates context for an incoming request
|
||||
* @link https://trpc.io/docs/context
|
||||
*/
|
||||
export const createContext = async ({ req, res }: CreateContextOptions, sessionGetter = getSession) => {
|
||||
export const createContext = async (
|
||||
{ req, res }: CreateContextOptions,
|
||||
sessionGetter: GetSessionFn = DEFAULT_SESSION_GETTER
|
||||
) => {
|
||||
// for API-response caching see https://trpc.io/docs/caching
|
||||
const session = await sessionGetter({ req });
|
||||
const session = await sessionGetter({ req, res });
|
||||
|
||||
const user = await getUserFromSession({ session, req });
|
||||
const locale = user?.locale ?? getLocaleFromHeaders(req);
|
||||
|
||||
@@ -20,10 +20,11 @@ import {
|
||||
} from "@calcom/core/videoClient";
|
||||
import dayjs from "@calcom/dayjs";
|
||||
import { sendCancelledEmails, sendFeedbackEmail } from "@calcom/emails";
|
||||
import { ErrorCode } from "@calcom/features/auth/lib/ErrorCode";
|
||||
import { verifyPassword } from "@calcom/features/auth/lib/verifyPassword";
|
||||
import { samlTenantProduct } from "@calcom/features/ee/sso/lib/saml";
|
||||
import { isPrismaObjOrUndefined, parseRecurringEvent } from "@calcom/lib";
|
||||
import getEnabledApps from "@calcom/lib/apps/getEnabledApps";
|
||||
import { ErrorCode, verifyPassword } from "@calcom/lib/auth";
|
||||
import { IS_SELF_HOSTED, WEBAPP_URL } from "@calcom/lib/constants";
|
||||
import { symmetricDecrypt } from "@calcom/lib/crypto";
|
||||
import getPaymentAppData from "@calcom/lib/getPaymentAppData";
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import { IdentityProvider } from "@prisma/client";
|
||||
import { z } from "zod";
|
||||
|
||||
import { hashPassword, validPassword, verifyPassword } from "@calcom/lib/auth";
|
||||
import { hashPassword } from "@calcom/features/auth/lib/hashPassword";
|
||||
import { validPassword } from "@calcom/features/auth/lib/validPassword";
|
||||
import { verifyPassword } from "@calcom/features/auth/lib/verifyPassword";
|
||||
import prisma from "@calcom/prisma";
|
||||
|
||||
import { TRPCError } from "@trpc/server";
|
||||
|
||||
Reference in new issue
Block a user