@@ -22,6 +22,7 @@ import { TRPCError } from "@trpc/server";
22
22
import { getDefaultScheduleId } from "../viewer/availability/util" ;
23
23
import { updateUserMetadataAllowedKeys , type TUpdateProfileInputSchema } from "./updateProfile.schema" ;
24
24
25
+ const log = logger . getSubLogger ( { prefix : [ "updateProfile" ] } ) ;
25
26
type UpdateProfileOptions = {
26
27
ctx : {
27
28
user : NonNullable < TrpcSessionUser > ;
@@ -35,6 +36,7 @@ export const updateProfileHandler = async ({ ctx, input }: UpdateProfileOptions)
35
36
const userMetadata = handleUserMetadata ( { ctx, input } ) ;
36
37
const data : Prisma . UserUpdateInput = {
37
38
...input ,
39
+ avatar : await getAvatarToSet ( input . avatar ) ,
38
40
metadata : userMetadata ,
39
41
} ;
40
42
@@ -61,12 +63,6 @@ export const updateProfileHandler = async ({ ctx, input }: UpdateProfileOptions)
61
63
}
62
64
}
63
65
}
64
- if ( input . avatar ) {
65
- data . avatar = await resizeBase64Image ( input . avatar ) ;
66
- }
67
- if ( input . avatar === null ) {
68
- data . avatar = null ;
69
- }
70
66
71
67
if ( isPremiumUsername ) {
72
68
const stripeCustomerId = userMetadata ?. stripeCustomerId ;
@@ -234,3 +230,17 @@ const handleUserMetadata = ({ ctx, input }: UpdateProfileOptions) => {
234
230
// Required so we don't override and delete saved values
235
231
return { ...userMetadata , ...cleanMetadata } ;
236
232
} ;
233
+
234
+ async function getAvatarToSet ( avatar : string | null | undefined ) {
235
+ if ( avatar === null || avatar === undefined ) {
236
+ return avatar ;
237
+ }
238
+
239
+ if ( ! avatar . startsWith ( "data:image" ) ) {
240
+ // Non Base64 avatar currently could only be the dynamic avatar URL(i.e. /{USER}/avatar.png). If we allow setting that URL, we would get infinite redirects on /user/avatar.ts endpoint
241
+ log . warn ( "Non Base64 avatar, ignored it" , { avatar } ) ;
242
+ // `undefined` would not ignore the avatar, but `null` would remove it. So, we return `undefined` here.
243
+ return undefined ;
244
+ }
245
+ return await resizeBase64Image ( avatar ) ;
246
+ }
0 commit comments