Reputation: 1588
I have created some auth service in my react app.
It executes in the react App.js constructor like this:
if(isAuthenticated()){
console.log("its true");
}else{
console.log("its false");
}
The service is this one
import axios from 'axios'
import Cookies from 'js-cookie';
export default function isAuthenticated(){
var accesstoken = Cookies.get('accesstoken');
axios({ method: 'post', url: 'http://localhost:3003/verify', headers: { Authorization: `Bearer ${accesstoken}` } })
.then(function (response) {
//thats not he response im seeing in network/verify/response, its a different one which always returns the same stuff(wrong) while the one I see in my chrome console is the good one
console.log(response)
if(response.data === "OK"){
return true;
}else{
return false;
}
});
}
The problem is that the console.log(response) is logging an unexpected result, it doesnt result the same as my chrome console in the /network/verify(my POST request) tab
Shouldn't it return the same?
Upvotes: 0
Views: 1592
Reputation: 7770
Basically you need to either return the promise or use async await
export default function isAuthenticated(){
var accesstoken = Cookies.get('accesstoken');
return axios({ method: 'post', url: 'http://localhost:3003/verify', headers: { Authorization: `Bearer ${accesstoken}` } })
.then(function (response) {
//thats not he response im seeing in network/verify/response, its a different one which always returns the same stuff(wrong) while the one I see in my chrome console is the good one
console.log(response)
if(response.data === "OK"){
return true;
}else{
return false;
}
});
}
Upvotes: 1