Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(auth): add path helper function #235

Merged
merged 2 commits into from
Sep 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}