Permalink
Browse files

fix(middleware): remove trailing slash from redirect paths (#235)

  • Loading branch information...
sghgh1996 authored and pi0 committed Sep 7, 2018
1 parent 21e22ca commit c401122621665c4ca26a76c32d4d81ec1eab0288
Showing with 15 additions and 3 deletions.
  1. +3 −3 lib/core/middleware.js
  2. +12 −0 lib/core/utilities.js
@@ -1,5 +1,5 @@
import Middleware from '../middleware'
import { routeOption, getMatchedComponents } from './utilities'
import { routeOption, getMatchedComponents, normalizePath } from './utilities'
Middleware.auth = function (ctx) {
// Disable middleware if options: { auth: false } is set on the route
@@ -19,15 +19,15 @@ Middleware.auth = function (ctx) {
if (ctx.app.$auth.$state.loggedIn) {
// -- Authorized --
// Redirect to home page if inside login page (or login page disabled)
if (!login || ctx.route.path === login.split('?')[0]) {
if (!login || normalizePath(ctx.route.path) === normalizePath(login)) {
ctx.app.$auth.redirect('home')
}
} else {
// -- Guest --
// Redirect to login page if not authorized and not inside callback page
// (Those passing `callback` at runtime need to mark their callback component
// with `auth: false` to avoid an unnecessary redirect from callback to login)
if (!callback || ctx.route.path !== callback.split('?')[0]) {
if (!callback || normalizePath(ctx.route.path) !== normalizePath(callback)) {
ctx.app.$auth.redirect('login')
}
}
@@ -53,3 +53,15 @@ export const getMatchedComponents = (route, matches = false) => {
})
}))
}
export function normalizePath (path = '') {
// Remove query string
let result = path.split('?')[0]
// Remove redundant / from the end of path
if (result.charAt(result.length - 1) === '/') {
result = result.slice(0, -1)
}
return result
}

0 comments on commit c401122

Please sign in to comment.