Skip to content

Commit

Permalink
feat: improve token options
Browse files Browse the repository at this point in the history
  • Loading branch information
Pooya Parsa committed Nov 15, 2017
1 parent b4086a0 commit 2a2c4c2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@
},
token: {
enabled: true,
type: 'Bearer',
localStorage: true,
name: 'token',
cookieName: 'token',
type: 'Bearer'
cookie: true,
cookieName: 'token'
}
}
```
Expand All @@ -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.
Expand Down
7 changes: 4 additions & 3 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}

Expand Down
30 changes: 15 additions & 15 deletions lib/templates/auth.store.js
Original file line number Diff line number Diff line change
@@ -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'
<% } %>
Expand All @@ -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<% } %>)
}
},

Expand All @@ -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
Expand All @@ -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
Expand All @@ -52,7 +52,7 @@ export default {
}
<% } %>

<% if (options.token.cookieName) { %>
<% if (options.token.cookie) { %>
// Update cookies
if (process.browser) {
// ...Browser
Expand All @@ -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
Expand All @@ -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')
<% } %>
Expand All @@ -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 %>'])
<% } %>

Expand Down

0 comments on commit 2a2c4c2

Please sign in to comment.