diff --git a/README.md b/README.md index d885b5562..82205344b 100644 --- a/README.md +++ b/README.md @@ -43,9 +43,11 @@ }, token: { enabled: true, + type: 'Bearer', + localStorage: true, name: 'token', - cookieName: 'token', - type: 'Bearer' + cookie: true, + cookieName: 'token' } } ``` @@ -68,10 +70,11 @@ Sets the global settings for store **logout** action. #### Token * **enabled** - Get and use tokens for authentication. -* **name** - Set the token name in the local storage. -* **cookieName** - Set the token name in Cookies. (Set to `null` to disable) * **type** - Sets the token type of the authorization header. -* **localStorage** - Decide whether to use or not the LocalStorage (default **true**). +* **localStorage** - Decide whether to use or not the LocalStorage. +* **name** - Set the token name in the local storage. +* **cookie** - Decide whether to use or not the Cookies. +* **cookieName** - Set the token name in Cookies. #### redirect * **notLoggedIn** - Sets the redirect URL default of the users not logged in. Only when `auth` middleware is added to a page. diff --git a/lib/module.js b/lib/module.js index 15ce7a351..b0f8c0f01 100644 --- a/lib/module.js +++ b/lib/module.js @@ -21,10 +21,11 @@ module.exports = function (moduleOptions) { }, token: { enabled: true, - name: 'token', - cookieName: 'token', type: 'Bearer', - localStorage: true + localStorage: true, + name: 'token', + cookie: true, + cookieName: 'token' } } diff --git a/lib/templates/auth.store.js b/lib/templates/auth.store.js index 8ed1cd6ee..11cda4b28 100644 --- a/lib/templates/auth.store.js +++ b/lib/templates/auth.store.js @@ -1,4 +1,4 @@ -<% if (options.token && options.token.enabled && options.token.cookieName) { %> +<% if (options.token.enabled && options.token.cookie) { %> import Cookie from 'cookie' import Cookies from 'js-cookie' <% } %> @@ -7,13 +7,13 @@ export default { namespaced: true, state: () => ({ - <% if (options.token && options.token.enabled) { %>token: null,<% } %> + <% if (options.token.enabled) { %>token: null,<% } %> user: null }), getters: { loggedIn (state) { - return Boolean(state.user<% if (options.token && options.token.enabled) { %> || state.token<% } %>) + return Boolean(state.user<% if (options.token.enabled) { %> || state.token<% } %>) } }, @@ -23,7 +23,7 @@ export default { state.user = user }, - <% if (options.token && options.token.enabled) { %> + <% if (options.token.enabled) { %> // SET_TOKEN SET_TOKEN (state, token) { state.token = token @@ -32,7 +32,7 @@ export default { }, actions: { - <% if (options.token && options.token.enabled) { %> + <% if (options.token.enabled) { %> // Update token updateToken ({ commit }, token) { // Update token in store's state @@ -52,7 +52,7 @@ export default { } <% } %> - <% if (options.token.cookieName) { %> + <% if (options.token.cookie) { %> // Update cookies if (process.browser) { // ...Browser @@ -78,19 +78,19 @@ export default { }, <% } %> - <% if (options.token && options.token.enabled) { %> + <% if (options.token.enabled) { %> // Fetch Token fetchToken ({ dispatch }) { let token - // Try to extract token from localStorage <% if (options.token.localStorage) { %> - if (process.browser && localStorage) { - token = localStorage.getItem('<%= options.token.name %>') - } + // Try to extract token from localStorage + if (process.browser && localStorage) { + token = localStorage.getItem('<%= options.token.name %>') + } <% } %> - <% if (options.token.cookieName) { %> + <% if (options.token.cookie) { %> // Try to extract token from cookies if (!token) { const cookieStr = process.browser ? document.cookie : this.$ctx.req.headers.cookie @@ -108,12 +108,12 @@ export default { // Reset reset ({ dispatch, commit }) { commit('SET_USER', null) - <% if (options.token && options.token.enabled) { %>dispatch('updateToken', null)<% } %> + <% if (options.token.enabled) { %>dispatch('updateToken', null)<% } %> }, // Fetch async fetch ({ getters, state, commit, dispatch }, { endpoint = '<%= options.user.endpoint %>' } = {}) { - <% if (options.token && options.token.enabled) { %> + <% if (options.token.enabled) { %> // Fetch and update latest token dispatch('fetchToken') <% } %> @@ -138,7 +138,7 @@ export default { // Send credentials to API let data = await this.$axios.$post(endpoint, fields) - <% if (options.token && options.token.enabled) { %> + <% if (options.token.enabled) { %> dispatch('updateToken', data['<%= options.token.name %>']) <% } %>