-
Notifications
You must be signed in to change notification settings - Fork 925
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: excluded components, simplified middleware and bug fix
- Loading branch information
1 parent
774fdfa
commit 0d085eb
Showing
3 changed files
with
35 additions
and
14 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
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
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,15 +1,28 @@ | ||
import middleware from './middleware' | ||
|
||
middleware.auth = function authMiddleware ({ store, redirect }) { | ||
// If user not logged in, redirect to /login | ||
if (!store.getters['auth/loggedIn']) { | ||
return redirect('<%= options.redirect.notLoggedIn %>') | ||
} | ||
} | ||
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 | ||
|
||
// Only apply the middleware to guarded routes | ||
// and exclude redirected paths to hit the middleware again. | ||
if (guarded && route.path !== '<%= options.redirect.notLoggedIn %>') { | ||
// Checking if guest redirection middleware is enabled | ||
<% if (options.redirect.guest) { %> | ||
// Guest is redirected back to login page. | ||
if (!store.getters['auth/loggedIn']) { | ||
return redirect('<%= options.redirect.notLoggedIn %>') | ||
} | ||
|
||
middleware['no-auth'] = function noAuthMiddleware ({ store, redirect }) { | ||
// If user is already logged in, redirect to / | ||
if (store.getters['auth/loggedIn']) { | ||
return redirect('<%= options.redirect.loggedIn %>') | ||
} | ||
// Checking if user redirection middleware is enabled | ||
<% } if (options.redirect.user) { %> | ||
// Guest is redirected back to login page | ||
if (store.getters['auth/loggedIn']) { | ||
return redirect('<%= options.redirect.loggedIn %>') | ||
} | ||
<% } %> | ||
} | ||
}); | ||
} |