Skip to content

Commit

Permalink
fix(watcher): close #52 and undefined bug
Browse files Browse the repository at this point in the history
  • Loading branch information
breakingrobot authored and Pooya Parsa committed Feb 3, 2018
1 parent b628455 commit 2a03f2f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 15 deletions.
7 changes: 7 additions & 0 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
6 changes: 5 additions & 1 deletion lib/templates/auth.class.js
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -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
Expand Down
15 changes: 1 addition & 14 deletions lib/templates/auth.middleware.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
)
})
}
13 changes: 13 additions & 0 deletions lib/templates/auth.utilities.js
Original file line number Diff line number Diff line change
@@ -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)
)
})
}

0 comments on commit 2a03f2f

Please sign in to comment.