From c770281fe94cb8335e4964462b4046c4edb8f1b9 Mon Sep 17 00:00:00 2001 From: Herberts Cruz Date: Tue, 17 Oct 2017 17:12:42 -0200 Subject: [PATCH 01/13] Implementation of the auth property settings in nuxt.config.js with default settings. --- .gitignore | 3 ++- src/index.js | 26 ++++++++++++++++++++++++-- templates/auth.store.js | 39 +++++++++++++++++++++++++++++++-------- 3 files changed, 57 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index de749b982..7e52f7db5 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,5 @@ node_modules .vscode .DS_STORE coverage -dist \ No newline at end of file +dist +package-lock.json diff --git a/src/index.js b/src/index.js index ad43d1fc4..3ebeb9575 100644 --- a/src/index.js +++ b/src/index.js @@ -1,7 +1,29 @@ const { resolve } = require('path') export default async function module (moduleOptions) { - // const options = Object.assign({}, this.options.auth, moduleOptions) + // Apply defaults + const defaults = { + login: { + endpoint: 'auth/login', + propertyName: 'token', + session: false + }, + logout: { + endpoint: 'auth/logout', + method: 'GET', + paramTokenName: '', + appendToken: false + }, + user: { + endpoint: 'auth/user', + propertyName: 'user', + paramTokenName: '', + appendToken: false + }, + tokenType: 'Bearer' + } + + const options = Object.assign({}, this.options.auth, moduleOptions, defaults) // Plugin this.addPlugin({ src: resolve(__dirname, '../templates/auth.plugin.js'), fileName: 'auth.plugin.js' }) @@ -10,5 +32,5 @@ export default async function module (moduleOptions) { this.addTemplate({ src: resolve(__dirname, '../templates/auth.middleware.js'), fileName: 'auth.middleware.js' }) // Store - this.addTemplate({ src: resolve(__dirname, '../templates/auth.store.js'), fileName: 'auth.store.js' }) + this.addTemplate({ src: resolve(__dirname, '../templates/auth.store.js'), fileName: 'auth.store.js', options }) } diff --git a/templates/auth.store.js b/templates/auth.store.js index 1d0dac803..a35509cc4 100644 --- a/templates/auth.store.js +++ b/templates/auth.store.js @@ -1,6 +1,8 @@ import Cookie from 'cookie' import Cookies from 'js-cookie' +const options = <%= serialize(options) %> + export default { namespaced: true, @@ -80,7 +82,13 @@ export default { await dispatch('updateToken', null) }, - async fetch ({ state, commit, dispatch }, { endpoint = 'auth/user' } = {}) { + async fetch ({ state, commit, dispatch }) { + let {endpoint, propertyName, paramTokenName, appendToken} = options.user + // Append token + if (appendToken) { + paramTokenName = (paramTokenName) ? ('?' + paramTokenName + '=') : '/'; + endpoint = endpoint + paramTokenName + state.token + } // Fetch and update latest token await dispatch('fetchToken') @@ -91,18 +99,25 @@ export default { // Try to get user profile try { - const userData = await this.$axios.$get(endpoint) - commit('SET_USER', userData.user) + const headers = {'Authorization': options.tokenType + ' ' + state.token} + const userData = await this.$axios.$get(endpoint, {headers}) + + if (propertyName) { + commit('SET_USER', userData[propertyName]) + } else { + commit('SET_USER', userData) + } } catch (e) { return dispatch('invalidate') } }, // Login - async login ({ commit, dispatch }, { fields, endpoint = 'auth/login', session = false } = {}) { + async login ({ commit, dispatch }, { fields } = {}) { + let {endpoint, propertyName} = options.login // Send credentials to API let tokenData = await this.$axios.$post(endpoint, fields) - let token = tokenData.token || tokenData.id_token + let token = tokenData[propertyName] // Update new token await dispatch('updateToken', token) @@ -112,15 +127,23 @@ export default { }, // Logout - async logout ({ commit, dispatch, state }, { endpoint = 'auth/logout', appendToken = false } = {}) { + async logout ({ commit, dispatch, state }) { + let {endpoint, method, paramTokenName, appendToken} = options.logout // Append token if (appendToken) { - endpoint = endpoint + '/' + state.token + paramTokenName = (paramTokenName) ? ('?' + paramTokenName + '=') : '/'; + endpoint = endpoint + paramTokenName + state.token } // Server side logout try { - await this.$axios.$get(endpoint) + const headers = {'Authorization': options.tokenType + ' ' + state.token} + + if (method.toUpperCase() === 'POST') { + await this.$axios.$post(endpoint, {}, {headers}) + } else { + await this.$axios.$get(endpoint, {headers}) + } } catch (e) { // eslint-disable-next-line no-console console.error('Error while logging out', e) From 2dbb3a5bf56fa3e6624d459bb7e8409c299c986d Mon Sep 17 00:00:00 2001 From: Herberts Cruz Date: Wed, 18 Oct 2017 11:20:05 -0200 Subject: [PATCH 02/13] Solved (TODO Use set-cookie header for this..res - auth.store.js) --- templates/auth.store.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/templates/auth.store.js b/templates/auth.store.js index a35509cc4..95b330374 100644 --- a/templates/auth.store.js +++ b/templates/auth.store.js @@ -53,7 +53,16 @@ export default { } } else { // ...Server - // TODO: Use set-cookie header for this.$ctx.res + if (token) { + this.$ctx.res.setHeader('Set-Cookie', Cookie.serialize('token', token, {})) + } else { + let expires + let date = new Date() + expires = date.setDate(date.getDate() - 1) + this.$ctx.res.setHeader('Set-Cookie', Cookie.serialize('token', '', { + expires: new Date(expires) + })) + } } }, From 465ace4f9c35844ad5d7c1e02e30bbbea9b08be7 Mon Sep 17 00:00:00 2001 From: Herberts Cruz Date: Wed, 18 Oct 2017 16:56:32 -0200 Subject: [PATCH 03/13] Bug fix resolution. --- templates/auth.store.js | 42 ++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/templates/auth.store.js b/templates/auth.store.js index 95b330374..3080ced5f 100644 --- a/templates/auth.store.js +++ b/templates/auth.store.js @@ -53,16 +53,16 @@ export default { } } else { // ...Server - if (token) { - this.$ctx.res.setHeader('Set-Cookie', Cookie.serialize('token', token, {})) - } else { + let params = { + domain: '/' + } + if (!token) { let expires let date = new Date() expires = date.setDate(date.getDate() - 1) - this.$ctx.res.setHeader('Set-Cookie', Cookie.serialize('token', '', { - expires: new Date(expires) - })) + params.expires = new Date(expires) } + this.$ctx.res.setHeader('Set-Cookie', Cookie.serialize(tokenNameCookie, token, params)) } }, @@ -86,26 +86,28 @@ export default { } }, - async invalidate ({ dispatch, commit }) { + invalidate ({ dispatch, commit }) { commit('SET_USER', null) - await dispatch('updateToken', null) + dispatch('updateToken', null) }, async fetch ({ state, commit, dispatch }) { let {endpoint, propertyName, paramTokenName, appendToken} = options.user - // Append token - if (appendToken) { - paramTokenName = (paramTokenName) ? ('?' + paramTokenName + '=') : '/'; - endpoint = endpoint + paramTokenName + state.token - } + // Fetch and update latest token - await dispatch('fetchToken') + dispatch('fetchToken') // Not loggedIn if (!state.token) { return } + // Append token + if (appendToken) { + paramTokenName = (paramTokenName) ? ('?' + paramTokenName + '=') : '/'; + endpoint = endpoint + paramTokenName + state.token + } + // Try to get user profile try { const headers = {'Authorization': options.tokenType + ' ' + state.token} @@ -122,22 +124,24 @@ export default { }, // Login - async login ({ commit, dispatch }, { fields } = {}) { + async login ({ dispatch }, { fields } = {}) { let {endpoint, propertyName} = options.login + // Send credentials to API let tokenData = await this.$axios.$post(endpoint, fields) let token = tokenData[propertyName] // Update new token - await dispatch('updateToken', token) + dispatch('updateToken', token) // Fetch authenticated user - await dispatch('fetch') + dispatch('fetch') }, // Logout - async logout ({ commit, dispatch, state }) { + async logout ({ dispatch, state }) { let {endpoint, method, paramTokenName, appendToken} = options.logout + // Append token if (appendToken) { paramTokenName = (paramTokenName) ? ('?' + paramTokenName + '=') : '/'; @@ -159,7 +163,7 @@ export default { } // Unload user profile & token - await dispatch('invalidate') + dispatch('invalidate') } } } From 302949c89e913eb385660613cc0285570d44a92f Mon Sep 17 00:00:00 2001 From: Herberts Cruz Date: Wed, 18 Oct 2017 22:20:44 -0200 Subject: [PATCH 04/13] Creation of the local storage and cookie token name configuration parameter. --- package.json | 3 ++- src/index.js | 4 +++- templates/auth.store.js | 14 ++++++++------ 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 4f2c07ad3..b2911c18c 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,8 @@ "dependencies": { "@nuxtjs/axios": "^4.0.1", "cookie": "^0.3.1", - "js-cookie": "^2.1.4" + "js-cookie": "^2.1.4", + "lodash": "^4.17.4" }, "devDependencies": { "nuxt-module-builder": "latest" diff --git a/src/index.js b/src/index.js index 3ebeb9575..d8fdf8eeb 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,5 @@ const { resolve } = require('path') +const { merge } = require('lodash') export default async function module (moduleOptions) { // Apply defaults @@ -20,10 +21,11 @@ export default async function module (moduleOptions) { paramTokenName: '', appendToken: false }, + storageTokenName: 'nuxt-auth-token', tokenType: 'Bearer' } - const options = Object.assign({}, this.options.auth, moduleOptions, defaults) + const options = merge(defaults, moduleOptions, this.options.auth) // Plugin this.addPlugin({ src: resolve(__dirname, '../templates/auth.plugin.js'), fileName: 'auth.plugin.js' }) diff --git a/templates/auth.store.js b/templates/auth.store.js index 3080ced5f..7bf2f18d5 100644 --- a/templates/auth.store.js +++ b/templates/auth.store.js @@ -1,7 +1,9 @@ import Cookie from 'cookie' import Cookies from 'js-cookie' +const { kebabCase } = require('lodash') const options = <%= serialize(options) %> +const storageTokenName = kebabCase(options.storageTokenName) export default { namespaced: true, @@ -37,9 +39,9 @@ export default { // Update localStorage if (process.browser && localStorage) { if (token) { - localStorage.setItem('nuxt::auth::token', token) + localStorage.setItem(storageTokenName, token) } else { - localStorage.removeItem('nuxt::auth::token') + localStorage.removeItem(storageTokenName) } } @@ -47,9 +49,9 @@ export default { if (process.browser) { // ...Browser if (token) { - Cookies.set('token', token) + Cookies.set(storageTokenName, token) } else { - Cookies.remove('token') + Cookies.remove(storageTokenName) } } else { // ...Server @@ -71,14 +73,14 @@ export default { // First try localStorage if (process.browser && localStorage) { - token = localStorage.getItem('nuxt::auth::token') + token = localStorage.getItem(storageTokenName) } // Then try to extract token from cookies if (!token) { const cookieStr = process.browser ? document.cookie : this.$ctx.req.headers.cookie const cookies = Cookie.parse(cookieStr || '') || {} - token = cookies.token + token = cookies[storageTokenName] } if (token) { From 679b6641950c47d4c71bee88eb99b2482c6a2d5f Mon Sep 17 00:00:00 2001 From: Herberts Cruz Date: Wed, 18 Oct 2017 23:35:38 -0200 Subject: [PATCH 05/13] Documentation update. --- README.md | 47 ++++++++++++++++++++++++++++++++++++++++++++--- package.json | 2 +- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index a3a057907..21bbe4ded 100644 --- a/README.md +++ b/README.md @@ -22,20 +22,61 @@ '@nuxtjs/auth', // ...Axios module should be included AFTER @nuxtjs/auth - '@nuxtjs/axios', + '@nuxtjs/axios' ], + // Default Values auth: { - /* auth options */ + login: { + endpoint: 'auth/login', + propertyName: 'token' + }, + logout: { + endpoint: 'auth/logout', + method: 'GET', + paramTokenName: '', + appendToken: false + }, + user: { + endpoint: 'auth/user', + propertyName: 'user', + paramTokenName: '', + appendToken: false + }, + storageTokenName: 'nuxt-auth-token', + tokenType: 'Bearer' } } ``` ## Options +####login +Set the global settings for the login action. +* **endpoint** - Set the URL of the login endpoint. It can be a relative or absolute path. +* **propertyName** - Set the name of the return object property that contains the access token. +####logout +Sets the global settings for the logout action. +* **endpoint** - Set the URL of the logout endpoint. It can be a relative or absolute path. +* **method** - Set the request to POST or GET. +* **paramTokenName** - Set the access token query string parameter name. +* **appendToken** - Set true if you want the access token to be inserted in the URL. + +####user +Sets the global settings for the fetch action. +* **endpoint** - Set the URL of the user data endpoint. It can be a relative or absolute path. +* **propertyName** - Set the name of the return object property that contains the user data. If you want the entire object returned, set an empty string. +* **paramTokenName** - Set the access token query string parameter name. +* **appendToken** - Set true if you want the access token to be inserted in the URL. + +####storageTokenName +Set the token name in the local storage and in the cookie. + +####tokenType +Sets the token type of the authorization header. ## License [MIT License](./LICENSE) -Copyright (c) Nuxt Community \ No newline at end of file +Copyright (c) Nuxt Community diff --git a/package.json b/package.json index b2911c18c..5e3910507 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "license": "MIT", "contributors": [ { - "name": "Pooya Parsa " + "name": "Pooya Parsa , Herberts Cruz " } ], "main": "dist/index.js", From 7b1817c7ea7a803235bf8aa28afac345513d9c22 Mon Sep 17 00:00:00 2001 From: Herberts Cruz Date: Wed, 18 Oct 2017 23:37:36 -0200 Subject: [PATCH 06/13] Documentation update. --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 21bbe4ded..6d457f88a 100644 --- a/README.md +++ b/README.md @@ -50,29 +50,29 @@ ## Options -####login +#### login Set the global settings for the login action. * **endpoint** - Set the URL of the login endpoint. It can be a relative or absolute path. * **propertyName** - Set the name of the return object property that contains the access token. -####logout +#### logout Sets the global settings for the logout action. * **endpoint** - Set the URL of the logout endpoint. It can be a relative or absolute path. * **method** - Set the request to POST or GET. * **paramTokenName** - Set the access token query string parameter name. * **appendToken** - Set true if you want the access token to be inserted in the URL. -####user +#### user Sets the global settings for the fetch action. * **endpoint** - Set the URL of the user data endpoint. It can be a relative or absolute path. * **propertyName** - Set the name of the return object property that contains the user data. If you want the entire object returned, set an empty string. * **paramTokenName** - Set the access token query string parameter name. * **appendToken** - Set true if you want the access token to be inserted in the URL. -####storageTokenName +#### storageTokenName Set the token name in the local storage and in the cookie. -####tokenType +#### tokenType Sets the token type of the authorization header. ## License From ada88fb4b0d3a7216aedd20998ea41438976967a Mon Sep 17 00:00:00 2001 From: Herberts Cruz Date: Thu, 19 Oct 2017 00:36:12 -0200 Subject: [PATCH 07/13] Documentation update and bug fix. --- README.md | 21 +++++++++++++++++++++ templates/auth.store.js | 2 +- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6d457f88a..0a87ce37d 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,27 @@ Set the token name in the local storage and in the cookie. #### tokenType Sets the token type of the authorization header. +## Example usage + +```js +// ... login code ... + store.dispatch('auth/login', { + fields: { + username: 'your_username', + password: 'your_password' + } + }) + +// ... logout code ... + store.dispatch('auth/logout') + +// ... code - get access token ... + store.state['auth']['token'] + +// ... code - get user data ... + store.state['auth']['user'] +``` + ## License [MIT License](./LICENSE) diff --git a/templates/auth.store.js b/templates/auth.store.js index 7bf2f18d5..889b23142 100644 --- a/templates/auth.store.js +++ b/templates/auth.store.js @@ -64,7 +64,7 @@ export default { expires = date.setDate(date.getDate() - 1) params.expires = new Date(expires) } - this.$ctx.res.setHeader('Set-Cookie', Cookie.serialize(tokenNameCookie, token, params)) + this.$ctx.res.setHeader('Set-Cookie', Cookie.serialize(storageTokenName, token, params)) } }, From 3ee523d4ad0451c0895f1c40349bf962eae3d31c Mon Sep 17 00:00:00 2001 From: Herberts Cruz Date: Thu, 19 Oct 2017 00:46:00 -0200 Subject: [PATCH 08/13] Documentation update. --- README.md | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 0a87ce37d..0345d8235 100644 --- a/README.md +++ b/README.md @@ -78,22 +78,25 @@ Sets the token type of the authorization header. ## Example usage ```js -// ... login code ... - store.dispatch('auth/login', { - fields: { - username: 'your_username', - password: 'your_password' - } - }) +// ... code ... +store.dispatch('auth/login', { + fields: { + username: 'your_username', + password: 'your_password' + } +}) // run login -// ... logout code ... - store.dispatch('auth/logout') +// ... code ... +store.dispatch('auth/logout') // run logout -// ... code - get access token ... - store.state['auth']['token'] +// ... code ... +store.state['auth']['token'] // get access token -// ... code - get user data ... - store.state['auth']['user'] +// ... code ... +store.state['auth']['user'] // get user data + +// ... code ... +store.getters['auth/loggedIn'] // get login status (true or false) ``` ## License From 3a2a36f237ad1de66f0870ba6612b425d4f418c0 Mon Sep 17 00:00:00 2001 From: Herberts Cruz Date: Thu, 19 Oct 2017 10:36:09 -0200 Subject: [PATCH 09/13] Small improvements. --- package.json | 5 ++++- templates/auth.store.js | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 5e3910507..ebf253585 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,10 @@ "license": "MIT", "contributors": [ { - "name": "Pooya Parsa , Herberts Cruz " + "name": "Pooya Parsa " + }, + { + "name": "Herberts Cruz " } ], "main": "dist/index.js", diff --git a/templates/auth.store.js b/templates/auth.store.js index 889b23142..80f61d8b6 100644 --- a/templates/auth.store.js +++ b/templates/auth.store.js @@ -1,6 +1,6 @@ import Cookie from 'cookie' import Cookies from 'js-cookie' -const { kebabCase } = require('lodash') +import { kebabCase } from 'lodash' const options = <%= serialize(options) %> const storageTokenName = kebabCase(options.storageTokenName) From 780ac1281cdd25695c8e181196d6bb16838ae85d Mon Sep 17 00:00:00 2001 From: Herberts Cruz Date: Fri, 20 Oct 2017 22:24:03 -0200 Subject: [PATCH 10/13] Ajustments. --- templates/auth.store.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/templates/auth.store.js b/templates/auth.store.js index 80f61d8b6..91c4466fe 100644 --- a/templates/auth.store.js +++ b/templates/auth.store.js @@ -106,7 +106,7 @@ export default { // Append token if (appendToken) { - paramTokenName = (paramTokenName) ? ('?' + paramTokenName + '=') : '/'; + paramTokenName = (paramTokenName) ? ('?' + paramTokenName + '=') : '/' endpoint = endpoint + paramTokenName + state.token } @@ -121,7 +121,7 @@ export default { commit('SET_USER', userData) } } catch (e) { - return dispatch('invalidate') + dispatch('invalidate') } }, @@ -146,7 +146,7 @@ export default { // Append token if (appendToken) { - paramTokenName = (paramTokenName) ? ('?' + paramTokenName + '=') : '/'; + paramTokenName = (paramTokenName) ? ('?' + paramTokenName + '=') : '/' endpoint = endpoint + paramTokenName + state.token } From 3173c462817c381965ac2ceeb9cddd0f6a5af1f1 Mon Sep 17 00:00:00 2001 From: Herberts Cruz Date: Sun, 22 Oct 2017 03:01:25 -0200 Subject: [PATCH 11/13] Added middlewave redirect URL settings. --- README.md | 22 +++++++++++++++++++++- src/index.js | 8 +++++--- templates/auth.middleware.js | 7 +++++-- templates/auth.store.js | 13 ++++++++----- 4 files changed, 39 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 0345d8235..b53e014ef 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,9 @@ appendToken: false }, storageTokenName: 'nuxt-auth-token', - tokenType: 'Bearer' + tokenType: 'Bearer', + notLoggedInRedirectTo: '/login', + loggedInRedirectTo: '/', } } ``` @@ -75,6 +77,12 @@ Set the token name in the local storage and in the cookie. #### tokenType Sets the token type of the authorization header. +#### notLoggedInRedirectTo +Sets the redirect URL default of the users not logged in. This is actived when 'auth' middeware is register. + +#### loggedInRedirectTo +Sets the redirect URL default of the users logged in. This is actived when 'no-auth' middeware is register. + ## Example usage ```js @@ -99,6 +107,18 @@ store.state['auth']['user'] // get user data store.getters['auth/loggedIn'] // get login status (true or false) ``` +## Middleware + +```js +// ... in nuxt.config.js ... +router: { + middleware: [ + 'auth', // If user not logged in, redirect to '/login' or to URL defined in notLoggedInRedirectTo property + 'no-auth' // If user is already logged in, redirect to '/' or to URL defined in loggedInRedirectTo property + ] +} +``` + ## License [MIT License](./LICENSE) diff --git a/src/index.js b/src/index.js index d8fdf8eeb..069cd9520 100644 --- a/src/index.js +++ b/src/index.js @@ -1,7 +1,7 @@ const { resolve } = require('path') const { merge } = require('lodash') -export default async function module (moduleOptions) { +export default function module (moduleOptions) { // Apply defaults const defaults = { login: { @@ -22,7 +22,9 @@ export default async function module (moduleOptions) { appendToken: false }, storageTokenName: 'nuxt-auth-token', - tokenType: 'Bearer' + tokenType: 'Bearer', + notLoggedInRedirectTo: '/login', + loggedInRedirectTo: '/', } const options = merge(defaults, moduleOptions, this.options.auth) @@ -31,7 +33,7 @@ export default async function module (moduleOptions) { this.addPlugin({ src: resolve(__dirname, '../templates/auth.plugin.js'), fileName: 'auth.plugin.js' }) // Middleware - this.addTemplate({ src: resolve(__dirname, '../templates/auth.middleware.js'), fileName: 'auth.middleware.js' }) + this.addTemplate({ src: resolve(__dirname, '../templates/auth.middleware.js'), fileName: 'auth.middleware.js', options }) // Store this.addTemplate({ src: resolve(__dirname, '../templates/auth.store.js'), fileName: 'auth.store.js', options }) diff --git a/templates/auth.middleware.js b/templates/auth.middleware.js index 27c60ed57..2ab05c7a0 100644 --- a/templates/auth.middleware.js +++ b/templates/auth.middleware.js @@ -1,15 +1,18 @@ import middleware from './middleware' +const options = <%= serialize(options) %> + middleware.auth = function authMiddleware ({ store, redirect }) { // If user not logged in, redirect to /login if (!store.getters['auth/loggedIn']) { - return redirect('/login') + console.log(options) + return redirect(options.notLoggedInRedirectTo) } } middleware['no-auth'] = function noAuthMiddleware ({ store, redirect }) { // If user is already logged in, redirect to / if (store.getters['auth/loggedIn']) { - return redirect('/') + return redirect(options.loggedInRedirectTo) } } diff --git a/templates/auth.store.js b/templates/auth.store.js index 91c4466fe..d98041f84 100644 --- a/templates/auth.store.js +++ b/templates/auth.store.js @@ -112,8 +112,10 @@ export default { // Try to get user profile try { - const headers = {'Authorization': options.tokenType + ' ' + state.token} - const userData = await this.$axios.$get(endpoint, {headers}) + // Set Authorization Token in request + this.$axios.setToken(state.token, options.tokenType) + + const userData = await this.$axios.$get(endpoint) if (propertyName) { commit('SET_USER', userData[propertyName]) @@ -152,12 +154,13 @@ export default { // Server side logout try { - const headers = {'Authorization': options.tokenType + ' ' + state.token} + // Set Authorization Token in request + this.$axios.setToken(state.token, options.tokenType); if (method.toUpperCase() === 'POST') { - await this.$axios.$post(endpoint, {}, {headers}) + await this.$axios.$post(endpoint) } else { - await this.$axios.$get(endpoint, {headers}) + await this.$axios.$get(endpoint) } } catch (e) { // eslint-disable-next-line no-console From 85352f72cfd860552e6a22f3e44f8b883175ead8 Mon Sep 17 00:00:00 2001 From: Herberts Cruz Date: Tue, 24 Oct 2017 23:40:56 -0200 Subject: [PATCH 12/13] Bug fixes. --- templates/auth.store.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/auth.store.js b/templates/auth.store.js index d98041f84..2090b607b 100644 --- a/templates/auth.store.js +++ b/templates/auth.store.js @@ -139,7 +139,7 @@ export default { dispatch('updateToken', token) // Fetch authenticated user - dispatch('fetch') + await dispatch('fetch') }, // Logout From 5533457e0843e61faeb09b4e1df12139829f4801 Mon Sep 17 00:00:00 2001 From: Herberts Cruz Date: Tue, 24 Oct 2017 23:47:43 -0200 Subject: [PATCH 13/13] Bug fixes. --- templates/auth.middleware.js | 1 - 1 file changed, 1 deletion(-) diff --git a/templates/auth.middleware.js b/templates/auth.middleware.js index 2ab05c7a0..6400277fc 100644 --- a/templates/auth.middleware.js +++ b/templates/auth.middleware.js @@ -5,7 +5,6 @@ const options = <%= serialize(options) %> middleware.auth = function authMiddleware ({ store, redirect }) { // If user not logged in, redirect to /login if (!store.getters['auth/loggedIn']) { - console.log(options) return redirect(options.notLoggedInRedirectTo) } }