async upload not working when not logged

I use this code to upload file from the front end.

        formData = new FormData;         formData.append('action', 'upload-attachment');         fileInputElement = document.getElementById('file');         formData.append('async-upload', fileInputElement.files[0]);         formData.append('name', fileInputElement.files[0].name);         formData.append('type', fileInputElement.files[0].type);         my_nonce = document.getElementById('my_nonce').value;         formData.append('_wpnonce', my_nonce);        '/wp-admin/async-upload.php', formData, {           headers: {             'Content-Type': 'multipart/form-data'           }         }).then(function(response) {           infos_contact.file_uploaded =;           infos_contact.file_uploaded_url =;         })["catch"](function(error) {           console.log(error);         }); 

It’s working fine when logged, but not anymore when I’m not logged. Since it’s for the front end, it’s useless if it doesn’t work when not logged. I guess WP protect the upload function if you’re not logged for security reason.

Can I use a hook to bypass this protection ?

Thanks !