From 67168aa01a85ef023d0b4c4b56eb9ee34e443003 Mon Sep 17 00:00:00 2001 From: Roman Grishin Date: Tue, 12 Sep 2023 20:10:58 +0600 Subject: [PATCH] fixed requires headers api.js --- backend/middlewares/auth.js | 1 - frontend/.gitignore | 1 + frontend/src/components/App.js | 15 ++++----- frontend/src/components/Main/Main.js | 4 +-- frontend/src/utils/api.js | 50 +++++++++++++++++++--------- 5 files changed, 44 insertions(+), 27 deletions(-) create mode 100644 frontend/.gitignore diff --git a/backend/middlewares/auth.js b/backend/middlewares/auth.js index 76176f5..237440c 100644 --- a/backend/middlewares/auth.js +++ b/backend/middlewares/auth.js @@ -23,7 +23,6 @@ module.exports.auth = (req, res, next) => { } req.user = payload; - console.log(payload); return next(); }; diff --git a/frontend/.gitignore b/frontend/.gitignore new file mode 100644 index 0000000..42afabf --- /dev/null +++ b/frontend/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/frontend/src/components/App.js b/frontend/src/components/App.js index f8deca2..273243b 100644 --- a/frontend/src/components/App.js +++ b/frontend/src/components/App.js @@ -54,6 +54,7 @@ function App() { if(token) { AuthApi.checkToken(token) .then((res) => { + console.log(res); setUserData({ ...userData, _id: res.user._id, @@ -64,28 +65,26 @@ function App() { }) .catch((err) => console.log(err)); } - }, [loggedIn]); + }, []); useEffect(() => { - const token = localStorage.getItem('token'); - if(token) { + AppApi.getProfileInfo() .then((res) => { setCurrentUser(res.user); }) .catch((err) => console.log(err)); - } + }, [loggedIn]); useEffect(() => { - const token = localStorage.getItem('token'); - if(token) { + AppApi.getInitialCards() .then((res) => { setCards(res.data); }) .catch((err) => console.log(err)); - } + }, [loggedIn]); @@ -196,7 +195,7 @@ function App() { return } localStorage.setItem('token', res.token); - + setLoggedIn(true); navigate('/', {replace: true}); }) diff --git a/frontend/src/components/Main/Main.js b/frontend/src/components/Main/Main.js index 2ee0403..80523d4 100644 --- a/frontend/src/components/Main/Main.js +++ b/frontend/src/components/Main/Main.js @@ -10,9 +10,7 @@ function Main({onEditProfile, onAddPlace, onEditAvatar, onCardClick, onCardLike, const cardData = useContext(CardContext) || []; useEffect(() => { - setTimeout(() => { - - }, 1000) + // console.log(localStorage.getItem('token')); }, []); return ( diff --git a/frontend/src/utils/api.js b/frontend/src/utils/api.js index ceb7b78..839b72e 100644 --- a/frontend/src/utils/api.js +++ b/frontend/src/utils/api.js @@ -1,7 +1,6 @@ class Api { - constructor({baseUrl, headers}) { + constructor({baseUrl}) { this._url = baseUrl; - this._headers = headers; } _getResponseData(res) { @@ -13,7 +12,10 @@ class Api { getProfileInfo() { return fetch(`https://${this._url}/users/me`, { - headers: this._headers + headers: { + authorization: `Bearer ${localStorage.getItem('token')}`, + 'Content-type': 'application/json' + }, }) .then(res => { return this._getResponseData(res); @@ -23,7 +25,10 @@ class Api { changeProfileInfo(item) { return fetch(`https://${this._url}/users/me`, { method: 'PATCH', - headers: this._headers, + headers: { + authorization: `Bearer ${localStorage.getItem('token')}`, + 'Content-type': 'application/json' + }, body: JSON.stringify({ name: item.name, about: item.about @@ -40,7 +45,10 @@ class Api { pushCardToServer({name, link, like, id}) { return fetch(`https://${this._url}/cards`, { method: 'POST', - headers: this._headers, + headers: { + authorization: `Bearer ${localStorage.getItem('token')}`, + 'Content-type': 'application/json' + }, body: JSON.stringify({ name: name, link: link, @@ -56,7 +64,10 @@ class Api { removeCardFromServer(cardID) { return fetch(`https://${this._url}/cards/${cardID}`, { method: 'DELETE', - headers: this._headers + headers: { + authorization: `Bearer ${localStorage.getItem('token')}`, + 'Content-type': 'application/json' + }, }) .then(res => { return this._getResponseData(res); @@ -66,7 +77,10 @@ class Api { addLike(cardId) { return fetch(`https://${this._url}/cards/${cardId}/likes`, { method: 'PUT', - headers: this._headers + headers: { + authorization: `Bearer ${localStorage.getItem('token')}`, + 'Content-type': 'application/json' + }, }) .then(res => { return this._getResponseData(res); @@ -76,7 +90,10 @@ class Api { removeLike(cardId) { return fetch(`https://${this._url}/cards/${cardId}/likes`, { method: 'DELETE', - headers: this._headers + headers: { + authorization: `Bearer ${localStorage.getItem('token')}`, + 'Content-type': 'application/json' + }, }) .then(res => { return this._getResponseData(res); @@ -86,7 +103,10 @@ class Api { changeAvatar(link) { return fetch(`https://${this._url}/users/me/avatar`, { method: 'PATCH', - headers: this._headers, + headers: { + authorization: `Bearer ${localStorage.getItem('token')}`, + 'Content-type': 'application/json' + }, body: JSON.stringify({ avatar: link }) @@ -98,7 +118,11 @@ class Api { getInitialCards() { return fetch(`https://${this._url}/cards`, { - headers: this._headers + headers: { + authorization: `Bearer ${localStorage.getItem('token')}`, + 'Content-type': 'application/json' + }, + }) .then(res => { return this._getResponseData(res); @@ -107,11 +131,7 @@ class Api { } const AppApi = new Api({ - baseUrl: 'api.mentor.oddyhater.nomoredomainsicu.ru', - headers: { - authorization: `Bearer ${localStorage.getItem('token')}`, - 'Content-type': 'application/json' - } + baseUrl: 'api.mentor.oddyhater.nomoredomainsicu.ru' }); export default AppApi;