diff --git a/docs/api/auth.md b/docs/api/auth.md index 1e8ff1af0..0bc63887a 100644 --- a/docs/api/auth.md +++ b/docs/api/auth.md @@ -124,3 +124,16 @@ export default function({ app }) { }) } ``` + +### `onRedirect(handler)` + + Pre-process URLs before redirect: (`plugins/auth.js`) + + ```js +export default function({ app }) { + app.$auth.onRedirect((to, from) => { + console.error(to) + // you can optionally change `to` by returning a new value + }) +} +``` diff --git a/lib/core/auth.js b/lib/core/auth.js index e24eb3831..f2a76b22f 100644 --- a/lib/core/auth.js +++ b/lib/core/auth.js @@ -14,6 +14,9 @@ export default class Auth { // Error listeners this._errorListeners = [] + // Redirect listeners + this._redirectListeners = [] + // Storage & State options.initialState = { user: null, loggedIn: false } const storage = new Storage(ctx, options) @@ -352,6 +355,9 @@ export default class Auth { } } + // Call onRedirect hook + to = this.callOnRedirect(to, from) || to + // Prevent infinity redirects if (isSameURL(to, from)) { return @@ -368,6 +374,17 @@ export default class Auth { } } + onRedirect (listener) { + this._redirectListeners.push(listener) + } + + callOnRedirect (to, from) { + for (const fn of this._redirectListeners) { + to = fn(to, from) || to + } + return to + } + hasScope (scope) { const userScopes = this.$state.user && getProp(this.$state.user, this.options.scopeKey)