Created
August 17, 2017 10:35
-
-
Save razdvapoka/1e70b09b39cff2eb2a014de9acc8a8d3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import axiosCreator from '../axios' | |
import { getToken } from '../../cookies/server' | |
import httpStatus from 'http-status-codes' | |
import { preload as preloadData } from 'redux-router-preload/lib/server' | |
import { removeToken } from '../../cookies/server' | |
const preload = (req, res, next) => { | |
const axios = axiosCreator(getToken(req)) | |
preloadData(req.store, {axios}) | |
.then(() => { | |
next() | |
}) | |
.catch(preloadError => { | |
if (preloadError.status === httpStatus.UNAUTHORIZED) { | |
console.log(`User unauthorized`) | |
removeToken(res) | |
res.redirect(`/auth/signin`) | |
} else { | |
console.error(`Router error:`, preloadError.stack) | |
res.status(httpStatus.INTERNAL_SERVER_ERROR).send(`error`) | |
} | |
}) | |
} | |
export default preload |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment