Skip to content

Commit 1b8856c

Browse files
ernestas-poskuspi0
authored andcommitted
feat: add fetchUser option (#27)
1 parent 9d1f050 commit 1b8856c

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
user: {
2929
endpoint: 'auth/user',
3030
propertyName: 'user',
31-
resetOnFail: true
31+
resetOnFail: true,
32+
enabled: true
3233
},
3334
login: {
3435
endpoint: 'auth/login',
@@ -92,16 +93,16 @@ store.dispatch('auth/login', {
9293
password: 'your_password'
9394
}
9495
})
95-
96+
9697
// ... code ...
9798
store.dispatch('auth/logout') // run logout
98-
99+
99100
// ... code ...
100101
store.state.auth.token // get access token
101-
102+
102103
// ... code ...
103104
store.state.auth.user // get user data
104-
105+
105106
// ... code ...
106107
store.getters['auth/loggedIn'] // get login status (true or false)
107108
```

lib/module.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ module.exports = function (moduleOptions) {
77
user: {
88
endpoint: 'auth/user',
99
propertyName: 'user',
10-
resetOnFail: true
10+
resetOnFail: true,
11+
enabled: true
1112
},
1213
login: {
1314
endpoint: 'auth/login',

lib/templates/auth.store.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export default {
88

99
state: () => ({
1010
<% if (options.token.enabled) { %>token: null,<% } %>
11-
user: null
11+
<% if (options.user.enabled) { %>user: null<% } %>
1212
}),
1313

1414
getters: {
@@ -18,10 +18,12 @@ export default {
1818
},
1919

2020
mutations: {
21+
<% if (options.user.enabled) { %>
2122
// SET_USER
2223
SET_USER (state, user) {
2324
state.user = user
2425
},
26+
<% } %>
2527

2628
<% if (options.token.enabled) { %>
2729
// SET_TOKEN
@@ -107,10 +109,11 @@ export default {
107109

108110
// Reset
109111
reset ({ dispatch, commit }) {
110-
commit('SET_USER', null)
112+
<% if (options.user.enabled) { %>commit('SET_USER', null)<% } %>
111113
<% if (options.token.enabled) { %>dispatch('updateToken', null)<% } %>
112114
},
113115

116+
<% if (options.user.enabled) { %>
114117
// Fetch
115118
async fetch ({ getters, state, commit, dispatch }, { endpoint = '<%= options.user.endpoint %>' } = {}) {
116119
<% if (options.token.enabled) { %>
@@ -135,6 +138,7 @@ export default {
135138
<% } %>
136139
}
137140
},
141+
<% } %>
138142

139143
// Login
140144
async login ({ dispatch }, { fields, endpoint = '<%= options.login.endpoint %>' } = {}) {
@@ -146,7 +150,9 @@ export default {
146150
<% } %>
147151

148152
// Fetch authenticated user
153+
<% if (options.user.enabled) { %>
149154
await dispatch('fetch')
155+
<% } %>
150156
},
151157

152158
// Logout

0 commit comments

Comments
 (0)