Skip to content

Commit

Permalink
misc: make error page working when auth middleware applied globally. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccallum authored and pi0 committed Apr 10, 2018
1 parent ad22695 commit c1b4cfd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/core/middleware.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import Middleware from '../middleware'
import { routeOption } from './utilities'
import { routeOption, getMatchedComponents } from './utilities'

Middleware.auth = function (ctx) {
// Disable middleware if options: { auth: false } is set on the route
if (routeOption(ctx.route, 'auth', false)) {
return
}

// Disable middleware if no route was matched to allow 404/error page
const matches = []
const Components = getMatchedComponents(ctx.route, matches)
if (!Components.length) {
return
}

const { login } = ctx.app.$auth.options.redirect

if (ctx.app.$auth.$state.loggedIn) {
Expand Down
9 changes: 9 additions & 0 deletions lib/core/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,12 @@ export const routeOption = (route, key, value) => {
}
})
}

export const getMatchedComponents = (route, matches = false) => {
return [].concat.apply([], route.matched.map(function (m, index) {
return Object.keys(m.components).map(function (key) {
matches && matches.push(index)
return m.components[key]
})
}))
}

0 comments on commit c1b4cfd

Please sign in to comment.