Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option fetchUser #27

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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