React路由器在服务器上没有重定向
React router not redirecting on server
我有一个高阶组件,检查用户是否经过身份验证,如果没有将重定向到不同的url。
这是一个同构应用程序,它在客户端工作,但如果我关闭JS,服务器不会重定向。
if (!this.props.authenticated) {
this.context.router.push('/');
}
我可以在服务器上点击这条语句,this.context.router
返回,但是什么也没有发生。
完整组件:
import React, { Component, PropTypes } from 'react';
import { connect } from 'react-redux';
export default function (ComposedComponent) {
class Authentication extends Component {
static contextTypes = {
router: React.PropTypes.object
}
static propTypes = {
authenticated: PropTypes.bool
}
componentWillMount() {
if (!this.props.authenticated) {
this.context.router.push('/');
}
}
componentWillUpdate(nextProps) {
if (!nextProps.authenticated) {
this.context.router.push('/');
}
}
render() {
return <ComposedComponent {...this.props} />;
}
}
function mapStateToProps(state) {
return { authenticated: state.auth.authenticated };
}
return connect(mapStateToProps)(Authentication);
}
,这个组件通过路由文件到达:
export default (
<Route path='/' component={App}>
<IndexRoute component={Welcome} />
<Route path='/feature' component={requireAuth(Feature)} />
<Route path='/signin' component={Signin} />
<Route path='/signout' component={Signout} />
<Route path='/signup' component={Signup} />
</Route>
);
这是服务器渲染代码:
import { RouterContext, match } from 'react-router';
import { Provider } from 'react-redux';
import { renderToString } from 'react-dom/server';
import React from 'react';
import reactCookie from 'react-cookie';
import configureStore from '../../shared/store';
import routes from '../../shared/routes';
import assets from '../../public/assets.json';
import { AUTH_USER } from '../../shared/actions/types';
export default function (req, res) {
match({ routes, location: req.url }, (error, redirectLocation, renderProps) => {
if (error) return res.status(500).send(error.message);
if (redirectLocation) return res.redirect(302, redirectLocation.pathname + redirectLocation.search);
if (!renderProps) return res.status(404).send('Page not found');
const store = configureStore();
// use cookies to retrieve token
const unplug = reactCookie.plugToRequest(req, res); // eslint-disable-line no-unused-vars
const token = reactCookie.load('token');
// if we have a token, consider the user to be signed in
if (token) {
// we need to update application state
store.dispatch({ type: AUTH_USER });
}
const content = renderToString(
<Provider store={store}>
<RouterContext {...renderProps} />
</Provider>
);
const initialState = JSON.stringify(store.getState());
return res.render('index', { content, assets, initialState });
});
}
你能分享你的服务器渲染代码吗?它可能需要看起来像https://github.com/ReactTraining/react-router/blob/master/docs/guides/ServerRendering.md,在这里您正在检查redirectLocation
标志。redirectLocation
可以通过使用onEnter
钩子而不是包装器组件返回。onEnter
钩子的文档在这里。
从下面的评论更新:好的,所以你的服务器代码正确检查redirectLocation
,但是路由代码需要使用onEnter
钩子来正确设置redirectLocation
。像这样:
好的,所以你在服务器代码中正确地尊重redirectLocation
,但redirectLocation
只使用onEnter钩子填充,如下所示:
const userIsInATeam = (nextState, replace) => {
if ( !isAuth ) {
replace('/')
}
}
<Route path="/users/:userId/teams" onEnter={userIsInATeam} />
我认为nextState
应该有你正在寻找的auth
道具来检查授权。
相关文章:
- 我需要一个注册按钮,它可以将数据发送到mySQL服务器,也可以在单击时重定向
- 如何进行服务器端HTTP重定向更新引用程序(更改原始引用程序)
- 我的页面总是将我重定向到我的服务器
- AngularJs $http postsuccessCallback 导致页面重定向,即使服务器以 200 状态响应也
- 服务器立即返回到 ajax 帖子,而不执行重定向代码
- jQuery表单插件:如何正确处理从服务器发回的重定向请求
- 在进行客户端重定向时,是否可以向服务器发送一些JSON
- JavaScript重定向它不是在服务器上工作,而是在本地主机上工作
- 在重定向之前将有关单击的链接的信息发送到服务器
- 在 iframe 中重定向服务器端加载
- 如何在没有持续服务器请求的情况下在 AngularJS 中进行视图重定向
- 重定向到服务器端的url
- 禁用来自服务器的任何类型的重定向
- 服务器返回重定向(302)后,iOS设备上的AJAX或XHR请求失败,代码为0
- 如何使用jquery发布,以便正确呈现来自服务器的重定向
- 有没有办法更改浏览器's的URL,而不将第一个原始请求重定向到服务器
- POST到服务器,其中encodeURIComponent包含><导致301重定向的字符
- 在响应请求之前,web服务器可以通过何种方式检查是否重定向现有页面上的GET请求
- 使用AJAX从服务器获取数据后,如何重定向到另一个URL?URL重定向不起作用
- 用php编写一个客户端重定向服务器端