2023-05-09 19:27:05 +00:00
|
|
|
import type { Session } from "next-auth";
|
|
|
|
|
|
|
|
import { WEBAPP_URL } from "@calcom/lib/constants";
|
|
|
|
import { defaultAvatarSrc } from "@calcom/lib/defaultAvatarImage";
|
2023-06-06 15:31:43 +00:00
|
|
|
import { userMetadata } from "@calcom/prisma/zod-utils";
|
2023-05-09 19:27:05 +00:00
|
|
|
|
|
|
|
import { TRPCError } from "@trpc/server";
|
|
|
|
import type { Maybe } from "@trpc/server";
|
|
|
|
|
|
|
|
import type { TRPCContextInner } from "../createContext";
|
|
|
|
import { middleware } from "../trpc";
|
|
|
|
|
|
|
|
export async function getUserFromSession(ctx: TRPCContextInner, session: Maybe<Session>) {
|
|
|
|
const { prisma } = ctx;
|
|
|
|
if (!session?.user?.id) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const user = await prisma.user.findUnique({
|
|
|
|
where: {
|
|
|
|
id: session.user.id,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
username: true,
|
|
|
|
name: true,
|
|
|
|
email: true,
|
2023-06-07 07:27:48 +00:00
|
|
|
emailVerified: true,
|
2023-05-09 19:27:05 +00:00
|
|
|
bio: true,
|
|
|
|
timeZone: true,
|
|
|
|
weekStart: true,
|
|
|
|
startTime: true,
|
|
|
|
endTime: true,
|
|
|
|
defaultScheduleId: true,
|
|
|
|
bufferTime: true,
|
|
|
|
theme: true,
|
|
|
|
createdDate: true,
|
|
|
|
hideBranding: true,
|
|
|
|
avatar: true,
|
|
|
|
twoFactorEnabled: true,
|
|
|
|
disableImpersonation: true,
|
|
|
|
identityProvider: true,
|
|
|
|
brandColor: true,
|
|
|
|
darkBrandColor: true,
|
|
|
|
away: true,
|
|
|
|
credentials: {
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
type: true,
|
|
|
|
key: true,
|
|
|
|
userId: true,
|
|
|
|
appId: true,
|
|
|
|
invalid: true,
|
|
|
|
},
|
|
|
|
orderBy: {
|
|
|
|
id: "asc",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
selectedCalendars: {
|
|
|
|
select: {
|
|
|
|
externalId: true,
|
|
|
|
integration: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
completedOnboarding: true,
|
|
|
|
destinationCalendar: true,
|
|
|
|
locale: true,
|
|
|
|
timeFormat: true,
|
|
|
|
trialEndsAt: true,
|
|
|
|
metadata: true,
|
|
|
|
role: true,
|
2023-05-30 15:34:35 +00:00
|
|
|
allowDynamicBooking: true,
|
2023-05-09 19:27:05 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
// some hacks to make sure `username` and `email` are never inferred as `null`
|
|
|
|
if (!user) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { email, username, id } = user;
|
|
|
|
if (!email || !id) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-06-06 15:31:43 +00:00
|
|
|
const userMetaData = userMetadata.parse(user.metadata || {});
|
2023-05-09 19:27:05 +00:00
|
|
|
const rawAvatar = user.avatar;
|
|
|
|
// This helps to prevent reaching the 4MB payload limit by avoiding base64 and instead passing the avatar url
|
|
|
|
user.avatar = rawAvatar ? `${WEBAPP_URL}/${user.username}/avatar.png` : defaultAvatarSrc({ email });
|
|
|
|
const locale = user?.locale || ctx.locale;
|
|
|
|
return {
|
|
|
|
...user,
|
|
|
|
id,
|
|
|
|
rawAvatar,
|
|
|
|
email,
|
|
|
|
username,
|
|
|
|
locale,
|
2023-06-06 15:31:43 +00:00
|
|
|
defaultBookerLayouts: userMetaData?.defaultBookerLayouts || null,
|
2023-05-09 19:27:05 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export type UserFromSession = Awaited<ReturnType<typeof getUserFromSession>>;
|
|
|
|
|
|
|
|
const getUserSession = async (ctx: TRPCContextInner) => {
|
|
|
|
const { getServerSession } = await import("@calcom/features/auth/lib/getServerSession");
|
|
|
|
const { req, res } = ctx;
|
|
|
|
|
|
|
|
const session = req ? await getServerSession({ req, res }) : null;
|
|
|
|
const user = session ? await getUserFromSession(ctx, session) : null;
|
|
|
|
|
|
|
|
return { user, session };
|
|
|
|
};
|
|
|
|
const sessionMiddleware = middleware(async ({ ctx, next }) => {
|
|
|
|
const { user, session } = await getUserSession(ctx);
|
|
|
|
|
|
|
|
return next({
|
|
|
|
ctx: { user, session },
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
export const isAuthed = middleware(async ({ ctx, next }) => {
|
|
|
|
const { user, session } = await getUserSession(ctx);
|
|
|
|
|
|
|
|
if (!user || !session) {
|
|
|
|
throw new TRPCError({ code: "UNAUTHORIZED" });
|
|
|
|
}
|
|
|
|
|
|
|
|
return next({
|
|
|
|
ctx: { ...ctx, user, session },
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
export const isAdminMiddleware = isAuthed.unstable_pipe(({ ctx, next }) => {
|
|
|
|
const { user } = ctx;
|
|
|
|
if (user?.role !== "ADMIN") {
|
|
|
|
throw new TRPCError({ code: "UNAUTHORIZED" });
|
|
|
|
}
|
|
|
|
return next({ ctx: { ...ctx, user: user } });
|
|
|
|
});
|
|
|
|
|
|
|
|
export default sessionMiddleware;
|