Skip to content

Commit 049d26f

Browse files
committed
Enable custom session tokens when calling cloud functions
1 parent 4510c13 commit 049d26f

File tree

2 files changed

+58
-6
lines changed

2 files changed

+58
-6
lines changed

src/Cloud.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,16 @@ export function run(
5050
throw new TypeError('Cloud function name must be a string.');
5151
}
5252

53+
var requestOptions = {};
54+
if (options.useMasterKey) {
55+
requestOptions.useMasterKey = options.useMasterKey;
56+
}
57+
if (options.sessionToken) {
58+
requestOptions.sessionToken = options.sessionToken;
59+
}
60+
5361
return (
54-
CoreManager.getCloudController().run(name, data, {
55-
useMasterKey: options.useMasterKey
56-
})._thenRunCallbacks(options)
62+
CoreManager.getCloudController().run(name, data, requestOptions)._thenRunCallbacks(options)
5763
);
5864
}
5965

@@ -63,11 +69,19 @@ var DefaultController = {
6369

6470
var payload = encode(data, true);
6571

72+
var requestOptions = {};
73+
if (options.hasOwnProperty('useMasterKey')) {
74+
requestOptions.useMasterKey = options.useMasterKey;
75+
}
76+
if (options.hasOwnProperty('sessionToken')) {
77+
requestOptions.sessionToken = options.sessionToken;
78+
}
79+
6680
var request = RESTController.request(
6781
'POST',
6882
'functions/' + name,
6983
payload,
70-
{ useMasterKey: !!options.useMasterKey }
84+
requestOptions
7185
);
7286

7387
return request.then(function(res) {

src/__tests__/Cloud-test.js

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,30 @@ describe('Cloud', () => {
4343
Cloud.run('myfunction', {});
4444

4545
expect(CoreManager.getCloudController().run.mock.calls[0])
46-
.toEqual(['myfunction', {}, { useMasterKey: undefined }]);
46+
.toEqual(['myfunction', {}, {}]);
4747
});
48+
49+
it('passes options', () => {
50+
Cloud.run('myfunction', {}, { useMasterKey: false });
51+
52+
expect(CoreManager.getCloudController().run.mock.calls[0])
53+
.toEqual(['myfunction', {}, {}]);
54+
55+
Cloud.run('myfunction', {}, { useMasterKey: true });
56+
57+
expect(CoreManager.getCloudController().run.mock.calls[1])
58+
.toEqual(['myfunction', {}, { useMasterKey: true }]);
59+
60+
Cloud.run('myfunction', {}, { sessionToken: 'asdf1234' });
61+
62+
expect(CoreManager.getCloudController().run.mock.calls[2])
63+
.toEqual(['myfunction', {}, { sessionToken: 'asdf1234' }]);
64+
65+
Cloud.run('myfunction', {}, { useMasterKey: true, sessionToken: 'asdf1234' });
66+
67+
expect(CoreManager.getCloudController().run.mock.calls[3])
68+
.toEqual(['myfunction', {}, { useMasterKey: true, sessionToken: 'asdf1234' }]);
69+
})
4870
});
4971

5072
describe('CloudController', () => {
@@ -65,6 +87,22 @@ describe('CloudController', () => {
6587
expect(CoreManager.getRESTController().request.mock.calls[0])
6688
.toEqual(['POST', 'functions/myfunction', {
6789
value: 12, when: { __type: 'Date', iso: '2015-01-01T00:00:00.000Z'}
68-
}, { useMasterKey: false }]);
90+
}, { }]);
91+
});
92+
93+
it('passes options', () => {
94+
Cloud.run('myfunction', { value: 12 }, { useMasterKey: true });
95+
96+
expect(CoreManager.getRESTController().request.mock.calls[0])
97+
.toEqual(['POST', 'functions/myfunction', {
98+
value: 12
99+
}, { useMasterKey: true }]);
100+
101+
Cloud.run('myfunction', { value: 12 }, { sessionToken: 'asdf1234' });
102+
103+
expect(CoreManager.getRESTController().request.mock.calls[1])
104+
.toEqual(['POST', 'functions/myfunction', {
105+
value: 12
106+
}, { sessionToken: 'asdf1234' }]);
69107
});
70108
});

0 commit comments

Comments
 (0)