diff --git a/lib/core/middleware.js b/lib/core/middleware.js index 8b5306abd..b732d1544 100644 --- a/lib/core/middleware.js +++ b/lib/core/middleware.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,7 +19,7 @@ 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 { @@ -27,7 +27,7 @@ Middleware.auth = function (ctx) { // 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') } } diff --git a/lib/core/utilities.js b/lib/core/utilities.js index 9ddb554d4..04bcc32aa 100644 --- a/lib/core/utilities.js +++ b/lib/core/utilities.js @@ -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 +}