Skip to content

Commit

Permalink
feat(middleware): add guest option in auth middleware (#264)
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardogobbosouza authored and pi0 committed May 23, 2019
1 parent 162bc54 commit 54b0720
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
8 changes: 8 additions & 0 deletions docs/middleware.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,12 @@ export default {
}
```

You can set `auth` option to `guest` in a specific component. When this middleware is enabled on a route and `loggedIn` is `true` user will be redirected to `redirect.home` route. (`/` by default)

```js
export default {
auth: 'guest'
}
```

👉 Now you have to configure some [Strategies](schemes/README.md) for auth.
7 changes: 5 additions & 2 deletions lib/core/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ Middleware.auth = function (ctx) {

if (ctx.app.$auth.$state.loggedIn) {
// -- Authorized --
// Redirect to home page if inside login page (or login page disabled)
if (!login || normalizePath(ctx.route.path) === normalizePath(login)) {
// Redirect to home page if:
// - inside login page
// - login page disabled
// - options: { auth: 'guest' } is set on the page
if (!login || normalizePath(ctx.route.path) === normalizePath(login) || routeOption(ctx.route, 'auth', 'guest')) {
ctx.app.$auth.redirect('home')
}
} else {
Expand Down

0 comments on commit 54b0720

Please sign in to comment.