Jelajahi Sumber

Fix not authenticating user on initial load. Fixes #71

poeti8 6 tahun lalu
induk
melakukan
0428e9eb5d
1 mengubah file dengan 5 tambahan dan 4 penghapusan
  1. 5 4
      client/pages/settings.js

+ 5 - 4
client/pages/settings.js

@@ -1,20 +1,21 @@
 import React from 'react';
 import PropTypes from 'prop-types';
 import { connect } from 'react-redux';
+import { decode } from 'jsonwebtoken';
 import BodyWrapper from '../components/BodyWrapper';
 import Footer from '../components/Footer';
 import { authUser } from '../actions';
 import Settings from '../components/Settings';
 
-const SettingsPage = ({ isAuthenticated }) => (
+const SettingsPage = ({ auth, isAuthenticated }) => console.log({auth}) || (
   <BodyWrapper>
-    {isAuthenticated ? <Settings /> : null}
+    {isAuthenticated ? <Settings /> : <PageLoading />}
     <Footer />
   </BodyWrapper>
 );
 
 SettingsPage.getInitialProps = ({ req, reduxStore }) => {
-  const token = req && req.cookies && req.cookies.token;
+  const token = decode(req && req.cookies && req.cookies.token);
   if (token && reduxStore) reduxStore.dispatch(authUser(token));
   return {};
 };
@@ -23,6 +24,6 @@ SettingsPage.propTypes = {
   isAuthenticated: PropTypes.bool.isRequired,
 };
 
-const mapStateToProps = ({ auth: { isAuthenticated } }) => ({ isAuthenticated });
+const mapStateToProps = ({ auth }) => ({ isAuthenticated: auth.isAuthenticated, auth });
 
 export default connect(mapStateToProps)(SettingsPage);