Created
October 1, 2021 15:21
-
-
Save afdallah/f1787282d883c955eb4d4a5968cd31c3 to your computer and use it in GitHub Desktop.
This file contains 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' | |
import { getToken } from '../helpers/auth' | |
const BASE_URL = 'https://notflixtv.herokuapp.com/api/v1' | |
const API = axios.create({ | |
baseURL: BASE_URL | |
}) | |
API.get() | |
API.interceptors.request.use( | |
config => { | |
const token = getToken() | |
if (token) { | |
config.headers = { | |
Authorization: `Bearer ${token}` | |
} | |
} | |
return config | |
}, | |
error => Promise.reject(error) | |
) | |
API.interceptors.response.use(response => { | |
// Check apakah ada tokennya apa ngga | |
// Kalo ada save ke localstorage | |
if (response.data.data.token) { | |
localStorage.setItem('user', JSON.stringify(response.data.data)) | |
} | |
return response.data | |
}) | |
export default API |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment