Skip to content

Commit b4086a0

Browse files
epartipilopi0
authored andcommitted
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
1 parent 48a0f56 commit b4086a0

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ Sets the global settings for store **logout** action.
7171
* **name** - Set the token name in the local storage.
7272
* **cookieName** - Set the token name in Cookies. (Set to `null` to disable)
7373
* **type** - Sets the token type of the authorization header.
74+
* **localStorage** - Decide whether to use or not the LocalStorage (default **true**).
7475
7576
#### redirect
7677
* **notLoggedIn** - Sets the redirect URL default of the users not logged in. Only when `auth` middleware is added to a page.

lib/module.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ module.exports = function (moduleOptions) {
2323
enabled: true,
2424
name: 'token',
2525
cookieName: 'token',
26-
type: 'Bearer'
26+
type: 'Bearer',
27+
localStorage: true
2728
}
2829
}
2930

lib/templates/auth.store.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,15 @@ export default {
4242
this.$axios.setToken(token, '<%= options.token.type %>');
4343

4444
// Update localStorage
45-
if (process.browser && localStorage) {
46-
if (token) {
47-
localStorage.setItem('<%= options.token.name %>', token)
48-
} else {
49-
localStorage.removeItem('<%= options.token.name %>')
45+
<% if (options.token.localStorage) { %>
46+
if (process.browser && localStorage) {
47+
if (token) {
48+
localStorage.setItem('<%= options.token.name %>', token)
49+
} else {
50+
localStorage.removeItem('<%= options.token.name %>')
51+
}
5052
}
51-
}
53+
<% } %>
5254

5355
<% if (options.token.cookieName) { %>
5456
// Update cookies
@@ -82,9 +84,11 @@ export default {
8284
let token
8385

8486
// Try to extract token from localStorage
85-
if (process.browser && localStorage) {
86-
token = localStorage.getItem('<%= options.token.name %>')
87-
}
87+
<% if (options.token.localStorage) { %>
88+
if (process.browser && localStorage) {
89+
token = localStorage.getItem('<%= options.token.name %>')
90+
}
91+
<% } %>
8892

8993
<% if (options.token.cookieName) { %>
9094
// Try to extract token from cookies

0 commit comments

Comments
 (0)