-
Notifications
You must be signed in to change notification settings - Fork 924
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,29 +1,31 @@ | |||
import middleware from './middleware' | import middleware from './middleware' | ||
|
|
||
middleware.auth = function authMiddleware ({ route, redirect, store }) { | const isRouteGuarded = ({ route: { matched: m } }) => | ||
route.matched.some((currentRoute) => { | m.some(({ components: c }) => process.server | ||
// Retriving Auth Guard status through route's component options. | ? Object.values(c).some(({ _Ctor: _c }) => | ||
const options = currentRoute.components.default.options | Object.values(_c).some(({ options: o }) => o && o.guarded) | ||
const guarded = options.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 | middleware.auth = function(context) { | ||
<% } if (options.redirect.user) { %> | const { route: {path: p}, redirect: r, store: s } = context | ||
// Guest is redirected back to login page |
|
||
// and excluding redirected paths from hitting the middleware again. | // Registering guest and auth routes. | ||
if (store.getters['auth/loggedIn'] && route.path !== '<%= options.redirect.loggedIn %>') { | let guestRoute = '<%= options.redirect.notLoggedIn %>' | ||
return redirect('<%= options.redirect.loggedIn %>') | 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) | |||
} | } | ||
}); | } | ||
} | } |