login.js 1003 B

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