Changeset View
Changeset View
Standalone View
Standalone View
src/resources/js/app.js
Show First 20 Lines • Show All 128 Lines • ▼ Show 20 Lines | methods: { | ||||
loginUser(response, dashboard, update) { | loginUser(response, dashboard, update) { | ||||
if (!update) { | if (!update) { | ||||
routerState.isLoggedIn = true | routerState.isLoggedIn = true | ||||
this.authInfo = null | this.authInfo = null | ||||
} | } | ||||
localStorage.setItem('token', response.access_token) | localStorage.setItem('token', response.access_token) | ||||
localStorage.setItem('refreshToken', response.refresh_token) | localStorage.setItem('refreshToken', response.refresh_token) | ||||
axios.defaults.headers.common.Authorization = 'Bearer ' + response.access_token | |||||
if (response.email) { | if (response.email) { | ||||
this.authInfo = response | this.authInfo = response | ||||
} | } | ||||
if (dashboard !== false) { | if (dashboard !== false) { | ||||
this.$router.push(routerState.afterLogin || { name: 'dashboard' }) | this.$router.push(routerState.afterLogin || { name: 'dashboard' }) | ||||
} | } | ||||
routerState.afterLogin = null | routerState.afterLogin = null | ||||
// Refresh the token before it expires | // Refresh the token before it expires | ||||
let timeout = response.expires_in || 0 | let timeout = response.expires_in || 0 | ||||
// We'll refresh 60 seconds before the token expires | // We'll refresh 60 seconds before the token expires | ||||
if (timeout > 60) { | if (timeout > 60) { | ||||
timeout -= 60 | timeout -= 60 | ||||
} | } | ||||
// TODO: We probably should try a few times in case of an error | // TODO: We probably should try a few times in case of an error | ||||
// TODO: We probably should prevent axios from doing any requests | // TODO: We probably should prevent axios from doing any requests | ||||
// while the token is being refreshed | // while the token is being refreshed | ||||
this.refreshTimeout = setTimeout(() => { | this.refreshTimeout = setTimeout(() => { | ||||
axios.post('api/auth/refresh', { refresh_token: response.refresh_token }).then(response => { | axios.post('api/auth/refresh', { refresh_token: localStorage.getItem('refreshToken') }).then(response => { | ||||
this.loginUser(response.data, false, true) | this.loginUser(response.data, false, true) | ||||
}) | }) | ||||
}, timeout * 1000) | }, timeout * 1000) | ||||
}, | }, | ||||
// Set user state to "not logged in" | // Set user state to "not logged in" | ||||
logoutUser(redirect) { | logoutUser(redirect) { | ||||
routerState.isLoggedIn = true | routerState.isLoggedIn = true | ||||
this.authInfo = null | this.authInfo = null | ||||
▲ Show 20 Lines • Show All 181 Lines • ▼ Show 20 Lines | |||||
// Add a axios request interceptor | // Add a axios request interceptor | ||||
axios.interceptors.request.use( | axios.interceptors.request.use( | ||||
config => { | config => { | ||||
// This is the only way I found to change configuration options | // This is the only way I found to change configuration options | ||||
// on a running application. We need this for browser testing. | // on a running application. We need this for browser testing. | ||||
config.headers['X-Test-Payment-Provider'] = window.config.paymentProvider | config.headers['X-Test-Payment-Provider'] = window.config.paymentProvider | ||||
// Set the Authorization header. Note that some request might force | |||||
// empty Authorization header therefore we check if the header is already set, | |||||
// not whether it's empty | |||||
const token = localStorage.getItem('token') | |||||
if (token && !('Authorization' in config.headers)) { | |||||
config.headers.Authorization = 'Bearer ' + token | |||||
} | |||||
let loader = config.loader | let loader = config.loader | ||||
if (loader) { | if (loader) { | ||||
startLoading(loader) | startLoading(loader) | ||||
} | } | ||||
return config | return config | ||||
}, | }, | ||||
error => { | error => { | ||||
▲ Show 20 Lines • Show All 117 Lines • Show Last 20 Lines |