Skip to content

Commit b628455

Browse files
author
Pooya Parsa
committed
feat: watchState and watchLoggedIn
#52
1 parent d9756e5 commit b628455

File tree

4 files changed

+27
-12
lines changed

4 files changed

+27
-12
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ Working with low level state: (Not recommended)
131131
this.$auth.setState(key, val)
132132
this.$auth.getState(key)
133133

134+
// Watch state changes
135+
this.$auth.watchState('loggedIn', newValue => { })
136+
134137
// Cookie
135138
this.$auth.setCookie(key, val, options)
136139
this.$auth.getCookie(key)
@@ -257,6 +260,12 @@ If enabled, user will be automatically logged out if any error happens. (For exa
257260
258261
If enabled, user will redirect back to the original guarded route instead of `redirects.home`.
259262
263+
### `watchLoggedIn`
264+
265+
* Default: `true`
266+
267+
If enabled, user will automatically redirected to `redirects.home` after login/logout.
268+
260269
### `namespace`
261270
262271
* Default: `auth`

lib/defaults.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module.exports = {
22
fetchUserOnLogin: true,
33
resetOnError: true,
44
rewriteRedirects: true,
5+
watchLoggedIn: true,
56
namespace: 'auth',
67
scopeKey: 'scope',
78
endpoints: {

lib/templates/auth.class.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ export default class Auth {
2222
this._resetOnError()
2323
}
2424

25+
// Watch for loggedIn changes only in client side
26+
if (this.options.watchLoggedIn && process.browser) {
27+
this._watchLoggedIn()
28+
}
29+
2530
this._registerVuexStore()
2631
}
2732

@@ -50,15 +55,13 @@ export default class Auth {
5055
})
5156
}
5257

53-
watchLoggedIn () {
58+
_watchLoggedIn () {
5459
this._loggedInWatcher =
5560
this._loggedInWatcher ||
56-
this.$store.watch(
57-
state => state[this.options.namespace]['loggedIn'],
58-
() => {
59-
this.redirect('home')
60-
}
61-
)
61+
this.watchState('loggedIn', () => {
62+
$auth.redirect('home')
63+
})
64+
6265
return this._loggedInWatcher
6366
}
6467

@@ -138,6 +141,13 @@ export default class Auth {
138141
return this.state[key]
139142
}
140143

144+
watchState (key, fn) {
145+
return this.$store.watch(
146+
state => getProp(state[this.options.namespace], key),
147+
fn
148+
)
149+
}
150+
141151
setLocalStorage (name, value) {
142152
if (typeof localStorage !== 'undefined') {
143153
if (value) {

lib/templates/auth.plugin.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ export default function (ctx, inject) {
1717
// Sync token
1818
$auth.syncToken()
1919

20-
// Watch for loggedIn changes only in client side
21-
if (process.browser) {
22-
$auth.watchLoggedIn()
23-
}
24-
2520
// Fetch user if is not available
2621
if (!$auth.state.user) {
2722
return $auth.fetchUser()

0 commit comments

Comments
 (0)