Skip to content

Commit

Permalink
feat: add fetchUser option (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
ernestas-poskus authored and pi0 committed Dec 24, 2017
1 parent 9d1f050 commit 1b8856c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
user: {
endpoint: 'auth/user',
propertyName: 'user',
resetOnFail: true
resetOnFail: true,
enabled: true
},
login: {
endpoint: 'auth/login',
Expand Down Expand Up @@ -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)
```
Expand Down
3 changes: 2 additions & 1 deletion lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ module.exports = function (moduleOptions) {
user: {
endpoint: 'auth/user',
propertyName: 'user',
resetOnFail: true
resetOnFail: true,
enabled: true
},
login: {
endpoint: 'auth/login',
Expand Down
10 changes: 8 additions & 2 deletions lib/templates/auth.store.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default {

state: () => ({
<% if (options.token.enabled) { %>token: null,<% } %>
user: null
<% if (options.user.enabled) { %>user: null<% } %>
}),

getters: {
Expand All @@ -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
Expand Down Expand Up @@ -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) { %>
Expand All @@ -135,6 +138,7 @@ export default {
<% } %>
}
},
<% } %>

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

// Fetch authenticated user
<% if (options.user.enabled) { %>
await dispatch('fetch')
<% } %>
},

// Logout
Expand Down

0 comments on commit 1b8856c

Please sign in to comment.