Skip to content

Commit

Permalink
feat: support onRedirect hook (#185)
Browse files Browse the repository at this point in the history
Co-authored-by: Paul Gascou <paul.gascvail@gmail.com>
  • Loading branch information
pooya parsa and paulgv committed May 23, 2019
1 parent e5579e9 commit aacb191
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
13 changes: 13 additions & 0 deletions docs/api/auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
}
```
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 listeners
this._redirectListeners = []

// Storage & State
options.initialState = { user: null, loggedIn: false }
const storage = new Storage(ctx, options)
Expand Down Expand Up @@ -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
Expand All @@ -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)

Expand Down

0 comments on commit aacb191

Please sign in to comment.