Permalink
Browse files

feat: add fetchUser option (#27)

  • Loading branch information...
ernestas-poskus authored and pi0 committed Dec 24, 2017
1 parent 9d1f050 commit 1b8856c3e74f6c1b8fe575c960aa10b46b686eb6
Showing with 16 additions and 8 deletions.
  1. +6 −5 README.md
  2. +2 −1 lib/module.js
  3. +8 −2 lib/templates/auth.store.js
@@ -28,7 +28,8 @@
user: {
endpoint: 'auth/user',
propertyName: 'user',
resetOnFail: true
resetOnFail: true,
enabled: true
},
login: {
endpoint: 'auth/login',
@@ -92,16 +93,16 @@ store.dispatch('auth/login', {
password: 'your_password'
}
})
// ... code ...
store.dispatch('auth/logout') // run logout
// ... code ...
store.state.auth.token // get access token
// ... code ...
store.state.auth.user // get user data
// ... code ...
store.getters['auth/loggedIn'] // get login status (true or false)
```
@@ -7,7 +7,8 @@ module.exports = function (moduleOptions) {
user: {
endpoint: 'auth/user',
propertyName: 'user',
resetOnFail: true
resetOnFail: true,
enabled: true
},
login: {
endpoint: 'auth/login',
@@ -8,7 +8,7 @@ export default {
state: () => ({
<% if (options.token.enabled) { %>token: null,<% } %>
user: null
<% if (options.user.enabled) { %>user: null<% } %>
}),
getters: {
@@ -18,10 +18,12 @@ export default {
},
mutations: {
<% if (options.user.enabled) { %>
// SET_USER
SET_USER (state, user) {
state.user = user
},
<% } %>
<% if (options.token.enabled) { %>
// SET_TOKEN
@@ -107,10 +109,11 @@ export default {
// Reset
reset ({ dispatch, commit }) {
commit('SET_USER', null)
<% if (options.user.enabled) { %>commit('SET_USER', null)<% } %>
<% if (options.token.enabled) { %>dispatch('updateToken', null)<% } %>
},
<% if (options.user.enabled) { %>
// Fetch
async fetch ({ getters, state, commit, dispatch }, { endpoint = '<%= options.user.endpoint %>' } = {}) {
<% if (options.token.enabled) { %>
@@ -135,6 +138,7 @@ export default {
<% } %>
}
},
<% } %>
// Login
async login ({ dispatch }, { fields, endpoint = '<%= options.login.endpoint %>' } = {}) {
@@ -146,7 +150,9 @@ export default {
<% } %>
// Fetch authenticated user
<% if (options.user.enabled) { %>
await dispatch('fetch')
<% } %>
},
// Logout

0 comments on commit 1b8856c

Please sign in to comment.