/
middleware.js
34 lines (30 loc) · 1.18 KB
/
middleware.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import Middleware from '../middleware'
import { routeOption, getMatchedComponents, normalizePath } 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, callback } = ctx.app.$auth.options.redirect
if (ctx.app.$auth.$state.loggedIn) {
// -- Authorized --
// Redirect to home page if inside login page (or login page disabled)
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 || normalizePath(ctx.route.path) !== normalizePath(callback)) {
ctx.app.$auth.redirect('login')
}
}
}