Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ritechoice23's avatar

Authorization header with laravel Precognition

Please I am using precognition vue SPA, I use sanctum for authentication, please how can I add the token to the request for authenticated routes?

I have passed it as the third arg in the config, it is not working. please help me out, thanks in advance.

0 likes
3 replies
hupp's avatar

@horlatech i don't know about your code. But you can compare this snippet with your code to check correct way to set token in header.

// Import Axios and set up a base URL if needed
import axios from 'axios';

// Set up Axios request interceptor
axios.interceptors.request.use(config => {
  // Retrieve the token from where you stored it (e.g., Vuex store or localStorage)
  const token = localStorage.getItem('token'); // Make sure to replace 'token' with your actual token key
  
  // If a token exists, add it to the request headers
  if (token) {
    config.headers.Authorization = `Bearer ${token}`;
  }
  
  return config;
}, error => {
  return Promise.reject(error);
});

Let me know your feedback.

ritechoice23's avatar

@hupp Thanks for your response, I am talking about laravel precognition, not a literal Axios request.

Please or to participate in this conversation.