Skip to content

Commit

Permalink
feat: add redux toolkit and fix auth HOC
Browse files Browse the repository at this point in the history
  • Loading branch information
amazingshellyyy authored and amazingandyyy committed Dec 22, 2022
1 parent 0aaf261 commit 05c97e7
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 1,298 deletions.
1 change: 1 addition & 0 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"webpack-merge": "^4.1.0"
},
"dependencies": {
"@reduxjs/toolkit": "^1.9.1",
"axios": "^0.27.2",
"parcel": "^2.8.0",
"prop-types": "^15.7.2",
Expand Down
11 changes: 3 additions & 8 deletions client/src/app.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux';
import { HashRouter, Route, Routes } from 'react-router-dom';
import reduxThunk from 'redux-thunk';
import Header from './components/header';
import Home from './components/home';
import Public from './components/public';
import Account from './components/account';
import Signin from './components/auth/signin';
import Signup from './components/auth/signup';
import Signout from './components/auth/signout'
import RequireAuth from './components/auth/require_auth';
import reducers from './reducers';
import AuthComponent from './components/auth/require_auth';
import { AUTH_USER } from './actions/types';

import { store } from './store';
import '../style/style.scss'

const createStoreWithMiddleware = applyMiddleware(reduxThunk)(createStore);
const store = createStoreWithMiddleware(reducers)
const token = localStorage.getItem('auth_jwt_token');

// if we have a token, consider the user to be signed in
Expand All @@ -32,7 +27,7 @@ ReactDOM.render(
<Routes>
<Route exact path="/" element= {<Home/>} />
<Route path="/public" element= {<Public/>} />
<Route path="/account" element= {RequireAuth(<Account />)} />
<Route path="/account" element= {<AuthComponent Component={Account}/>} />
<Route path="/signin" element= {<Signin/>} />
<Route path="/signup" element= {<Signup/>} />
<Route path="/signout" element= {<Signout/>} />
Expand Down
28 changes: 13 additions & 15 deletions client/src/components/auth/require_auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@ import { useEffect } from 'react';
import { connect } from 'react-redux';
import { useNavigate } from "react-router-dom";

export default function(ComposedComponent) {
const Authentication = (props) => {
const navigate = useNavigate()
useEffect(() => {
if (!props.authenticated) {
navigate('/signin')
}
}, [props.authenticated])
return <ComposedComponent {...props} />
}
const AuthComponent = ({ Component, ...props }) => {
const navigate = useNavigate()
useEffect(() => {
if (!props.authenticated) {
navigate('/signin')
}
}, [])

function mapStateToProps({auth}) {
return { authenticated: auth.authenticated };
}
return <Component {...props} />
}

return connect(mapStateToProps)(Authentication);
}
function mapStateToProps({auth}) {
return { authenticated: auth.authenticated };
}
export default connect(mapStateToProps)(AuthComponent)
12 changes: 4 additions & 8 deletions client/src/reducers/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { combineReducers } from 'redux';

import auth from './auth_reducer';
import user from './user_reducer';

const rootReducer = combineReducers({
auth,
user
});

export default rootReducer;
export default {
user,
auth
};
Empty file added client/src/slices/index.js
Empty file.
10 changes: 10 additions & 0 deletions client/src/store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { configureStore } from '@reduxjs/toolkit'
import slices from './slices'
import reducers from './reducers'
export const store = configureStore({
reducer: {...slices, ...reducers},
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware({
serializableCheck: false
})
})
Loading

0 comments on commit 05c97e7

Please sign in to comment.