router.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright 2019 Hackware SpA <human@hackware.cl>
  2. // This file is part of "Hackware Userland" and licensed under the terms
  3. // of the GNU Affero General Public License version 3, or (at your option)
  4. // a later version. You should have received a copy of this license along
  5. // with the software. If not, see <https://www.gnu.org/licenses/>.
  6. import Vue from 'vue';
  7. import Router from 'vue-router';
  8. Vue.use(Router);
  9. const router = new Router({
  10. mode: 'history',
  11. base: process.env.BASE_URL,
  12. routes: [
  13. {
  14. path: '/',
  15. redirect: { name: 'wallet' },
  16. },
  17. {
  18. path: '/login',
  19. component: () => import('./views/Login.vue'),
  20. children: [
  21. {
  22. path: '',
  23. component: () => import('./views/LoginByPassword.vue'),
  24. name: 'login-by-password',
  25. },
  26. {
  27. path: 'email',
  28. name: 'login-by-email',
  29. component: () => import('./views/LoginByEmail.vue'),
  30. },
  31. ],
  32. },
  33. {
  34. path: '/add-funds',
  35. component: () => import('./views/AddFunds.vue'),
  36. children: [
  37. {
  38. path: '',
  39. component: () => import('./views/AddFundsPayment.vue'),
  40. name: 'add-funds-payment',
  41. },
  42. {
  43. path: 'verify',
  44. name: 'add-funds-verify',
  45. component: () => import('./views/AddFundsVerify.vue'),
  46. },
  47. {
  48. path: ':gateway',
  49. name: 'add-funds-gateway',
  50. component: () => import('./views/AddFundsGateway.vue'),
  51. },
  52. ],
  53. },
  54. {
  55. path: '/transactions',
  56. name: 'transactions',
  57. component: () => import('./views/Transactions.vue'),
  58. },
  59. {
  60. path: '/wallet',
  61. name: 'wallet',
  62. component: () => import('./views/Wallet.vue'),
  63. },
  64. ],
  65. });
  66. router.beforeEach((to, from, next) => {
  67. const { $auth } = router.app;
  68. if ($auth.isLoggedIn() || ['login-by-email', 'login-by-password'].includes(to.name)) {
  69. next();
  70. } else {
  71. router.app.$nextTick(() => {
  72. const app = router.app.$children[0]; // App.vue, is this too hacky?
  73. const authToken = ('auth_token' in to.query) ? to.query.auth_token : null;
  74. $auth.fetchUser(authToken, false)
  75. .then((response) => {
  76. if ([401, 403].includes(response.status)) {
  77. app.showSidebar = false;
  78. next({ name: 'login-by-password' });
  79. throw new Error(`${response.statusText} request to ${response.url}`);
  80. }
  81. return response.json();
  82. })
  83. .then((body) => {
  84. if ('error' in body) {
  85. next({ name: 'login-by-password' });
  86. throw new Error(body.error.message);
  87. }
  88. $auth.setUser(body);
  89. app.initSidebar();
  90. next();
  91. });
  92. });
  93. }
  94. });
  95. export default router;