Sending Requests with Axios
With Interceptors
You can use axios
as you regularly do using this method.
makeSuper
function: API Reference
The SuperTokensRequest.makeSuper(axios);
supertokens.axios.makeSuper(axios);
- Adds interceptors to the given
axios
instance. - To be called at least once on an axios instance before using it.
Example
import SuperTokensRequest from 'supertokens-website/axios';
import axios from "axios";
SuperTokensRequest.makeSuper(axios);
SuperTokensRequest.init("/refresh", 440);
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 === SESSION_EXPIRED_STATUS_CODE) {
// redirect user to login
} else {
// handle error
}
}
}
supertokens.axios.makeSuper(axios);
supertokens.axios.init("/refresh", 440, true);
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 === SESSION_EXPIRED_STATUS_CODE) {
// redirect user to login
} else {
// handle error
}
}
}
If your API domain is different than your website domain, then be sure to add
withCredentials: true
in the axios config call.
Without Interceptors
You need to replace all your axios
calls as shown below.
API Reference
The axios function:SuperTokensRequest.axios("/someAPI", config);
supertokens.axios.axios("/someAPI", config);
- Used to send a request to your API endpoint
- Treat it like a regular
axios
call - There are other functions to specifically call
get
,post
,delete
andput
methods. They can be seen in the API reference page.
import SuperTokensRequest from 'supertokens-website/axios';
SuperTokensRequest.init("/refreshTokenURL", 440, false);
async function doAPICalls() {
try {
let postData = { ... };
let response = await SuperTokensRequest.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 === SESSION_EXPIRED_STATUS_CODE) {
// redirect user to login
} else {
// handle error
}
}
}
supertokens.axios.init("/refreshTokenURL", 440, false);
async function doAPICalls() {
try {
let postData = { ... };
let response = await SuperTokensRequest.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 === SESSION_EXPIRED_STATUS_CODE) {
// redirect user to login
} else {
// handle error
}
}
}
If your API domain is different than your website domain, then be sure to add
withCredentials: true
in the axios config call.