From 2a03f2f64f7d031ad40fe7403856b00d3bfbcd18 Mon Sep 17 00:00:00 2001 From: Tony BRIET Date: Sat, 3 Feb 2018 22:06:57 +0100 Subject: [PATCH] fix(watcher): close #52 and undefined bug --- lib/module.js | 7 +++++++ lib/templates/auth.class.js | 6 +++++- lib/templates/auth.middleware.js | 15 +-------------- lib/templates/auth.utilities.js | 13 +++++++++++++ 4 files changed, 26 insertions(+), 15 deletions(-) create mode 100644 lib/templates/auth.utilities.js diff --git a/lib/module.js b/lib/module.js index 26d9dba4b..fbd9eb13f 100644 --- a/lib/module.js +++ b/lib/module.js @@ -40,6 +40,13 @@ module.exports = function (moduleOptions) { options }) + // Utilities + this.addTemplate({ + src: resolve(__dirname, './templates/auth.utilities.js'), + fileName: 'auth/auth.utilities.js', + options + }) + // Class this.addTemplate({ src: resolve(__dirname, './templates/auth.class.js'), diff --git a/lib/templates/auth.class.js b/lib/templates/auth.class.js index 8056a749f..1c665edb8 100644 --- a/lib/templates/auth.class.js +++ b/lib/templates/auth.class.js @@ -1,5 +1,6 @@ import Cookies from 'js-cookie' import { parse as parseCookie } from 'cookie' +import { routeOption } from './auth.utilities' import getProp from 'dotprop' import Vue from 'vue' @@ -59,7 +60,10 @@ export default class Auth { this._loggedInWatcher = this._loggedInWatcher || this.watchState('loggedIn', () => { - $auth.redirect('home') + if (routeOption(this.$route, 'auth', false)) { + return + } + this.redirect('home') }) return this._loggedInWatcher diff --git a/lib/templates/auth.middleware.js b/lib/templates/auth.middleware.js index 2c6eba63f..f94a54dc2 100644 --- a/lib/templates/auth.middleware.js +++ b/lib/templates/auth.middleware.js @@ -1,4 +1,5 @@ import Middleware from '../middleware' +import { routeOption } from "./auth.utilities"; Middleware.auth = function (ctx) { // Disable middleware if options: { auth: false } is set on the route @@ -20,17 +21,3 @@ Middleware.auth = function (ctx) { ctx.app.$auth.redirect('login') } } - -// Utility to get route option -function routeOption (route, key, value) { - return route.matched.some(m => { - // Browser - if (process.browser) { - return Object.values(m.components).some(component => component.options[key] === value) - } - // SSR - return Object.values(m.components).some(component => - Object.values(component._Ctor).some(ctor => ctor.options && ctor.options[key] === value) - ) - }) -} diff --git a/lib/templates/auth.utilities.js b/lib/templates/auth.utilities.js new file mode 100644 index 000000000..2c67887b1 --- /dev/null +++ b/lib/templates/auth.utilities.js @@ -0,0 +1,13 @@ +// Utility to get route option +export const routeOption = (route, key, value) => { + return route.matched.some(m => { + // Browser + if (process.browser) { + return Object.values(m.components).some(component => component.options[key] === value) + } + // SSR + return Object.values(m.components).some(component => + Object.values(component._Ctor).some(ctor => ctor.options && ctor.options[key] === value) + ) + }) +}