這是我在當前項目中使用的過程。當用戶登錄時,我拿取令牌並存儲在localStorage中。然後,每當用戶轉到任何路線時,我都會將路線服務的組件包裝在一個特定的路徑中。這是檢查令牌的HOC的代碼。
export function requireAuthentication(Component) {
class AuthenticatedComponent extends React.Component {
componentWillMount() {
this.checkAuth(this.props.user.isAuthenticated);
}
componentWillReceiveProps (nextProps) {
this.checkAuth(nextProps.user.isAuthenticated);
}
checkAuth (isAuthenticated) {
if (!isAuthenticated) {
let redirectAfterLogin = this.props.location.pathname;
browserHistory.push(`/login?next=${redirectAfterLogin}`);
}
}
render() {
return (
<div>
{this.props.user.isAuthenticated === true
? <Component {...this.props}/>
: null
}
</div>
)
}
}
const mapStateToProps = (state) => ({
user: state.user
});
return connect(mapStateToProps)(AuthenticatedComponent);
}
然後在我的index.js我包裹每個受保護的路徑與此HOC像這樣:
<Route path='/protected' component={requireAuthentication(ProtectedComponent)} />
這是用戶減速器的外觀。
export default function userReducer(state = {}, action) {
switch(action.type) {
case types.USER_LOGIN_SUCCESS:
return {...action.user, isAuthenticated: true};
default:
return state;
}
}
action.user包含令牌。令牌可以來自用戶首次登錄時的API,也可以來自本地存儲,如果該用戶已經是登錄用戶。
將令牌存儲在會話或本地存儲中。在應用程序啓動時公開端點以驗證令牌。如果有一個令牌並且它仍然有效,那麼讓它們在不重新登錄的情況下進入。如果沒有令牌或它是無效的,那麼將它們踢到登錄屏幕並刪除令牌(如果存在)。 – kentcdodds
另請注意,客戶端上的任何內容都不安全,因此您應該始終驗證服務器端的輸入(在這種情況下,僅在用戶登錄並提供令牌後才提供登錄所需的頁面)。 –