Skip to content

Commit

Permalink
feat(store): let LocalStorage to be optional (#18, @epartipilo)
Browse files Browse the repository at this point in the history
* Added the attribute to choose if we want to use localStorage or not

* Update README file
  • Loading branch information
epartipilo authored and pi0 committed Nov 15, 2017
1 parent 48a0f56 commit b4086a0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ module.exports = function (moduleOptions) {
enabled: true,
name: 'token',
cookieName: 'token',
type: 'Bearer'
type: 'Bearer',
localStorage: true
}
}

Expand Down
22 changes: 13 additions & 9 deletions lib/templates/auth.store.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b4086a0

Please sign in to comment.