-
Notifications
You must be signed in to change notification settings - Fork 927
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0ca4169
commit 952700c
Showing
1 changed file
with
25 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,31 @@ | ||
import middleware from './middleware' | ||
|
||
middleware.auth = function authMiddleware ({ route, redirect, store }) { | ||
route.matched.some((currentRoute) => { | ||
// Retriving Auth Guard status through route's component options. | ||
const options = currentRoute.components.default.options | ||
const guarded = options.guarded | ||
const isRouteGuarded = ({ route: { matched: m } }) => | ||
m.some(({ components: c }) => process.server | ||
? Object.values(c).some(({ _Ctor: _c }) => | ||
Object.values(_c).some(({ options: o }) => o && o.guarded) | ||
) | ||
: Object.values(c).some(o => o.options.guarded) | ||
) | ||
|
||
// Only apply the middleware to guarded routes | ||
if (guarded) { | ||
// Checking if guest redirection middleware is enabled | ||
<% if (options.redirect.guest) { %> | ||
// Guest is redirected back to login page | ||
// and excluding redirected paths from hitting the middleware again. | ||
if (!store.getters['auth/loggedIn'] && route.path !== '<%= options.redirect.notLoggedIn %>') { | ||
return redirect('<%= options.redirect.notLoggedIn %>') | ||
} | ||
|
||
// Checking if user redirection middleware is enabled | ||
<% } if (options.redirect.user) { %> | ||
// Guest is redirected back to login page | ||
// and excluding redirected paths from hitting the middleware again. | ||
if (store.getters['auth/loggedIn'] && route.path !== '<%= options.redirect.loggedIn %>') { | ||
return redirect('<%= options.redirect.loggedIn %>') | ||
} | ||
<% } %> | ||
middleware.auth = function(context) { | ||
const { route: {path: p}, redirect: r, store: s } = context | ||
|
||
// Registering guest and auth routes. | ||
let guestRoute = '<%= options.redirect.notLoggedIn %>' | ||
let authRoute = '<%= options.redirect.loggedIn %>' | ||
|
||
// Retriving Auth Guard status through route's component options. | ||
let g = isRouteGuarded(context) | ||
// Apply the middleware to guarded routes | ||
if (g) { | ||
if (<%= options.redirect.guest %> && !s.getters['auth/loggedIn'] && guestRoute !== p) { | ||
return r(guestRoute) | ||
} | ||
|
||
if (<% options.redirect.user %> && s.getters['auth/loggedIn'] && authRoute !== p) { | ||
return r(authRoute) | ||
} | ||
}); | ||
} | ||
} |