Skip to content

Commit

Permalink
fix(middleware): remove trailing slash from redirect paths (#235)
Browse files Browse the repository at this point in the history
  • Loading branch information
sghgh1996 authored and pi0 committed Sep 7, 2018
1 parent 315bf4b commit 398a515
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/core/middleware.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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')
}
}
Expand Down
12 changes: 12 additions & 0 deletions lib/core/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 398a515

Please sign in to comment.