Last active
November 27, 2018 01:39
-
-
Save IgorDePaula/01a653af47d1235afc1319895d2f0162 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 config from './config' | |
import axios from 'axios' | |
import router from './router' | |
// import * as types from './app/auth/vuex/mutation-types' | |
import { Loading } from 'element-ui' | |
export const http = axios.create(config) | |
let loadingInstance = null | |
const interceptors = (http, store, route, config) => { | |
http.interceptors.request.use((config) => { | |
loadingInstance = Loading.service({ fullscreen: true, text: 'Carregando...' }) | |
return config | |
}, (error) => { | |
loadingInstance.close() | |
return Promise.reject(error) | |
}) | |
// Add a response interceptor | |
http.interceptors.response.use((response) => { | |
if (response.headers.hasOwnProperty('newtoken')) { | |
setToken(response.headers.newtoken) | |
} | |
loadingInstance.close() | |
return response | |
}, (error) => { | |
if (error.response.data.hasOwnProperty('error')) { | |
const errorMessage = error.response.data.error | |
// window.console.log(error) | |
if (errorMessage === 'token_expired' || errorMessage === 'token_invalid') { | |
store.dispatch('setUser', '') | |
store.dispatch('setTokenUser', '') | |
router.push('auth') | |
} | |
} | |
// const { response } = error | |
loadingInstance.close() | |
// window.console.log(error) | |
// window.console.log(response) | |
return Promise.reject(error) | |
}) | |
} | |
export function setToken (token) { | |
http.defaults.headers.common.Authorization = `Bearer ${token}` | |
} | |
export default function install (Vue, {router, store}) { | |
interceptors(http, store, router, config) | |
Vue.prototype.$http = http | |
} |
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 http from './http' | |
import leftColumn from './components/directives/left-column' | |
/* eslint-disable no-new */ | |
Vue.use(http, {store, router}) | |
... |
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 * as types from './mutation-types' | |
import { postLogin, getUserApi } from '../services' | |
import { setToken } from 'src/http' | |
export const attempLogin = (context, payload) => { | |
return postLogin(payload.email, payload.password) | |
.then(data => { | |
context.commit(types.setToken, data.token) | |
setToken(data.token) | |
attempUser(context, data.token) | |
}) | |
} | |
export const attempUser = (context, token) => { | |
return getUserApi(token) | |
.then(data => { | |
context.commit(types.setUser, data.user) | |
}) | |
} | |
export const setUser = (context, user) => { | |
context.commit(types.setUser, user) | |
} | |
export const setTokenUser = (context, token) => { | |
context.commit(types.setToken, token) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment