Skip to content

Commit

Permalink
fix: ssr and code reduction
Browse files Browse the repository at this point in the history
  • Loading branch information
breakingrobot authored and pi0 committed Jan 26, 2018
1 parent 0ca4169 commit 952700c
Showing 1 changed file with 25 additions and 23 deletions.
48 changes: 25 additions & 23 deletions lib/templates/auth.middleware.js
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)
} }
}); }
} }

0 comments on commit 952700c

Please sign in to comment.