main.ts 2.81 KB
Newer Older
Kevin Robert's avatar
Kevin Robert committed
1
2
3
4
5
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.common with an alias.
import Vue from 'vue';
import App from './app.vue';
import Vue2Filters from 'vue2-filters';
6
import VueDraggable from 'vue-draggable';
Kevin Robert's avatar
Kevin Robert committed
7
import router from './router';
8
import { metaInfo } from './shared/meta';
Kevin Robert's avatar
Kevin Robert committed
9
10
11
12
13
import * as config from './shared/config/config';

import AccountService from './account/account.service';
import AlertService from '@/shared/alert/alert.service';
import TranslationService from '@/locale/translation.service';
14
import FrontFormService from '@/front/services/front-form.service';
15
import { ROUTES } from '@/router/routes-constants';
16
import HeaderService from '@/shared/service/header-service';
Kevin Robert's avatar
Kevin Robert committed
17
18
19
20
21

/* tslint:enable */
Vue.config.productionTip = false;
config.initVueApp(Vue);
Vue.use(Vue2Filters);
22
Vue.use(VueDraggable);
Kevin Robert's avatar
Kevin Robert committed
23
24
25
26
27
28
29

const i18n = config.initI18N(Vue);
const store = config.initVueXStore(Vue);

const alertService = new AlertService(store);
const translationService = new TranslationService(store, i18n);
const accountService = new AccountService(store, translationService, router);
30
const headerService = new HeaderService(store, translationService, i18n);
31
const frontFormService = new FrontFormService(store, router, alertService, translationService, i18n);
Kevin Robert's avatar
Kevin Robert committed
32
33
34

router.beforeEach((to, from, next) => {
  if (!to.matched.length) {
35
    next(ROUTES.notFound.path);
Kevin Robert's avatar
Kevin Robert committed
36
37
38
  }

  if (to.meta && to.meta.authorities && to.meta.authorities.length > 0) {
39
40
    // si la page demandée est soumise à des droits,
    // on vérifie si l'utilisateur est autorisé à la consulter
Kevin Robert's avatar
Kevin Robert committed
41
42
    accountService.hasAnyAuthorityAndCheckAuth(to.meta.authorities).then(value => {
      if (!value) {
43
        // s'il n'est pas autorisé ou pas connecté, on sauvegarde l'url et on redirige sur la page d'accueil
Kevin Robert's avatar
Kevin Robert committed
44
        sessionStorage.setItem('requested-url', to.fullPath);
45
46
        next(ROUTES.login.path);
      } else {
47
        // s'il est autorisé et connecté, on continue
48
49
50
51
        next();
      }
    });
  } else if (to.fullPath === ROUTES.login.path) {
52
    // si la page demandée est la page de login (non soumise aux droits)
53
    accountService.checkAuth().then(value => {
54
      // si l'utilisateur est déjà connecté, on redirige vers la page d'accueil
55
56
      if (value) {
        next(ROUTES.admin.path);
Kevin Robert's avatar
Kevin Robert committed
57
      } else {
58
        // sinon on continue vers la page de login
Kevin Robert's avatar
Kevin Robert committed
59
60
61
62
63
64
65
66
67
68
69
70
        next();
      }
    });
  } else {
    // no authorities, so just proceed
    next();
  }
});

/* tslint:disable */
new Vue({
  el: '#app',
71
  components: { App },
Kevin Robert's avatar
Kevin Robert committed
72
73
  template: '<App/>',
  router,
74
  metaInfo,
Kevin Robert's avatar
Kevin Robert committed
75
76
77
78
  provide: {
    alertService: () => alertService,
    translationService: () => translationService,
    accountService: () => accountService,
79
    frontFormService: () => frontFormService,
80
    headerService: () => headerService,
Kevin Robert's avatar
Kevin Robert committed
81
82
83
84
  },
  i18n,
  store,
});