-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔥 add auth state, twitter, github oauth and strict public routes
- Loading branch information
1 parent
429ddc8
commit 5a7f97e
Showing
12 changed files
with
223 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,75 @@ | ||
import React from "react"; | ||
import React, { useEffect, useState } from "react"; | ||
import axios from "axios"; | ||
import { BrowserRouter as Router, Route, Switch } from "react-router-dom"; | ||
import { Provider } from "react-redux"; | ||
import { connect } from "react-redux"; | ||
|
||
import "./App.css"; | ||
import SignupContainer from "./containers/SignupContainer"; | ||
import LoginContainer from "./containers/LoginContainer"; | ||
import PrivateRoute from "./containers/PrivateRoute"; | ||
import PublicRoute from "./containers/PublicRoute"; | ||
import Dashboard from "./containers/Dashboard"; | ||
import EditProfile from "./containers/EditProfile"; | ||
import store from "./redux/store"; | ||
// import store from "./redux/store"; | ||
|
||
import { setAuthState } from "./redux/actions/authActions"; | ||
|
||
const App = ({ isAuth, setAuthState }) => { | ||
const [isLoading, setIsLoading] = useState(true); | ||
|
||
useEffect(() => { | ||
axios | ||
.get("/auth/state", { withCredentials: true }) | ||
.then((res) => { | ||
setAuthState(res.data); | ||
setIsLoading(false); | ||
}) | ||
.catch((err) => { | ||
setIsLoading(true); | ||
throw new Error(err); | ||
}); | ||
}, []); | ||
|
||
function App() { | ||
return ( | ||
<Provider store={store}> | ||
<> | ||
<Router> | ||
<Switch> | ||
<PrivateRoute exact path="/" component={Dashboard} /> | ||
<PrivateRoute exact path="/edit" component={EditProfile} /> | ||
<Route exact path="/signup" component={SignupContainer} /> | ||
<Route exact path="/login" component={LoginContainer} /> | ||
</Switch> | ||
</Router> | ||
</> | ||
</Provider> | ||
<> | ||
<Router> | ||
<Switch> | ||
<PrivateRoute | ||
exact | ||
path="/" | ||
isLoading={isLoading} | ||
isAuthenticated={isAuth} | ||
component={Dashboard} | ||
/> | ||
<PrivateRoute | ||
exact | ||
path="/edit" | ||
isLoading={isLoading} | ||
isAuthenticated={isAuth} | ||
component={EditProfile} | ||
/> | ||
<PublicRoute | ||
exact | ||
isLoading={isLoading} | ||
isAuthenticated={isAuth} | ||
path="/signup" | ||
component={SignupContainer} | ||
/> | ||
<PublicRoute | ||
exact | ||
isLoading={isLoading} | ||
isAuthenticated={isAuth} | ||
path="/login" | ||
component={LoginContainer} | ||
/> | ||
</Switch> | ||
</Router> | ||
</> | ||
); | ||
} | ||
}; | ||
|
||
const mapStateToProps = (state) => ({ | ||
isAuth: state.auth.isAuthenticated, | ||
}); | ||
|
||
export default App; | ||
export default connect(mapStateToProps, { setAuthState })(App); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* eslint-disable no-nested-ternary */ | ||
/* eslint-disable implicit-arrow-linebreak */ | ||
|
||
import React from "react"; | ||
import { Route, Redirect } from "react-router-dom"; | ||
import PropTypes from "prop-types"; | ||
|
||
import Loading from "../components/Loading"; | ||
|
||
const PublicRoute = ({ | ||
component: Component, | ||
isLoading, | ||
isAuthenticated, | ||
...rest | ||
}) => ( | ||
<Route | ||
{...rest} | ||
render={(props) => | ||
isLoading ? ( | ||
<Loading /> | ||
) : !isAuthenticated ? ( | ||
<Component {...props} /> | ||
) : ( | ||
<Redirect to="/" /> | ||
) | ||
} | ||
/> | ||
); | ||
|
||
PublicRoute.propTypes = { | ||
component: PropTypes.oneOfType([PropTypes.object, PropTypes.func]).isRequired, | ||
}; | ||
|
||
export default PublicRoute; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,15 @@ | ||
import React from "react"; | ||
import ReactDOM from "react-dom"; | ||
import { Provider } from "react-redux"; | ||
|
||
import store from "./redux/store"; | ||
|
||
import App from "./App"; | ||
|
||
ReactDOM.render(<App />, document.getElementById("root")); | ||
const Index = () => ( | ||
<Provider store={store}> | ||
<App /> | ||
</Provider> | ||
); | ||
|
||
ReactDOM.render(<Index />, document.getElementById("root")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,9 @@ | ||
export const SET_CURRENT_USER = "SET_CURRENT_USER"; | ||
export const CLEAR_CURRENT_USER = "CLEAR_CURRENT_USER"; | ||
export const USER_LOADING = "USER_LOADING"; | ||
|
||
export const SET_AUTH_STATE = "SET_AUTH_STATE"; | ||
export const EDIT_USER = "EDIT_USER"; | ||
export const DELETE_USER = "DELETE_USER"; | ||
export const SET_AUTH_STATE = "SET_AUTH_STATE"; | ||
|
||
export const GET_ERRORS = "GET_ERRORS"; | ||
export const CLEAR_ERRORS = "CLEAR_ERRORS"; |
Oops, something went wrong.