Skip to content

Instantly share code, notes, and snippets.

@srph
Created February 21, 2016 13:50
Show Gist options
  • Select an option

  • Save srph/38f67a10e991b6cb2d83 to your computer and use it in GitHub Desktop.

Select an option

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
Copy Markdown

Thanks!

@harry-liu

Copy link
Copy Markdown

help a lot!

@andresinostroza

Copy link
Copy Markdown

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

@anselmobattisti

Copy link
Copy Markdown

thanks buddy helped a lot

@francisrod01

Copy link
Copy Markdown

Thank you so much! 👍

@ayhaadam

ayhaadam commented Mar 6, 2018

Copy link
Copy Markdown

This is so helpful

@andresabello

Copy link
Copy Markdown

Just what I was looking for!
Thank you@

@larizzatg

Copy link
Copy Markdown

Thanks :)

@faiyazbits

Copy link
Copy Markdown

thanks

@xicond

xicond commented Oct 16, 2018

Copy link
Copy Markdown

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

@msayan

msayan commented Oct 25, 2018

Copy link
Copy Markdown

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

Can you find any solution about this?

@srph

srph commented Nov 16, 2018

Copy link
Copy Markdown
Author

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

@pinoywebs123

Copy link
Copy Markdown

awesome

@akshayinnopix

Copy link
Copy Markdown

Thank u

ghost commented Feb 21, 2019

Copy link
Copy Markdown

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

fico7489 commented Mar 1, 2019

Copy link
Copy Markdown

👍

@ZakKa89

ZakKa89 commented May 27, 2019

Copy link
Copy Markdown

Thanks this works great

@mdao032

mdao032 commented Jul 13, 2019

Copy link
Copy Markdown

Thank you !

@winni4eva

Copy link
Copy Markdown

Thanks a million

@tranduchuy

Copy link
Copy Markdown

Thanks so much! This help a lot

@LucasCercal

Copy link
Copy Markdown

Thank you for your help!

@silasstoffel

Copy link
Copy Markdown

Thank you!

@xino1010

xino1010 commented Jul 8, 2021

Copy link
Copy Markdown

Thanks!

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