-
Notifications
You must be signed in to change notification settings - Fork 46
/
configureStore.js
36 lines (27 loc) · 1.3 KB
/
configureStore.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import createHistory from 'history/createMemoryHistory'
import { NOT_FOUND } from 'redux-first-router'
import configureStore from '../src/configureStore'
export default async (req, res) => {
const jwToken = req.cookies.jwToken // see server/index.js to change jwToken
const preLoadedState = { jwToken } // onBeforeChange will authenticate using this
const history = createHistory({ initialEntries: [req.path] })
const { store, thunk } = configureStore(history, preLoadedState)
// if not using onBeforeChange + jwTokens, you can also async authenticate
// here against your db (i.e. using req.cookies.sessionId)
let location = store.getState().location
if (doesRedirect(location, res)) return false
// using redux-thunk perhaps request and dispatch some app-wide state as well, e.g:
// await Promise.all([store.dispatch(myThunkA), store.dispatch(myThunkB)])
await thunk(store) // THE PAYOFF BABY!
location = store.getState().location // remember: state has now changed
if (doesRedirect(location, res)) return false // only do this again if ur thunks have redirects
const status = location.type === NOT_FOUND ? 404 : 200
res.status(status)
return store
}
const doesRedirect = ({ kind, pathname }, res) => {
if (kind === 'redirect') {
res.redirect(302, pathname)
return true
}
}