Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { store } from '@/store'
- import { addNotification } from '@/store/slices/NotificationSlice'
- import { clearUserData } from '@/store/slices/User'
- import axios from 'axios'
- import Router from 'next/router'
- import { setupCache } from 'axios-cache-interceptor'
- const axiosInstance = axios.create()
- const cacheOptions = {
- ttl: 1000 * 60 * 5,
- debug: (message) => {
- // Etc
- console.log(message)
- },
- }
- const AXIOS = setupCache(axiosInstance, cacheOptions)
- AXIOS.defaults.baseURL = process.env.NEXT_PUBLIC_API_URL
- console.log(setupCache)
- export const AXIOS_SET_AUTH_TOKEN = (token) => {
- AXIOS.defaults.headers.common.Authorization = "Bearer "+token
- }
- AXIOS.interceptors.response.use(
- function (response) {
- if (response.config.notification) {
- store.dispatch(
- addNotification({
- type: 'success',
- message: response.data.message,
- })
- )
- }
- return {
- res: response.data.data,
- status: true,
- }
- },
- function (error) {
- store.dispatch(
- addNotification({
- type: 'error',
- message: error.response.data.message,
- })
- )
- if (error.response.status === '401' || error.response.status === 401) {
- store.dispatch(clearUserData())
- AXIOS_SET_AUTH_TOKEN('')
- Router.push('/login')
- }
- return {
- res: error.response,
- status: false,
- }
- }
- )
- export default AXIOS
Add Comment
Please, Sign In to add comment