Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add onRedirect hook to pre-process URLs before redirection #185

Merged
merged 4 commits into from
May 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions docs/api/auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
}
```
17 changes: 17 additions & 0 deletions lib/core/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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)

Expand Down