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

Fix avatar infinite redirect #5299

Merged
merged 6 commits into from
Nov 1, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
11 changes: 10 additions & 1 deletion apps/web/pages/api/user/avatar.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import crypto from "crypto";
import type { NextApiRequest, NextApiResponse } from "next";

import { CAL_URL, WEBAPP_URL } from "@calcom/lib/constants";
import { getPlaceholderAvatar } from "@calcom/lib/getPlaceholderAvatar";
import prisma from "@calcom/prisma";

Expand All @@ -11,6 +12,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
const username = req.query.username as string;
const teamname = req.query.teamname as string;
let identity;
let linksToThisRoute = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let linksToThisRoute = false;
let isAvatarSourceWebAppUrl = false;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might not be a better variable name considering it can be WEBSITE_URL

if (username) {
const user = await prisma.user.findUnique({
where: {
Expand All @@ -26,6 +28,9 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
email: user?.email,
avatar: user?.avatar,
};
linksToThisRoute =
hariombalhara marked this conversation as resolved.
Show resolved Hide resolved
identity.avatar === `${CAL_URL}/${username}/avatar.png` ||
identity.avatar === `${WEBAPP_URL}/${username}/avatar.png`;
hariombalhara marked this conversation as resolved.
Show resolved Hide resolved
} else if (teamname) {
const team = await prisma.team.findUnique({
where: {
Expand All @@ -40,14 +45,18 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
shouldDefaultBeNameBased: true,
avatar: team?.logo,
};
linksToThisRoute =
identity.avatar === `${CAL_URL}/team/${teamname}/avatar.png` ||
identity.avatar === `${WEBAPP_URL}/team/${teamname}/avatar.png`;
}

const emailMd5 = crypto
.createHash("md5")
.update((identity?.email as string) || "[email protected]")
.digest("hex");
const img = identity?.avatar;
if (!img) {
// If image isn't set or links to this route itself, use default avatar
if (!img || linksToThisRoute) {
hariombalhara marked this conversation as resolved.
Show resolved Hide resolved
let defaultSrc = defaultAvatarSrc({ md5: emailMd5 });
if (identity?.shouldDefaultBeNameBased) {
defaultSrc = getPlaceholderAvatar(null, identity.name);
Expand Down
3 changes: 2 additions & 1 deletion packages/trpc/server/createContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ async function getUserFromSession({
return null;
}
// This helps to prevent reaching the 4MB payload limit by avoiding base64 and instead passing the avatar url
if (user.avatar) user.avatar = `${CAL_URL}/${user.username}/avatar.png`;
hariombalhara marked this conversation as resolved.
Show resolved Hide resolved
// TODO: Setting avatar value to /avatar.png(which is a dynamic route) would actually reset the avatar because /avatar.png is supposed to return the value of user.avatar
// if (user.avatar) user.avatar = `${CAL_URL}/${user.username}/avatar.png`;
const avatar = user.avatar || defaultAvatarSrc({ email });

const locale = user.locale || getLocaleFromHeaders(req);
Expand Down