Skip to content

Commit

Permalink
fixed requires headers api.js
Browse files Browse the repository at this point in the history
  • Loading branch information
OddyHater committed Sep 12, 2023
1 parent 6fb36cf commit 67168aa
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 27 deletions.
1 change: 0 additions & 1 deletion backend/middlewares/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ module.exports.auth = (req, res, next) => {
}

req.user = payload;
console.log(payload);

return next();
};
1 change: 1 addition & 0 deletions frontend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
15 changes: 7 additions & 8 deletions frontend/src/components/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ function App() {
if(token) {
AuthApi.checkToken(token)
.then((res) => {
console.log(res);
setUserData({
...userData,
_id: res.user._id,
Expand All @@ -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]);


Expand Down Expand Up @@ -196,7 +195,7 @@ function App() {
return
}
localStorage.setItem('token', res.token);

setLoggedIn(true);
navigate('/', {replace: true});
})
Expand Down
4 changes: 1 addition & 3 deletions frontend/src/components/Main/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ function Main({onEditProfile, onAddPlace, onEditAvatar, onCardClick, onCardLike,
const cardData = useContext(CardContext) || [];

useEffect(() => {
setTimeout(() => {

}, 1000)
// console.log(localStorage.getItem('token'));
}, []);

return (
Expand Down
50 changes: 35 additions & 15 deletions frontend/src/utils/api.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
class Api {
constructor({baseUrl, headers}) {
constructor({baseUrl}) {
this._url = baseUrl;
this._headers = headers;
}

_getResponseData(res) {
Expand All @@ -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);
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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
})
Expand All @@ -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);
Expand All @@ -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;

0 comments on commit 67168aa

Please sign in to comment.