Skip to content

Instantly share code, notes, and snippets.

@srph
Created February 21, 2016 13:50
Show Gist options
  • Star 78 You must be signed in to star a gist
  • Fork 15 You must be signed in to fork a gist
  • 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);
});
@sauldeleon
Copy link

Thanks!

@harry-liu
Copy link

help a lot!

@andresinostroza
Copy link

What is the difference between use cookie-machine or localstorage?

@anselmobattisti
Copy link

thanks buddy helped a lot

@francisrod01
Copy link

Thank you so much! 👍

@ayhaadam
Copy link

ayhaadam commented Mar 6, 2018

This is so helpful

@andresabello
Copy link

Just what I was looking for!
Thank you@

@larizzatg
Copy link

Thanks :)

@faiyazbits
Copy link

thanks

@xicond
Copy link

xicond commented Oct 16, 2018

How about if token rejected/expired, any way to do in interceptor ?

@msayan
Copy link

msayan commented Oct 25, 2018

How about if token rejected/expired, any way to do in interceptor ?

Can you find any solution about this?

@srph
Copy link
Author

srph commented Nov 16, 2018

As for expired tokens, anyone may find this gist useful.

@pinoywebs123
Copy link

awesome

@akshayinnopix
Copy link

Thank u

Copy link

ghost commented Feb 21, 2019

hi there!
where or when this file oauth.js is called?
i created this file like you did, but axios dont put the token. iam sure that i have a cookie with my token.
thanks

@fico7489
Copy link

fico7489 commented Mar 1, 2019

👍

@ZakKa89
Copy link

ZakKa89 commented May 27, 2019

Thanks this works great

@mdao032
Copy link

mdao032 commented Jul 13, 2019

Thank you !

@winni4eva
Copy link

Thanks a million

@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