From 1b8856c3e74f6c1b8fe575c960aa10b46b686eb6 Mon Sep 17 00:00:00 2001 From: Ernestas Poskus Date: Sun, 24 Dec 2017 15:25:41 +0200 Subject: [PATCH] feat: add fetchUser option (#27) --- README.md | 11 ++++++----- lib/module.js | 3 ++- lib/templates/auth.store.js | 10 ++++++++-- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 0d0a1cf21..34dae0eda 100644 --- a/README.md +++ b/README.md @@ -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) ``` diff --git a/lib/module.js b/lib/module.js index b824667be..8d03e5782 100644 --- a/lib/module.js +++ b/lib/module.js @@ -7,7 +7,8 @@ module.exports = function (moduleOptions) { user: { endpoint: 'auth/user', propertyName: 'user', - resetOnFail: true + resetOnFail: true, + enabled: true }, login: { endpoint: 'auth/login', diff --git a/lib/templates/auth.store.js b/lib/templates/auth.store.js index f8b7c8b50..e44f17e93 100644 --- a/lib/templates/auth.store.js +++ b/lib/templates/auth.store.js @@ -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