diff --git a/docs/api/auth.md b/docs/api/auth.md index 4c8c12b68..7fbac3dfa 100644 --- a/docs/api/auth.md +++ b/docs/api/auth.md @@ -111,3 +111,16 @@ export default function({ $auth }) { }) } ``` + +### `onRedirect(handler)` + +Pre-process URLs before redirect: (`plugins/auth.js`) + +```js +export default function({ app }) { + app.$auth.onRedirect((to) => { + console.log(to) + return to + }) +} +``` diff --git a/lib/core/auth.js b/lib/core/auth.js index 37d890877..42366c507 100644 --- a/lib/core/auth.js +++ b/lib/core/auth.js @@ -14,6 +14,9 @@ export default class Auth { // Error listeners this._errorListeners = [] + // Redirect listener + this._redirectListeners = [] + // Storage & State options.initialState = { user: null, loggedIn: false } const storage = new Storage(ctx, options) @@ -310,6 +313,8 @@ export default class Auth { return } + to = this.callOnRedirect(to) + // Apply rewrites if (this.options.rewriteRedirects) { if (name === 'login' && isRelativeURL(from) && !isSameURL(to, from)) { @@ -342,6 +347,18 @@ export default class Auth { } } + onRedirect (listener) { + this._redirectListeners.push(listener) + } + + callOnRedirect (to) { + for (let fn of this._redirectListeners) { + to = fn(to) + } + + return to + } + hasScope (scope) { const userScopes = this.$state.user && getProp(this.$state.user, this.options.scopeKey)