Currently it for clent-side apps only. Not for universall (isomorphic) apps. Will add it soon
I'm using the promise middleware to dispatch actions like this:
dispatch({
types: [ 'ACTION_START', 'ACTION_SUCCESS', 'ACTION_FAILURE' ],
promise: new Promise(...),
});
If you want to use another implementation - just change action object
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import MyLoginForm from './components/MyLoginForm';
import { check } from './auth-actions';
const mapDispatchToProps = (dispatch) => bindActionCreators({
onSubmit: check
}, dispatch);
const MyLoginFromContainer = connect(null, mapDispatchToProps)(MyLoginForm);
ReactDOM.render(<MyLoginFromContainer />, document.getElementById('app'));
After successful auth you'll get the token and the data inside your auth reducer state.
So, if you want to add token to your future requests just use applyToken function from helpers.js
Let's say your auth-header look like:
Authorization: Bearer <token>
In this case we need to use thunk-middleware because in action creator we need to obtain the token from state
// someActions.js
import fetch from 'isomorphic-fetch';
import applyToken from './helpers';
const apiUrl = 'https://my-super-api.com/v1/do-job';
export function doJobAction(data) {
return (dispatch, getState) => {
const token = getSate().auth.token;
const requestOptions = {
body: JSON.stringify(data),
method: 'POST',
headers: {
'Content-type': 'application/json',
'Accept': 'application/json'
}
};
// applyToken before request
const request = fetch(authUrl, applyToken(requestOptions, token));
return dispatch({
types: [ 'DO_JOB_START', 'DO_JOB_SUCCESS', 'DO_JOB_FAILURE' ],
promise: request,
});
};
}