login.js 973 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. import React, { Component } from 'react';
  2. import PropTypes from 'prop-types';
  3. import { connect } from 'react-redux';
  4. import Router from 'next/router';
  5. import BodyWrapper from '../components/BodyWrapper';
  6. import Login from '../components/Login';
  7. import { authUser } from '../actions';
  8. class LoginPage extends Component {
  9. componentDidMount() {
  10. if (this.props.isAuthenticated) {
  11. Router.push('/');
  12. }
  13. }
  14. render() {
  15. return (
  16. !this.props.isAuthenticated && (
  17. <BodyWrapper>
  18. <Login />
  19. </BodyWrapper>
  20. )
  21. );
  22. }
  23. }
  24. LoginPage.getInitialProps = ({ req, reduxStore }) => {
  25. const token = req && req.cookies && req.cookies.token;
  26. if (token && reduxStore) reduxStore.dispatch(authUser(token));
  27. return {};
  28. };
  29. LoginPage.propTypes = {
  30. isAuthenticated: PropTypes.bool.isRequired,
  31. };
  32. const mapStateToProps = ({ auth: { isAuthenticated } }) => ({ isAuthenticated });
  33. export default connect(mapStateToProps)(LoginPage);