PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, February 21, 2022

[FIXED] Send Sanctum token automaticaly

 February 21, 2022     laravel, vue.js     No comments   

Issue

I'm working on a Laravel 8 Project with Sanctum and VueJs, and I would like to know if there is any way to send the token without write each time header in my axios request:

const token = localStorage.getItem("token");
        axios
            .get("/api/endpoint/", {
                headers: {
                    "Content-Type": "application/json",
                    Authorization: "Bearer " + token
                }
            })
            .then(response => (this.expenses = response.data));
    }

Solution

Define axios config in lib or something like this:

import Axios from 'axios'

const axios = Axios.create({
    baseURL: process.env.NEXT_PUBLIC_SERVER_BASE_URL,
    headers: {
        'Content-Type': 'application/json',
        Authorization: "Bearer " + token
    },
})

export default axios

Then use your lib instead of base axios like this:

import axios from '@/lib/axios'

 axios.post(...)
        


Answered By - Ali Raza
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing