Skip to content

Instantly share code, notes, and snippets.

@srph
Created February 21, 2016 13:50
Show Gist options
  • Save srph/38f67a10e991b6cb2d83 to your computer and use it in GitHub Desktop.
Save srph/38f67a10e991b6cb2d83 to your computer and use it in GitHub Desktop.
axios: interceptor which includes your oauth token in every request as an Authorization header
import axios from 'axios';
// You can use any cookie library or whatever
// library to access your client storage.
import cookie from 'cookie-machine';
axios.interceptors.request.use(function(config) {
const token = cookie.get(__TOKEN_KEY__);
if ( token != null ) {
config.headers.Authorization = `Bearer ${token}`;
}
return config;
}, function(err) {
return Promise.reject(err);
});
@tranduchuy
Copy link

Thanks so much! This help a lot

@LucasCercal
Copy link

Thank you for your help!

@silasstoffel
Copy link

Thank you!

@xino1010
Copy link

xino1010 commented Jul 8, 2021

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment