Permalink
Browse files

feat: improve token options

  • Loading branch information...
pi0 committed Nov 15, 2017
1 parent b4086a0 commit 2a2c4c279e382b444862bd9e54fb0888c80df8df
Showing with 27 additions and 23 deletions.
  1. +8 −5 README.md
  2. +4 −3 lib/module.js
  3. +15 −15 lib/templates/auth.store.js
@@ -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.
@@ -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'
}
}
@@ -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 %>'])
<% } %>

0 comments on commit 2a2c4c2

Please sign in to comment.