front and back work

This commit is contained in:
QkoSad
2023-07-29 18:29:41 +03:00
parent 40051f9d5e
commit 2fee49e583
9 changed files with 29 additions and 26 deletions
-16
View File
@@ -28,16 +28,11 @@ import { logOut } from './reducers/auth';
const App = () => {
useEffect(() => {
// check for token in LS when app first runs
if (localStorage.token) {
// if there is a token set axios headers for all requests
setAuthToken(localStorage.token);
}
// try to fetch a user, if no token or invalid token we
// will get a 401 response from our API
store.dispatch(loadUser());
// log user out from all tabs if they log out in one tab
window.addEventListener("storage", () => {
if (!localStorage.token) store.dispatch(logOut);
});
@@ -80,17 +75,6 @@ const App = () => {
</Router>
</Provider>
);
/*
return (
<Provider store={store}>
<Router>
<Routes>
<Route path="login" element={<Login />} />
</Routes>
</Router>
</Provider>
);
*/
};
export default App;
@@ -82,7 +82,6 @@ const ProfileForm = () => {
const editing = profile ? true : false;
e.preventDefault();
await dispatch(createProfile(formData, editing)).then(() => {
console.log(editing)
if (!editing) navigate("/dashboard");
});
};
+2 -4
View File
@@ -25,13 +25,11 @@ store.subscribe(() => {
// if the token changes set the value in localStorage and axios headers
if (previousState.auth.token !== currentState.auth.token) {
const token = currentState.auth.token;
if (typeof token === 'string') setAuthToken(token);
else throw new Error("token not string")
setAuthToken(token);
}
});
// Infer the `RootState` and `AppDispatch` types from the store itself
export type RootState = ReturnType<typeof store.getState>
// Inferred type: {posts: PostsState, comments: CommentsState, users: UsersState}
export type AppDispatch = typeof store.dispatch
export default store;
+1 -1
View File
@@ -2,7 +2,7 @@ import api from './api';
// store our JWT in LS and set axios headers if we do have a token
const setAuthToken = (token: string) => {
const setAuthToken = (token: any) => {
if (token) {
api.defaults.headers.common['x-auth-token'] = token;
localStorage.setItem('token', token);