Skip to content

Commit

Permalink
feature: excluded components, simplified middleware and bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
breakingrobot authored and pi0 committed Jan 26, 2018
1 parent 774fdfa commit 0d085eb
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 14 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
method: 'GET',
},
redirect: {
guest: true,
user: true,
notLoggedIn: '/login',
loggedIn: '/'
},
Expand Down Expand Up @@ -81,9 +83,14 @@ Sets the global settings for store **logout** action.
* **cookieName** - Set the token name in Cookies.
#### redirect
* **guest** (Boolean) - Sets if the middleware should redirect guests users (non-authenticated).
* **user** (Boolean) - Sets if the middleware should redirect logged users (authenticated).
* **noAuth** (Boolean) - Sets if the middleware should redirect logged users (authenticated).
* **notLoggedIn** (Boolean) - Sets the redirect URL default of the users not logged in. Only when `auth` middleware is added to a page.
* **loggedIn** (Boolean) - Sets the redirect URL default of the users logged in. Only when `no-auth` middleware is added to a page.
## Example usage
```js
Expand Down Expand Up @@ -115,8 +122,7 @@ store.getters['auth/loggedIn'] // get login status (true or false)
// ... in nuxt.config.js ...
router: {
middleware: [
'auth', // If user not logged in, redirect to '/login' or to URL defined in redirect property
'no-auth' // If user is already logged in, redirect to '/' or to URL defined in redirect property
'auth',
]
}
```
Expand Down
4 changes: 3 additions & 1 deletion lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ module.exports = function (moduleOptions) {
method: 'GET',
},
redirect: {
guest: true,
user: true,
notLoggedIn: '/login',
loggedIn: '/'
},
Expand All @@ -29,7 +31,7 @@ module.exports = function (moduleOptions) {
name: 'token',
cookie: true,
cookieName: 'token'
}
},
}

const options = merge(defaults, moduleOptions, this.options.auth)
Expand Down
35 changes: 24 additions & 11 deletions lib/templates/auth.middleware.js
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 %>')
}
<% } %>
}
});
}

0 comments on commit 0d085eb

Please sign in to comment.