From e70f96686299102cc850d518a83280cba3c8dbef Mon Sep 17 00:00:00 2001 From: ricardovanlaarhoven Date: Thu, 24 Oct 2019 14:51:58 +0200 Subject: [PATCH 1/5] remove templateloader add child routes add route guards --- generator/index.js | 5 - .../src/guards/AuthorisationGuard | 9 ++ .../src/templates/Authorisation.vue | 0 generator/templates/Default/src/App.vue | 53 +------- generator/templates/Default/src/newmain.js | 3 + generator/templates/Default/src/router.js | 125 +++++++++--------- .../src/templates/Default.vue | 0 .../templates/TemplateLoader/src/App.vue | 30 ----- prompts.js | 8 +- 9 files changed, 78 insertions(+), 155 deletions(-) create mode 100644 generator/templates/Authorisation/src/guards/AuthorisationGuard rename generator/templates/{TemplateLoader => Authorisation}/src/templates/Authorisation.vue (100%) rename generator/templates/{TemplateLoader => Default}/src/templates/Default.vue (100%) delete mode 100644 generator/templates/TemplateLoader/src/App.vue diff --git a/generator/index.js b/generator/index.js index 818342b..6aab130 100644 --- a/generator/index.js +++ b/generator/index.js @@ -26,11 +26,6 @@ module.exports = (api, options) => { ...options, }); - if (options.useTemplateLoader) { - api.render('./templates/TemplateLoader', { - ...options, - }); - } if (options.useAuthorisation) { api.render('./templates/Authorisation', { ...options, diff --git a/generator/templates/Authorisation/src/guards/AuthorisationGuard b/generator/templates/Authorisation/src/guards/AuthorisationGuard new file mode 100644 index 0000000..4389023 --- /dev/null +++ b/generator/templates/Authorisation/src/guards/AuthorisationGuard @@ -0,0 +1,9 @@ +import Vue from 'vue'; + +export default function (to, from, next) { + if (!Vue.prototype.$store.getters['Authorization/isLoggedIn']) { + next({name: 'login'}); + return; + } + next(); +} diff --git a/generator/templates/TemplateLoader/src/templates/Authorisation.vue b/generator/templates/Authorisation/src/templates/Authorisation.vue similarity index 100% rename from generator/templates/TemplateLoader/src/templates/Authorisation.vue rename to generator/templates/Authorisation/src/templates/Authorisation.vue diff --git a/generator/templates/Default/src/App.vue b/generator/templates/Default/src/App.vue index de95cf1..e2ac7df 100644 --- a/generator/templates/Default/src/App.vue +++ b/generator/templates/Default/src/App.vue @@ -1,62 +1,17 @@ - - - diff --git a/generator/templates/Default/src/newmain.js b/generator/templates/Default/src/newmain.js index bed1da0..4999936 100644 --- a/generator/templates/Default/src/newmain.js +++ b/generator/templates/Default/src/newmain.js @@ -14,6 +14,9 @@ import './registerServiceWorker' Vue.prototype.$http = API; window.$http = API; +<%_ if (options.useAuthorisation) { _%> +Vue.prototype.$store = store; +<%_ } _%> <%_ if (options.useCrud) { _%> Vue.use(VuetifyResource); <%_ } _%> diff --git a/generator/templates/Default/src/router.js b/generator/templates/Default/src/router.js index 9e803a1..1d8b95f 100644 --- a/generator/templates/Default/src/router.js +++ b/generator/templates/Default/src/router.js @@ -12,73 +12,70 @@ export default new Router({ name: 'home', component: () => import('@/views/Home'), }, + // <%_ if (options.useAuthorisation) { _%> { - path: '/403', - name: '403', - component: () => import('./views/PageForbidden.vue'), + path: '', + component: () => import('@/templates/Authorisation'), + children: [ + { + path: '/login', + name: 'login', + component: () => import('@/views/Login'), + }, + { + path: '/password/forgotten', + name: 'password.forgotten', + component: () => import('./views/PasswordForgotten.vue'), + }, + { + path: '/password/reset/:token', + name: 'password.reset', + component: () => import('./views/PasswordReset.vue'), + }, + { + path: '/invitation/accept/:token', + name: 'invitation.accept', + component: () => import('./views/InvitationAccept.vue'), + }, + ] }, - <%_ if (options.useAuthorisation) { _%> + // <%_ } _%> { - path: '/login', - name: 'login', - component: () => import('@/views/Login'), - <%_ if (options.useTemplateLoader) { _%> - meta: { - template: () => import('./templates/Authorisation.vue'), - }, - <%_ } _%> - }, - { - path: '/password/forgotten', - name: 'password.forgotten', - component: () => import('./views/PasswordForgotten.vue'), - <%_ if (options.useTemplateLoader) { _%> - meta: { - template: () => import('./templates/Authorisation.vue'), - }, - <%_ } _%> - }, - { - path: '/password/reset/:token', - name: 'password.reset', - component: () => import('./views/PasswordReset.vue'), - <%_ if (options.useTemplateLoader) { _%> - meta: { - template: () => import('./templates/Authorisation.vue'), - }, - <%_ } _%> - }, - { - path: '/invitation/accept/:token', - name: 'invitation.accept', - component: () => import('./views/InvitationAccept.vue'), - <%_ if (options.useTemplateLoader) { _%> - meta: { - template: () => import('./templates/Authorisation.vue'), - }, - <%_ } _%> - }, - { - path: '/profile', - name: 'profile', - component: () => import('./views/Profile.vue') - }, - <%_ } _%> - <%_ if (options.useCrud) { _%> - { - path: '/users', - name: 'users', - component: () => import('./views/UserResource.vue') - }, - <%_ } _%> - { - path: '/404', - name: '404', - component: () => import('./views/PageNotFound.vue'), - }, - { - path: '*', - redirect: '/404' + path: '', + beforeEnter: AuthorisationGuard, + component: () => import('@/templates/Default'), + children: [ + // <%_ if (options.useAuthorisation) { _%> + { + path: '/profile', + name: 'profile', + component: () => import('./views/Profile.vue') + }, + // <%_ } _%> + // <%_ if (options.useCrud) { _%> + { + path: '/users', + name: 'users', + component: () => import('./views/UserResource.vue') + }, + // <%_ } _%> + { + path: '/404', + name: '404', + component: () => import('./views/PageNotFound.vue'), + }, + { + path: '/403', + name: '403', + component: () => import('./views/PageForbidden.vue'), + }, + { + path: '*', + redirect: '/404' + }, + ], }, + + ], }); diff --git a/generator/templates/TemplateLoader/src/templates/Default.vue b/generator/templates/Default/src/templates/Default.vue similarity index 100% rename from generator/templates/TemplateLoader/src/templates/Default.vue rename to generator/templates/Default/src/templates/Default.vue diff --git a/generator/templates/TemplateLoader/src/App.vue b/generator/templates/TemplateLoader/src/App.vue deleted file mode 100644 index abe97ce..0000000 --- a/generator/templates/TemplateLoader/src/App.vue +++ /dev/null @@ -1,30 +0,0 @@ - - - diff --git a/prompts.js b/prompts.js index 468d4ee..d31ef21 100644 --- a/prompts.js +++ b/prompts.js @@ -9,12 +9,6 @@ module.exports = [ message: 'Do you want to use a crud system?', default: true, }, - { - name: 'useTemplateLoader', - type: 'confirm', - message: 'Do you want to use a template loader (multiple templates configuration in the router)', - default: true, - }, { name: 'useAuthorisation', type: 'confirm', @@ -39,4 +33,4 @@ module.exports = [ message: 'What is the root API url? (you can change this later)', default: '//example.local/api', } -]; \ No newline at end of file +]; From 31f25ad8150f557aab5e2c83bbb2e24ce0634eb0 Mon Sep 17 00:00:00 2001 From: ricardovanlaarhoven Date: Thu, 24 Oct 2019 15:06:30 +0200 Subject: [PATCH 2/5] remove comments and add home to protected routes --- generator/templates/Default/src/router.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/generator/templates/Default/src/router.js b/generator/templates/Default/src/router.js index 1d8b95f..3dc0782 100644 --- a/generator/templates/Default/src/router.js +++ b/generator/templates/Default/src/router.js @@ -7,12 +7,7 @@ export default new Router({ mode: 'history', base: process.env.BASE_URL, routes: [ - { - path: '/', - name: 'home', - component: () => import('@/views/Home'), - }, - // <%_ if (options.useAuthorisation) { _%> + <%_ if (options.useAuthorisation) { _%> { path: '', component: () => import('@/templates/Authorisation'), @@ -39,26 +34,31 @@ export default new Router({ }, ] }, - // <%_ } _%> + <%_ } _%> { path: '', beforeEnter: AuthorisationGuard, component: () => import('@/templates/Default'), children: [ - // <%_ if (options.useAuthorisation) { _%> + { + path: '/', + name: 'home', + component: () => import('@/views/Home'), + }, + <%_ if (options.useAuthorisation) { _%> { path: '/profile', name: 'profile', component: () => import('./views/Profile.vue') }, - // <%_ } _%> - // <%_ if (options.useCrud) { _%> + <%_ } _%> + <%_ if (options.useCrud) { _%> { path: '/users', name: 'users', component: () => import('./views/UserResource.vue') }, - // <%_ } _%> + <%_ } _%> { path: '/404', name: '404', From 95cca77f9415743f36b293bec73b1529c8ff6cad Mon Sep 17 00:00:00 2001 From: ricardovanlaarhoven Date: Thu, 24 Oct 2019 15:06:48 +0200 Subject: [PATCH 3/5] add Authorisation guard to route --- generator/templates/Default/src/router.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/generator/templates/Default/src/router.js b/generator/templates/Default/src/router.js index 3dc0782..355fafe 100644 --- a/generator/templates/Default/src/router.js +++ b/generator/templates/Default/src/router.js @@ -1,5 +1,8 @@ import Vue from 'vue'; import Router from 'vue-router'; +<%_ if (options.useAuthorisation) { _%> +import AuthorisationGuard from './guards/AuthorisationGuard'; +<%_ } _%> Vue.use(Router); From e6b89ac1e0b34a436c531545a0ad9630dabea8f7 Mon Sep 17 00:00:00 2001 From: ricardovanlaarhoven Date: Thu, 24 Oct 2019 15:07:07 +0200 Subject: [PATCH 4/5] optionaly apply guard --- generator/templates/Default/src/router.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/generator/templates/Default/src/router.js b/generator/templates/Default/src/router.js index 355fafe..584874a 100644 --- a/generator/templates/Default/src/router.js +++ b/generator/templates/Default/src/router.js @@ -40,7 +40,9 @@ export default new Router({ <%_ } _%> { path: '', + <%_ if (options.useAuthorisation) { _%> beforeEnter: AuthorisationGuard, + <%_ } _%> component: () => import('@/templates/Default'), children: [ { From d2326e0f3c22583e7fc5e6574b91db23c69d4df1 Mon Sep 17 00:00:00 2001 From: ricardovanlaarhoven Date: Fri, 25 Oct 2019 13:51:30 +0200 Subject: [PATCH 5/5] add a redirect to the home route --- generator/templates/Default/src/router.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/generator/templates/Default/src/router.js b/generator/templates/Default/src/router.js index 584874a..11a6fc7 100644 --- a/generator/templates/Default/src/router.js +++ b/generator/templates/Default/src/router.js @@ -10,6 +10,10 @@ export default new Router({ mode: 'history', base: process.env.BASE_URL, routes: [ + { + path: '', + redirect: {name: 'home'}, + }, <%_ if (options.useAuthorisation) { _%> { path: '',