php - Vue.js requests sending a token, suddenly fail with 401 when testing on real server
Get the solution ↓↓↓I have a Vue.js application that has this in the api.js file.
Its the place where I put the Authorization headers before I make requests to the server.
Vue.http.options.root = 'http://localhost:8044'
Vue.http.options.api = Vue.http.options.root + '/api/v1/'
Vue.http.headers.common['Accept'] = 'application/json'
Vue.http.headers.common['Content-Type'] = 'application/json'
Vue.http.interceptors.push((request, next) => {
// set token
store.state.token && request.headers.set('Authorization', `Bearer ${store.state.token}`)
// continue to next interceptor
next((response) => {
console.log(response)
// DEBUG THE ROUTES
//console.log(store.state);
//console.log(response);
if (response.status === 401) {
router.push({ name: 'logout' })
}
})
})
Basically, if a route fails with a 401, it means the header token failed authorization, and I logout the user.
This works perfectly fine locally. If a 401 is returned it logs out, and if the header succeeds, the request is allowed to continue.
However, when I try to connect with the real server, as opposed to my localhost (both running identical installations of Lumen/App)
Vue.http.options.root = 'http://myserver.com'
It always returns a 401 no matter what. Even if I have valid token.
The first login request, returns a valid token, and works fine. Then I store the token, and use it in subsequent requests.
But then every subsequent request returns a 401, and immediately logs me out. Even though when I console log the request, it shows the Auth headers being used just fine.
Only on the real server. Not on the local one. Why would this be?
The other thing is this...If I fire the first request to get the token, and copy the token into Postman, and manually fire any request to the server, in question, it returns just fine with a 200. Not an Unauthorized 401.
I thought it could be a CORS issue, but I checked all that and I have CORS enabled on both servers, so its not that. What else could it be?
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: composer detected issues in your platform: your composer dependencies require a php version ">= 8.0.2".
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.