Skip to content
4 changes: 4 additions & 0 deletions packages/user/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ const PERMISSIONS_USERS_ENABLE = "users:enable";
const PERMISSIONS_USERS_LIST = "users:list";
const PERMISSIONS_USERS_READ = "users:read";

// Default max size for user photo in MB
const DEFAULT_USER_PHOTO_MAX_SIZE_IN_MB = 5;

export {
DEFAULT_USER_PHOTO_MAX_SIZE_IN_MB,
EMAIL_VERIFICATION_MODE,
EMAIL_VERIFICATION_PATH,
INVITATION_ACCEPT_LINK_PATH,
Expand Down
16 changes: 16 additions & 0 deletions packages/user/src/model/users/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Session from "supertokens-node/recipe/session";
import ThirdPartyEmailPassword from "supertokens-node/recipe/thirdpartyemailpassword";

import UserSqlFactory from "./sqlFactory";
import { DEFAULT_USER_PHOTO_MAX_SIZE_IN_MB } from "../../constants";
import CustomApiError from "../../customApiError";
import validatePassword from "../../validator/password";

Expand Down Expand Up @@ -242,6 +243,21 @@ class UserService extends BaseService<User, UserCreateInput, UserUpdateInput> {
return undefined;
}

const photoSizeLimit =
this.config.user.photoMaxSizeInMB || DEFAULT_USER_PHOTO_MAX_SIZE_IN_MB;

if (photoSizeLimit) {
const maxSizeInBytes = photoSizeLimit * 1024 * 1024; // Convert to bytes

if (Buffer.isBuffer(data.data) && data.data.length > maxSizeInBytes) {
throw new CustomApiError({
message: `File size exceeds ${photoSizeLimit}MB limit`,
name: "ERROR_FILE_TOO_LARGE",
statusCode: 413,
});
}
}

if (!this._supportedMimeTypes.includes(data.mimetype)) {
throw new CustomApiError({
message: "Unsupported file type for profile picture",
Expand Down
1 change: 1 addition & 0 deletions packages/user/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ interface UserConfig {
};
password?: StrongPasswordOptions;
permissions?: string[];
photoMaxSizeInMB?: number;
role?: string;
roles?: string[];
routePrefix?: string;
Expand Down