Changeset View
Changeset View
Standalone View
Standalone View
src/resources/js/app.js
/** | /** | ||||
* First we will load all of this project's JavaScript dependencies which | * First we will load all of this project's JavaScript dependencies which | ||||
* includes Vue and other libraries. It is a great starting point when | * includes Vue and other libraries. It is a great starting point when | ||||
* building robust, powerful web applications using Vue and Laravel. | * building robust, powerful web applications using Vue and Laravel. | ||||
*/ | */ | ||||
require('./bootstrap') | require('./bootstrap') | ||||
import AppComponent from '../vue/App' | import AppComponent from '../vue/App' | ||||
import MenuComponent from '../vue/Widgets/Menu' | import MenuComponent from '../vue/Widgets/Menu' | ||||
import SupportForm from '../vue/Widgets/SupportForm' | import SupportForm from '../vue/Widgets/SupportForm' | ||||
import { loadLangAsync, i18n } from './locale' | import { loadLangAsync, i18n } from './locale' | ||||
import { clearFormValidation, pick, startLoading, stopLoading } from './utils' | import { clearFormValidation, pick, startLoading, stopLoading } from './utils' | ||||
const routerState = { | const routerState = { | ||||
afterLogin: null, | afterLogin: null, | ||||
isLoggedIn: !!localStorage.getItem('token') | isLoggedIn: !!localStorage.getItem('token'), | ||||
isLocked: false | |||||
} | } | ||||
let loadingRoute | let loadingRoute | ||||
// Note: This has to be before the app is created | // Note: This has to be before the app is created | ||||
// Note: You cannot use app inside of the function | // Note: You cannot use app inside of the function | ||||
window.router.beforeEach((to, from, next) => { | window.router.beforeEach((to, from, next) => { | ||||
// check if the route requires authentication and user is not logged in | // check if the route requires authentication and user is not logged in | ||||
if (to.meta.requiresAuth && !routerState.isLoggedIn) { | if (to.meta.requiresAuth && !routerState.isLoggedIn) { | ||||
// remember the original request, to use after login | // remember the original request, to use after login | ||||
routerState.afterLogin = to; | routerState.afterLogin = to; | ||||
// redirect to login page | // redirect to login page | ||||
next({ name: 'login' }) | next({ name: 'login' }) | ||||
return | |||||
} | |||||
if (routerState.isLocked && to.meta.requiresAuth && !['login', 'wallet'].includes(to.name)) { | |||||
// redirect to the wallet page | |||||
next({ name: 'wallet' }) | |||||
return | return | ||||
} | } | ||||
if (to.meta.loading) { | if (to.meta.loading) { | ||||
startLoading() | startLoading() | ||||
loadingRoute = to.name | loadingRoute = to.name | ||||
} | } | ||||
▲ Show 20 Lines • Show All 93 Lines • ▼ Show 20 Lines | methods: { | ||||
localStorage.setItem('token', response.access_token) | localStorage.setItem('token', response.access_token) | ||||
localStorage.setItem('refreshToken', response.refresh_token) | localStorage.setItem('refreshToken', response.refresh_token) | ||||
if (response.email) { | if (response.email) { | ||||
this.authInfo = response | this.authInfo = response | ||||
} | } | ||||
routerState.isLocked = this.authInfo && this.authInfo.isLocked | |||||
if (dashboard !== false) { | if (dashboard !== false) { | ||||
this.$router.push(routerState.afterLogin || { name: 'dashboard' }) | this.$router.push(routerState.afterLogin || { name: response.redirect || 'dashboard' }) | ||||
} else if (routerState.isLocked && this.$route.name != 'wallet' && this.$route.meta.requiresAuth) { | |||||
// Always redirect locked user, here we can be after router's beforeEach handler | |||||
this.$router.push({ name: 'wallet' }) | |||||
} | } | ||||
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 | ||||
▲ Show 20 Lines • Show All 164 Lines • ▼ Show 20 Lines | methods: { | ||||
} | } | ||||
if (!obj.isReady) { | if (!obj.isReady) { | ||||
return this.$t('status.notready') | return this.$t('status.notready') | ||||
} | } | ||||
return this.$t('status.active') | return this.$t('status.active') | ||||
}, | }, | ||||
unlock() { | |||||
routerState.isLocked = this.authInfo.isLocked = false | |||||
this.$router.push({ name: 'dashboard' }) | |||||
}, | |||||
// Append some wallet properties to the object | // Append some wallet properties to the object | ||||
userWalletProps(object) { | userWalletProps(object) { | ||||
let wallet = this.authInfo.accounts[0] | let wallet = this.authInfo.accounts[0] | ||||
if (!wallet) { | if (!wallet) { | ||||
wallet = this.authInfo.wallets[0] | wallet = this.authInfo.wallets[0] | ||||
} | } | ||||
▲ Show 20 Lines • Show All 159 Lines • Show Last 20 Lines |