我有一个高阶组件,用于检查用户是否经过身份验证,如果没有,则会重定向到其他网址。
它是一个同构的应用程序,这适用于客户端,但如果我关闭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 });
});
}
答案 0 :(得分:1)
您可以共享服务器呈现代码吗?它可能需要看起来像https://github.com/ReactTraining/react-router/blob/master/docs/guides/ServerRendering.md,您需要检查redirectLocation
标志。可以使用redirectLocation
挂钩而不是包装器组件返回onEnter
。记录onEnter
钩子here。
从以下评论更新:确定您的服务器代码正在检查redirectLocation
,但路由代码需要使用onEnter
挂钩正确设置redirectLocation
。像这样:
好的,所以你正确地尊重服务器代码中的redirectLocation
,但只使用onEnter挂钩填充redirectLocation
,如下所示:
const userIsInATeam = (nextState, replace) => {
if ( !isAuth ) {
replace('/')
}
}
<Route path="/users/:userId/teams" onEnter={userIsInATeam} />
我认为nextState
应该有auth
道具,您需要在其中查找身份验证。