Skip to content

Commit c616f2a

Browse files
committed
浏览器支持传入 masterKey
1 parent a4288f0 commit c616f2a

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/browserify-wrapper/ajax-browser.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,14 @@ const ajax = (method, url, data, success, error) => {
4949
};
5050
xhr.open(method, url, true);
5151
xhr.setRequestHeader('X-LC-Id', appId);
52-
// 浏览器端不支持传入 masterKey 做 sign
53-
const signature = AVUtils.sign(appKey);
52+
53+
let signature;
54+
if (masterKey) {
55+
signature = AVUtils.sign(masterKey, true);
56+
} else {
57+
signature = AVUtils.sign(appKey);
58+
}
59+
5460
xhr.setRequestHeader('X-LC-Sign', signature);
5561
xhr.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
5662
xhr.send(data);

src/utils.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,19 @@ const init = (AV) => {
139139
*/
140140

141141
AV.init = (...args) => {
142+
143+
const masterKeyWarn = () => {
144+
console.warn('MasterKey should not be used in the browser. ' +
145+
'The permissions of MasterKey can be across all the server permissions,' +
146+
' including the setting of ACL .');
147+
};
148+
142149
switch (args.length) {
143150
case 1:
144151
const options = args[0];
145152
if (typeof options === 'object') {
146153
if (!AVConfig.isNode && options.masterKey) {
147-
throw new Error('AV.init(): Master Key is only used in Node.js.');
154+
masterKeyWarn();
148155
}
149156
initialize(options.appId, options.appKey, options.masterKey);
150157
setRegionServer(options.region);
@@ -157,7 +164,7 @@ const init = (AV) => {
157164
case 3:
158165
console.warn('Please use AV.init() to replace AV.initialize() .');
159166
if (!AVConfig.isNode && args.length === 3) {
160-
throw new Error('AV.init(): Master Key is only used in Node.js.');
167+
masterKeyWarn();
161168
}
162169
initialize(...args);
163170
setRegionServer('cn');

0 commit comments

Comments
 (0)