Skip to content

Commit

Permalink
#8 add CSRF
Browse files Browse the repository at this point in the history
  • Loading branch information
fxmontigny committed Dec 5, 2018
1 parent 8845c3c commit 71d9009
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 51 deletions.
101 changes: 51 additions & 50 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,40 @@ A module for Quill rich text editor to upload images to be selected from toolbar
### Webpack/ES6

```javascript
import Quill from 'quill'
import { ImageUpload } from 'quill-image-upload'
import Quill from 'quill';
import { ImageUpload } from 'quill-image-upload';

Quill.register('modules/imageUpload', ImageUpload)
Quill.register('modules/imageUpload', ImageUpload);

const quill = new Quill(editor, {
// ...
modules: {
// ...
imageUpload: {
url: '', // server url. If the url is empty then the base64 returns
method: 'POST', // change query method, default 'POST'
name: 'image', // custom form name
withCredentials: false, // withCredentials
headers: {}, // add custom headers, example { token: 'your-token'}
customUploader: () => {}, // add custom uploader
// personalize successful callback and call next function to insert new url to the editor
callbackOK: (serverResponse, next) => {
next(serverResponse)
},
// personalize failed callback
callbackKO: serverError => {
alert(serverError)
},
// optional
// add callback when a image have been chosen
checkBeforeSend: (file, next) => {
console.log(file)
next(file) // go back to component and send to the server
}
}
}
})
// ...
modules: {
// ...
imageUpload: {
url: '', // server url. If the url is empty then the base64 returns
method: 'POST', // change query method, default 'POST'
name: 'image', // custom form name
withCredentials: false, // withCredentials
headers: {}, // add custom headers, example { token: 'your-token'}
csrf: { token: 'token', hash: '' }, // add custom CSRF
customUploader: () => {}, // add custom uploader
// personalize successful callback and call next function to insert new url to the editor
callbackOK: (serverResponse, next) => {
next(serverResponse);
},
// personalize failed callback
callbackKO: serverError => {
alert(serverError);
},
// optional
// add callback when a image have been chosen
checkBeforeSend: (file, next) => {
console.log(file);
next(file); // go back to component and send to the server
}
}
}
});
```

### Script Tag
Expand All @@ -52,24 +53,24 @@ Copy image-upload.min.js into your web root or include from node_modules

```javascript
var quill = new Quill(editor, {
// ...
modules: {
// ...
imageUpload: {
url: '', // server url. If the url is empty then the base64 returns
method: 'POST', // change query method, default 'POST'
name: 'image', // custom form name
withCredentials: false, // withCredentials
headers: {}, // add custom headers, example { token: 'your-token'}
// personalize successful callback and call next function to insert new url to the editor
callbackOK: (serverResponse, next) => {
next(serverResponse)
},
// personalize failed callback
callbackKO: serverError => {
alert(serverError)
}
}
}
})
// ...
modules: {
// ...
imageUpload: {
url: '', // server url. If the url is empty then the base64 returns
method: 'POST', // change query method, default 'POST'
name: 'image', // custom form name
withCredentials: false, // withCredentials
headers: {}, // add custom headers, example { token: 'your-token'}
// personalize successful callback and call next function to insert new url to the editor
callbackOK: (serverResponse, next) => {
next(serverResponse);
},
// personalize failed callback
callbackKO: serverError => {
alert(serverError);
}
}
}
});
```
2 changes: 1 addition & 1 deletion image-upload.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ export class ImageUpload {
const fd = new FormData();

fd.append(name, file);

if (this.options.csrf) {
// add CSRF
fd.append(this.options.csrf.token, this.options.csrf.hash);
}

const xhr = new XMLHttpRequest();
// init http query
xhr.open(method, url, true);
Expand Down
6 changes: 6 additions & 0 deletions src/image-upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ export class ImageUpload {
const fd = new FormData();

fd.append(name, file);

if (this.options.csrf) {
// add CSRF
fd.append(this.options.csrf.token, this.options.csrf.hash);
}

const xhr = new XMLHttpRequest();
// init http query
xhr.open(method, url, true);
Expand Down

0 comments on commit 71d9009

Please sign in to comment.