Skip to content

Commit c683023

Browse files
authored
Merge pull request #7 from yarcub/1-add-state-to-token-callback
Pass store state to token fetch function
2 parents fa03480 + fedf1b9 commit c683023

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

README.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Major and minor versions follow the version number of Twilio javascript SDK. Cur
1818

1919
Middleware expects the following dependencies:
2020
* **Twilio**: the global Twilio instance
21-
* **token**: it expects a function that returns a Promise of a capability token
21+
* **token**: it expects a function that receives store state and returns a Promise of a capability token
2222
* **opts**: Twilio sdk [options](https://www.twilio.com/docs/api/client/device)
2323

2424
#### Apply middleware
@@ -29,9 +29,10 @@ import {middleware} from 'twiliojs-redux';
2929
import fetch from 'isomorphic-fetch';
3030
import rootReducer from './reducers';
3131

32-
const token = () => {
33-
return fetch('capability_token')
34-
.then((response) => response.text())
32+
const token = (state) => {
33+
return fetch('capability_token', {
34+
headers: { 'Authorization': `Bearer ${state.authToken}` }
35+
}).then((response) => response.text())
3536
}
3637

3738
const createStoreWithMiddleware = applyMiddleware(

src/middleware.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ var actions = require('./actions'),
22
constants = require('./constants');
33

44
const middleware = (twilioDevice, token, opts) => store => {
5-
token().then( value => {
5+
const getToken = () => token(store.getState())
6+
7+
getToken().then( value => {
68
twilioDevice.ready( device => {
79
store.dispatch(actions.changeDeviceStatus(device));
810
});
911

1012
twilioDevice.offline( device => {
1113
store.dispatch(actions.changeDeviceStatus(device));
12-
token().then( value => {
14+
getToken().then( value => {
1315
twilioDevice.setup(value, opts);
1416
})
1517
});

0 commit comments

Comments
 (0)