Created
February 21, 2016 13:50
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); |
help a lot!
What is the difference between use cookie-machine or localstorage?
thanks buddy helped a lot
Thank you so much! 👍
This is so helpful
Just what I was looking for!
Thank you@
Thanks :)
thanks
How about if token rejected/expired, any way to do in interceptor ?
How about if token rejected/expired, any way to do in interceptor ?
Can you find any solution about this?
As for expired tokens, anyone may find this gist useful.
awesome
Thank u
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
👍
Thanks this works great
Thank you !
Thanks a million
Thanks so much! This help a lot
Thank you for your help!
Thank you!
Thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks!