JWT refresh

This commit is contained in:
2020-11-29 20:23:20 +01:00
parent e48d300ca9
commit 6d02533d69
18 changed files with 69 additions and 22 deletions

View File

@@ -2,6 +2,29 @@ import axios from "axios";
import * as toggles from "./toggles";
import { push } from "connected-react-router";
import { backend } from "../config.js";
import store from "../index.js";
axios.defaults.withCredentials = true;
axios.interceptors.response.use(
(response) => {
return response;
},
(error) => {
return new Promise((resolve) => {
if (error.response && error.response.status === 401 && error.config && !error.config.__isRetryRequest) {
const response = axios.post(backend + "user/refreshtoken", {withCredentials: true}).then((res) => {
const jwt = res.headers['x-auth-token'];
store.dispatch(setNewToken(jwt))
error.config.__isRetryRequest = true
error.config.headers['x-auth-token'] = jwt
return axios(error.config);
})
resolve(response)
}
throw error
})
});
const autocomplete = (input) => {
return {
@@ -16,6 +39,13 @@ export const clearAutocomplete = () => {
};
};
export const setNewToken = (token) => {
return {
type: "SET_NEW_TOKEN",
payload: token
}
}
export const fetchAutocomplete = (input) => {
return function (dispatch) {
axios
@@ -124,13 +154,14 @@ export const fetchDish = (id) => {
};
};
export const refreshUserData = (token) => {
export const refreshUserData = () => {
return function (dispatch) {
const state = store.getState()
axios({
url: backend + "user/refresh",
method: "POST",
headers: {
"x-auth-token": token,
"x-auth-token": state.data.userData.jwt,
},
})
.then((response) => {
@@ -138,7 +169,7 @@ export const refreshUserData = (token) => {
toggles.setLoggedIn(
response.data.firstname,
response.data.lastname,
token,
state.data.userData.jwt,
response.data.id,
response.data.email,
response.data.NIP,
@@ -149,7 +180,7 @@ export const refreshUserData = (token) => {
);
})
.catch((err) => {
if (err.status === 401) {
if (err.status === 401 || err.status === 500) {
dispatch(logout());
}
console.log(err);

View File

@@ -33,6 +33,7 @@ export default function Dish(props) {
})
.catch((err) => {
console.log(err);
throw err;
});
}, [id]);

View File

@@ -316,7 +316,7 @@ export default function EditDish() {
.then((response) => {
dispatch(hideBackdrop());
dispatch(notification("Danie zmienione.", "success"));
dispatch(refreshUserData(token));
dispatch(refreshUserData());
history.push(`/dish/${dish._id}`);
})
.catch((error) => {
@@ -324,6 +324,7 @@ export default function EditDish() {
dispatch(
notification("Wystąpił nieoczekiwany błąd, przepraszamy.", "error")
);
throw error;
});
};

View File

@@ -318,7 +318,7 @@ export default function NewRestaurant() {
.then((response) => {
dispatch(hideBackdrop());
dispatch(notification("Danie dodane pomyślnie.", "success"));
dispatch(refreshUserData(token));
dispatch(refreshUserData());
history.push(`/editRestaurant/${restaurantID}`);
})
.catch((error) => {
@@ -329,6 +329,7 @@ export default function NewRestaurant() {
"error"
)
);
throw error;
});
};

View File

@@ -202,7 +202,7 @@ export default function NewRestaurant() {
"success"
)
);
dispatch(refreshUserData(token));
dispatch(refreshUserData());
history.push("/");
})
.catch((error) => {
@@ -215,6 +215,7 @@ export default function NewRestaurant() {
)
);
history.push("/");
throw error;
});
};

View File

@@ -173,7 +173,7 @@ export default function EditRestaurantInfo(props) {
},
})
.then((response) => {
dispatch(refreshUserData(jwt));
dispatch(refreshUserData());
setState({ ...state, hidden: !state.hidden });
dispatch(hideBackdrop());
dispatch(notification("Widoczność zmieniona poprawnie", "success"));
@@ -182,6 +182,7 @@ export default function EditRestaurantInfo(props) {
console.log(err);
dispatch(hideBackdrop());
dispatch(notification("Wystąpił błąd :(", "error"));
throw err;
});
}
@@ -200,7 +201,7 @@ export default function EditRestaurantInfo(props) {
},
})
.then((response) => {
dispatch(refreshUserData(jwt));
dispatch(refreshUserData());
dispatch(hideBackdrop());
dispatch(notification("Restauracja została usunięta", "success"));
history.push("/");
@@ -209,6 +210,7 @@ export default function EditRestaurantInfo(props) {
console.log(err);
dispatch(hideBackdrop());
dispatch(notification("Wystąpił nieoczekiwany błąd :(", "error"));
throw err;
});
};
@@ -257,6 +259,7 @@ export default function EditRestaurantInfo(props) {
console.log(err);
dispatch(hideBackdrop());
dispatch(notification("Wystąpił nieoczekiwany błąd :(", "error"));
throw err;
});
}
};

View File

@@ -67,6 +67,7 @@ export default function EditRestaurantLocation(props) {
.catch((e) => {
dispatch(hideBackdrop());
dispatch(notification("Nie udało się zmienić lokalizacji :(", "error"));
throw e;
});
};

View File

@@ -69,11 +69,12 @@ export default function EditRestaurantMenu(props) {
.then((res) => {
dispatch(hideBackdrop());
dispatch(notification("Dodano kategorię.", "success"));
dispatch(refreshUserData(token));
dispatch(refreshUserData());
})
.catch((e) => {
dispatch(hideBackdrop());
dispatch(notification("Nie udało się dodać kategorii :(", "error"));
throw e;
});
}
};
@@ -96,11 +97,12 @@ export default function EditRestaurantMenu(props) {
.then((res) => {
dispatch(hideBackdrop());
dispatch(notification("Usunięto kategorię.", "success"));
dispatch(refreshUserData(token));
dispatch(refreshUserData());
})
.catch((e) => {
dispatch(hideBackdrop());
dispatch(notification("Nie udało się usunąć kategorii :(", "error"));
throw e;
});
};

View File

@@ -60,11 +60,12 @@ export default function EditRestaurantPhoto(props) {
.then((res) => {
dispatch(hideBackdrop());
dispatch(notification("Zmieniono zdjęcie.", "success"));
dispatch(refreshUserData(token));
dispatch(refreshUserData());
})
.catch((e) => {
dispatch(hideBackdrop());
dispatch(notification("Nie udało się zmienić zdjęcia :(", "error"));
throw e;
});
};
return (

View File

@@ -55,6 +55,7 @@ export default function EditRestaurantSubscription(props) {
.catch((error) => {
dispatch(hideBackdrop());
dispatch(notification("Wystąpił błąd, spróbuj ponownie.", "error"));
throw error;
});
};
@@ -63,7 +64,7 @@ export default function EditRestaurantSubscription(props) {
};
const onAccept = () => {
dispatch(refreshUserData(token));
dispatch(refreshUserData());
openInNewTab("https://secure.przelewy24.pl/trnRequest/0");
};

View File

@@ -159,11 +159,12 @@ export default function EditCategoriesList(props) {
.then((response) => {
dispatch(hideBackdrop());
dispatch(notification("Zmieniono zestaw.", "success"));
dispatch(refreshUserData(token));
dispatch(refreshUserData());
})
.catch((error) => {
dispatch(hideBackdrop());
dispatch(notification("Wystąpił błąd.", "error"));
throw error;
});
} else {
dispatch(notification("Popraw dane.", "error"));

View File

@@ -30,6 +30,7 @@ export default function ImageUpload(props) {
.catch((error) => {
console.log("Wystąpił błąd podczas wgrywania pliku.");
setLoading(false);
throw error;
});
};

View File

@@ -30,6 +30,7 @@ export default function ImageUpload(props) {
.catch((error) => {
console.log("Wystąpił błąd podczas wgrywania pliku.");
setLoading(false);
throw error;
});
};

View File

@@ -110,7 +110,7 @@ export default function EditDishList(props) {
.then((response) => {
dispatch(hideBackdrop());
dispatch(notification("Dodano do zestawu.", "success"));
dispatch(refreshUserData(token));
dispatch(refreshUserData());
})
.catch((error) => {
dispatch(hideBackdrop());

View File

@@ -59,7 +59,7 @@ export default function EditDishList(props) {
.then((response) => {
dispatch(hideBackdrop());
dispatch(notification("Zmodyfikowano zestaw.", "success"));
dispatch(refreshUserData(token));
dispatch(refreshUserData());
})
.catch((error) => {
dispatch(hideBackdrop());

View File

@@ -1,4 +1,5 @@
export const backend = "https://api.menui.pl/";
//export const backend = "https://api.menui.pl/";
export const backend = "http://localhost:4000/";
export const restaurantTypes = [
"afrykańska",
@@ -34,8 +35,4 @@ export const restaurantTypes = [
"żydowska",
"mieszane",
"inna",
];
export const authErrorInterceptor = (error) => {
}
];

View File

@@ -29,6 +29,8 @@ ReactDOM.render(
document.getElementById("root")
);
export default store;
// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers: https://bit.ly/CRA-PWA

View File

@@ -98,6 +98,8 @@ const data = (state = initialState, action) => {
}),
},
});
case "SET_NEW_TOKEN":
return (state = {...state, userData: {...state.userData, jwt: action.payload}})
default:
return state;
}