JWT refresh
This commit is contained in:
@@ -2,6 +2,29 @@ import axios from "axios";
|
|||||||
import * as toggles from "./toggles";
|
import * as toggles from "./toggles";
|
||||||
import { push } from "connected-react-router";
|
import { push } from "connected-react-router";
|
||||||
import { backend } from "../config.js";
|
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) => {
|
const autocomplete = (input) => {
|
||||||
return {
|
return {
|
||||||
@@ -16,6 +39,13 @@ export const clearAutocomplete = () => {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const setNewToken = (token) => {
|
||||||
|
return {
|
||||||
|
type: "SET_NEW_TOKEN",
|
||||||
|
payload: token
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export const fetchAutocomplete = (input) => {
|
export const fetchAutocomplete = (input) => {
|
||||||
return function (dispatch) {
|
return function (dispatch) {
|
||||||
axios
|
axios
|
||||||
@@ -124,13 +154,14 @@ export const fetchDish = (id) => {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export const refreshUserData = (token) => {
|
export const refreshUserData = () => {
|
||||||
return function (dispatch) {
|
return function (dispatch) {
|
||||||
|
const state = store.getState()
|
||||||
axios({
|
axios({
|
||||||
url: backend + "user/refresh",
|
url: backend + "user/refresh",
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
"x-auth-token": token,
|
"x-auth-token": state.data.userData.jwt,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
@@ -138,7 +169,7 @@ export const refreshUserData = (token) => {
|
|||||||
toggles.setLoggedIn(
|
toggles.setLoggedIn(
|
||||||
response.data.firstname,
|
response.data.firstname,
|
||||||
response.data.lastname,
|
response.data.lastname,
|
||||||
token,
|
state.data.userData.jwt,
|
||||||
response.data.id,
|
response.data.id,
|
||||||
response.data.email,
|
response.data.email,
|
||||||
response.data.NIP,
|
response.data.NIP,
|
||||||
@@ -149,7 +180,7 @@ export const refreshUserData = (token) => {
|
|||||||
);
|
);
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
if (err.status === 401) {
|
if (err.status === 401 || err.status === 500) {
|
||||||
dispatch(logout());
|
dispatch(logout());
|
||||||
}
|
}
|
||||||
console.log(err);
|
console.log(err);
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ export default function Dish(props) {
|
|||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
console.log(err);
|
console.log(err);
|
||||||
|
throw err;
|
||||||
});
|
});
|
||||||
}, [id]);
|
}, [id]);
|
||||||
|
|
||||||
|
|||||||
@@ -316,7 +316,7 @@ export default function EditDish() {
|
|||||||
.then((response) => {
|
.then((response) => {
|
||||||
dispatch(hideBackdrop());
|
dispatch(hideBackdrop());
|
||||||
dispatch(notification("Danie zmienione.", "success"));
|
dispatch(notification("Danie zmienione.", "success"));
|
||||||
dispatch(refreshUserData(token));
|
dispatch(refreshUserData());
|
||||||
history.push(`/dish/${dish._id}`);
|
history.push(`/dish/${dish._id}`);
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
@@ -324,6 +324,7 @@ export default function EditDish() {
|
|||||||
dispatch(
|
dispatch(
|
||||||
notification("Wystąpił nieoczekiwany błąd, przepraszamy.", "error")
|
notification("Wystąpił nieoczekiwany błąd, przepraszamy.", "error")
|
||||||
);
|
);
|
||||||
|
throw error;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -318,7 +318,7 @@ export default function NewRestaurant() {
|
|||||||
.then((response) => {
|
.then((response) => {
|
||||||
dispatch(hideBackdrop());
|
dispatch(hideBackdrop());
|
||||||
dispatch(notification("Danie dodane pomyślnie.", "success"));
|
dispatch(notification("Danie dodane pomyślnie.", "success"));
|
||||||
dispatch(refreshUserData(token));
|
dispatch(refreshUserData());
|
||||||
history.push(`/editRestaurant/${restaurantID}`);
|
history.push(`/editRestaurant/${restaurantID}`);
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
@@ -329,6 +329,7 @@ export default function NewRestaurant() {
|
|||||||
"error"
|
"error"
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
throw error;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -202,7 +202,7 @@ export default function NewRestaurant() {
|
|||||||
"success"
|
"success"
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
dispatch(refreshUserData(token));
|
dispatch(refreshUserData());
|
||||||
history.push("/");
|
history.push("/");
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
@@ -215,6 +215,7 @@ export default function NewRestaurant() {
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
history.push("/");
|
history.push("/");
|
||||||
|
throw error;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -173,7 +173,7 @@ export default function EditRestaurantInfo(props) {
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
dispatch(refreshUserData(jwt));
|
dispatch(refreshUserData());
|
||||||
setState({ ...state, hidden: !state.hidden });
|
setState({ ...state, hidden: !state.hidden });
|
||||||
dispatch(hideBackdrop());
|
dispatch(hideBackdrop());
|
||||||
dispatch(notification("Widoczność zmieniona poprawnie", "success"));
|
dispatch(notification("Widoczność zmieniona poprawnie", "success"));
|
||||||
@@ -182,6 +182,7 @@ export default function EditRestaurantInfo(props) {
|
|||||||
console.log(err);
|
console.log(err);
|
||||||
dispatch(hideBackdrop());
|
dispatch(hideBackdrop());
|
||||||
dispatch(notification("Wystąpił błąd :(", "error"));
|
dispatch(notification("Wystąpił błąd :(", "error"));
|
||||||
|
throw err;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -200,7 +201,7 @@ export default function EditRestaurantInfo(props) {
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
dispatch(refreshUserData(jwt));
|
dispatch(refreshUserData());
|
||||||
dispatch(hideBackdrop());
|
dispatch(hideBackdrop());
|
||||||
dispatch(notification("Restauracja została usunięta", "success"));
|
dispatch(notification("Restauracja została usunięta", "success"));
|
||||||
history.push("/");
|
history.push("/");
|
||||||
@@ -209,6 +210,7 @@ export default function EditRestaurantInfo(props) {
|
|||||||
console.log(err);
|
console.log(err);
|
||||||
dispatch(hideBackdrop());
|
dispatch(hideBackdrop());
|
||||||
dispatch(notification("Wystąpił nieoczekiwany błąd :(", "error"));
|
dispatch(notification("Wystąpił nieoczekiwany błąd :(", "error"));
|
||||||
|
throw err;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -257,6 +259,7 @@ export default function EditRestaurantInfo(props) {
|
|||||||
console.log(err);
|
console.log(err);
|
||||||
dispatch(hideBackdrop());
|
dispatch(hideBackdrop());
|
||||||
dispatch(notification("Wystąpił nieoczekiwany błąd :(", "error"));
|
dispatch(notification("Wystąpił nieoczekiwany błąd :(", "error"));
|
||||||
|
throw err;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -67,6 +67,7 @@ export default function EditRestaurantLocation(props) {
|
|||||||
.catch((e) => {
|
.catch((e) => {
|
||||||
dispatch(hideBackdrop());
|
dispatch(hideBackdrop());
|
||||||
dispatch(notification("Nie udało się zmienić lokalizacji :(", "error"));
|
dispatch(notification("Nie udało się zmienić lokalizacji :(", "error"));
|
||||||
|
throw e;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -69,11 +69,12 @@ export default function EditRestaurantMenu(props) {
|
|||||||
.then((res) => {
|
.then((res) => {
|
||||||
dispatch(hideBackdrop());
|
dispatch(hideBackdrop());
|
||||||
dispatch(notification("Dodano kategorię.", "success"));
|
dispatch(notification("Dodano kategorię.", "success"));
|
||||||
dispatch(refreshUserData(token));
|
dispatch(refreshUserData());
|
||||||
})
|
})
|
||||||
.catch((e) => {
|
.catch((e) => {
|
||||||
dispatch(hideBackdrop());
|
dispatch(hideBackdrop());
|
||||||
dispatch(notification("Nie udało się dodać kategorii :(", "error"));
|
dispatch(notification("Nie udało się dodać kategorii :(", "error"));
|
||||||
|
throw e;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -96,11 +97,12 @@ export default function EditRestaurantMenu(props) {
|
|||||||
.then((res) => {
|
.then((res) => {
|
||||||
dispatch(hideBackdrop());
|
dispatch(hideBackdrop());
|
||||||
dispatch(notification("Usunięto kategorię.", "success"));
|
dispatch(notification("Usunięto kategorię.", "success"));
|
||||||
dispatch(refreshUserData(token));
|
dispatch(refreshUserData());
|
||||||
})
|
})
|
||||||
.catch((e) => {
|
.catch((e) => {
|
||||||
dispatch(hideBackdrop());
|
dispatch(hideBackdrop());
|
||||||
dispatch(notification("Nie udało się usunąć kategorii :(", "error"));
|
dispatch(notification("Nie udało się usunąć kategorii :(", "error"));
|
||||||
|
throw e;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -60,11 +60,12 @@ export default function EditRestaurantPhoto(props) {
|
|||||||
.then((res) => {
|
.then((res) => {
|
||||||
dispatch(hideBackdrop());
|
dispatch(hideBackdrop());
|
||||||
dispatch(notification("Zmieniono zdjęcie.", "success"));
|
dispatch(notification("Zmieniono zdjęcie.", "success"));
|
||||||
dispatch(refreshUserData(token));
|
dispatch(refreshUserData());
|
||||||
})
|
})
|
||||||
.catch((e) => {
|
.catch((e) => {
|
||||||
dispatch(hideBackdrop());
|
dispatch(hideBackdrop());
|
||||||
dispatch(notification("Nie udało się zmienić zdjęcia :(", "error"));
|
dispatch(notification("Nie udało się zmienić zdjęcia :(", "error"));
|
||||||
|
throw e;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -55,6 +55,7 @@ export default function EditRestaurantSubscription(props) {
|
|||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
dispatch(hideBackdrop());
|
dispatch(hideBackdrop());
|
||||||
dispatch(notification("Wystąpił błąd, spróbuj ponownie.", "error"));
|
dispatch(notification("Wystąpił błąd, spróbuj ponownie.", "error"));
|
||||||
|
throw error;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -63,7 +64,7 @@ export default function EditRestaurantSubscription(props) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const onAccept = () => {
|
const onAccept = () => {
|
||||||
dispatch(refreshUserData(token));
|
dispatch(refreshUserData());
|
||||||
openInNewTab("https://secure.przelewy24.pl/trnRequest/0");
|
openInNewTab("https://secure.przelewy24.pl/trnRequest/0");
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -159,11 +159,12 @@ export default function EditCategoriesList(props) {
|
|||||||
.then((response) => {
|
.then((response) => {
|
||||||
dispatch(hideBackdrop());
|
dispatch(hideBackdrop());
|
||||||
dispatch(notification("Zmieniono zestaw.", "success"));
|
dispatch(notification("Zmieniono zestaw.", "success"));
|
||||||
dispatch(refreshUserData(token));
|
dispatch(refreshUserData());
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
dispatch(hideBackdrop());
|
dispatch(hideBackdrop());
|
||||||
dispatch(notification("Wystąpił błąd.", "error"));
|
dispatch(notification("Wystąpił błąd.", "error"));
|
||||||
|
throw error;
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
dispatch(notification("Popraw dane.", "error"));
|
dispatch(notification("Popraw dane.", "error"));
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ export default function ImageUpload(props) {
|
|||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
console.log("Wystąpił błąd podczas wgrywania pliku.");
|
console.log("Wystąpił błąd podczas wgrywania pliku.");
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
|
throw error;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ export default function ImageUpload(props) {
|
|||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
console.log("Wystąpił błąd podczas wgrywania pliku.");
|
console.log("Wystąpił błąd podczas wgrywania pliku.");
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
|
throw error;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -110,7 +110,7 @@ export default function EditDishList(props) {
|
|||||||
.then((response) => {
|
.then((response) => {
|
||||||
dispatch(hideBackdrop());
|
dispatch(hideBackdrop());
|
||||||
dispatch(notification("Dodano do zestawu.", "success"));
|
dispatch(notification("Dodano do zestawu.", "success"));
|
||||||
dispatch(refreshUserData(token));
|
dispatch(refreshUserData());
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
dispatch(hideBackdrop());
|
dispatch(hideBackdrop());
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ export default function EditDishList(props) {
|
|||||||
.then((response) => {
|
.then((response) => {
|
||||||
dispatch(hideBackdrop());
|
dispatch(hideBackdrop());
|
||||||
dispatch(notification("Zmodyfikowano zestaw.", "success"));
|
dispatch(notification("Zmodyfikowano zestaw.", "success"));
|
||||||
dispatch(refreshUserData(token));
|
dispatch(refreshUserData());
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
dispatch(hideBackdrop());
|
dispatch(hideBackdrop());
|
||||||
|
|||||||
@@ -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 = [
|
export const restaurantTypes = [
|
||||||
"afrykańska",
|
"afrykańska",
|
||||||
@@ -34,8 +35,4 @@ export const restaurantTypes = [
|
|||||||
"żydowska",
|
"żydowska",
|
||||||
"mieszane",
|
"mieszane",
|
||||||
"inna",
|
"inna",
|
||||||
];
|
];
|
||||||
|
|
||||||
export const authErrorInterceptor = (error) => {
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -29,6 +29,8 @@ ReactDOM.render(
|
|||||||
document.getElementById("root")
|
document.getElementById("root")
|
||||||
);
|
);
|
||||||
|
|
||||||
|
export default store;
|
||||||
|
|
||||||
// If you want your app to work offline and load faster, you can change
|
// If you want your app to work offline and load faster, you can change
|
||||||
// unregister() to register() below. Note this comes with some pitfalls.
|
// unregister() to register() below. Note this comes with some pitfalls.
|
||||||
// Learn more about service workers: https://bit.ly/CRA-PWA
|
// Learn more about service workers: https://bit.ly/CRA-PWA
|
||||||
|
|||||||
@@ -98,6 +98,8 @@ const data = (state = initialState, action) => {
|
|||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
case "SET_NEW_TOKEN":
|
||||||
|
return (state = {...state, userData: {...state.userData, jwt: action.payload}})
|
||||||
default:
|
default:
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user