From b4086a07fe81c845acc28ff657e2f188c212e833 Mon Sep 17 00:00:00 2001 From: Eduardo Partipilo Date: Wed, 15 Nov 2017 17:43:51 +0100 Subject: [PATCH] feat(store): let LocalStorage to be optional (#18, @epartipilo) * Added the attribute to choose if we want to use localStorage or not * Update README file --- README.md | 1 + lib/module.js | 3 ++- lib/templates/auth.store.js | 22 +++++++++++++--------- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 8bb98e6dc..d885b5562 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,7 @@ Sets the global settings for store **logout** action. * **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**). #### 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 9d51266d8..15ce7a351 100644 --- a/lib/module.js +++ b/lib/module.js @@ -23,7 +23,8 @@ module.exports = function (moduleOptions) { enabled: true, name: 'token', cookieName: 'token', - type: 'Bearer' + type: 'Bearer', + localStorage: true } } diff --git a/lib/templates/auth.store.js b/lib/templates/auth.store.js index c8645cc09..8ed1cd6ee 100644 --- a/lib/templates/auth.store.js +++ b/lib/templates/auth.store.js @@ -42,13 +42,15 @@ export default { this.$axios.setToken(token, '<%= options.token.type %>'); // Update localStorage - if (process.browser && localStorage) { - if (token) { - localStorage.setItem('<%= options.token.name %>', token) - } else { - localStorage.removeItem('<%= options.token.name %>') + <% if (options.token.localStorage) { %> + if (process.browser && localStorage) { + if (token) { + localStorage.setItem('<%= options.token.name %>', token) + } else { + localStorage.removeItem('<%= options.token.name %>') + } } - } + <% } %> <% if (options.token.cookieName) { %> // Update cookies @@ -82,9 +84,11 @@ export default { let token // Try to extract token from localStorage - if (process.browser && localStorage) { - token = localStorage.getItem('<%= options.token.name %>') - } + <% if (options.token.localStorage) { %> + if (process.browser && localStorage) { + token = localStorage.getItem('<%= options.token.name %>') + } + <% } %> <% if (options.token.cookieName) { %> // Try to extract token from cookies