From 54b0720a887d22d73604a90afb102dc04f1c82c4 Mon Sep 17 00:00:00 2001 From: Ricardo Gobbo de Souza Date: Thu, 23 May 2019 16:45:46 -0300 Subject: [PATCH] feat(middleware): add guest option in auth middleware (#264) --- docs/middleware.md | 8 ++++++++ lib/core/middleware.js | 7 +++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/docs/middleware.md b/docs/middleware.md index 1ce0a2301..022e1e45e 100644 --- a/docs/middleware.md +++ b/docs/middleware.md @@ -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. diff --git a/lib/core/middleware.js b/lib/core/middleware.js index b732d1544..68836006e 100644 --- a/lib/core/middleware.js +++ b/lib/core/middleware.js @@ -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 {