Sending Requests with Axios
axios
interceptors#
Step 1) Add - Via NPM
- Via Script Tag
import SuperTokens from 'supertokens-website';
SuperTokens.addAxiosInterceptors(axios);
supertokens.addAxiosInterceptors(axios);
important
To be called on each axios
instances that is being imported.
axios
as usual#
Step 2) Use - Via NPM
- Via Script Tag
import SuperTokens from 'supertokens-website';
import axios from "axios";
SuperTokens.addAxiosInterceptors(axios);
SuperTokens.init({
apiDomain: "https://api.example.com"
});
async function doAPICalls() {
try {
let postData = { /* ... */ };
let response = await axios({url: "someAPI", method: "post", data: postData });
let data = await response.data;
let someField = data.someField;
} catch (err) {
if (err.response !== undefined && err.response.status === 401) {
// redirect user to login
} else {
// handle error
}
}
}
supertokens.addAxiosInterceptors(axios);
supertokens.init({
apiDomain: "https://api.example.com"
});
async function doAPICalls() {
try {
let postData = { /* ... */ };
let response = await axios({url: "someAPI", method: "post", data: postData });
let data = await response.data;
let someField = data.someField;
} catch (err) {
if (err.response !== undefined && err.response.status === 401) {
// redirect user to login
} else {
// handle error
}
}
}