1

I'm having trouble moving from using fetch to using fetch within multiple dispatches. So this code works:

export const checkLoginCredentials = (username, password) => {
let data = {username: username, password: password}
fetch('/authenticate', {
    body: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    },
    method: 'POST'
  }).then(response => response.json()).then(responseJson => console.log(responseJson))}

Which means when I hit this function I see an authentication token in my console. This code is not working:

export const checkLoginCredentials = (username, password) => {
return (dispatch) => {
dispatch({ type: 'FIND_USER'});
let data = {username: username, password: password}
return fetch('/authenticate', {
    body: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    },
    method: 'POST'
  }).then(response => response.json()).then(responseJson => dispatch({ type: 'AUTHENTICATE', payload: responseJson }))}}

In my store I have:

import { combineReducers, createStore, applyMiddleware, compose } from 'redux'
import userReducer from './userReducer'
import gameReducer from './gameReducer'
import thunk from 'redux-thunk';


// we wrap store in a function for testing purposes
const composeEnhancers =
typeof window === 'object' && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ ?
window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
// Specify extension’s options like name, actionsBlacklist, 
actionsCreators, serialize...
}) : compose;

const enhancer = composeEnhancers(
applyMiddleware(thunk),
  // other store enhancers if any
);
const rootReducer = combineReducers({
  user: userReducer,
  game: gameReducer})

 export const configureStore = () => {
return createStore(rootReducer, enhancer);}

Any ideas would be appreciated! ps. I am using isomorphic-fetch

0 Answers0