@@ -247,7 +247,38 @@ export default class OAuthPlugin extends AdminForthPlugin {
247247 }
248248
249249 if ( this . options . userAvatarField && userInfo . profilePictureUrl ) {
250- console . log ( "HERE WE IMITATING SAVING USING UPLOAD PLUGIN" , this . avatarUploadPlugin ) ;
250+ const user = await this . adminforth . resource ( this . resource . resourceId ) . get ( Filters . EQ ( this . options . emailField , userInfo . email ) ) ;
251+ if ( user && user [ this . options . userAvatarField ] === null ) {
252+ const avatarResponse = await fetch ( userInfo . profilePictureUrl ) ;
253+ const blob = await avatarResponse . blob ( ) ;
254+ const fileType = blob . type ;
255+ const fileExtension = fileType . split ( '/' ) [ 1 ] ;
256+ const fileName = `avatar_${ user [ this . options . emailField ] } _${ randomUUID ( ) } ` ;
257+ const file = new File ( [ blob ] , fileName , { type : fileType } ) ;
258+ const { uploadUrl, uploadExtraParams, filePath, error } = await this . avatarUploadPlugin . getFileUploadUrl (
259+ fileName ,
260+ fileType ,
261+ null ,
262+ fileExtension ,
263+ null
264+ )
265+ const res = await fetch ( uploadUrl , {
266+ method : 'PUT' ,
267+ headers : {
268+ 'Content-Type' : fileType ,
269+ ...uploadExtraParams
270+ } ,
271+ body : file
272+ } ) ;
273+
274+ const success = res . ok ;
275+ if ( ! success ) {
276+ console . error ( 'Failed to upload avatar for user' , user [ this . options . emailField ] ) ;
277+ } else {
278+ const userResourcePrimaryKey = this . resource . columns . find ( col => col . primaryKey ) ?. name ;
279+ this . adminforth . resource ( this . resource . resourceId ) . update ( user [ userResourcePrimaryKey ] , { [ this . options . userAvatarField ] : filePath } )
280+ }
281+ }
251282 }
252283
253284 return await this . doLogin ( userInfo . email , response , {
0 commit comments