• 1

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/post.php

Line Number: 190


File: /home2/naijacod/public_html/application/views/post.php
Line: 190
Function: _error_handler

File: /home2/naijacod/public_html/application/controllers/Posts.php
Line: 420
Function: view

File: /home2/naijacod/public_html/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

A better way to set authorization token in axios

Instead of always setting the auth token to the header every time you want to make an HTTP request with something like:

     const config = {
          headers: { Authorization: `Bearer ${accessToken}` }



A smarter way to achieve this is to set the authorization token in the header inside a new file that you can use anywhere in your codebase, you can make a new file and put the below code then import into any file and use.

const axios = require('axios');
axios.defaults.baseURL = 'http://localhost:1010/'
axios.defaults.headers.common = {'Authorization': `bearer ${token}`}
export default axios;
Added a comment on Feb 21,2020

To add to this, you can also do it this way

export default axios.create({
  baseURL: 'http://localhost:1010/',
  headers: {
    Authorization: `bearer ${token}`

  • 0
Reply Report
Added a comment on Feb 21,2020

Yes, this is synonymous to the first example, the issue with this is that you can’t use it in multiple places, you’ll always need to redefine the headers and all that but with the second example just add that in a file, import and just use the axios directly and it can access the token info.

  • 1
Reply Report