diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..f1cc3ad --- /dev/null +++ b/.editorconfig @@ -0,0 +1,15 @@ +# http://editorconfig.org + +root = true + +[*] +charset = utf-8 +indent_style = space +indent_size = 2 +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true + +[*.md] +insert_final_newline = false +trim_trailing_whitespace = false diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..7bf90e2 --- /dev/null +++ b/.eslintignore @@ -0,0 +1,3 @@ +flow +dist +packages diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..c425769 --- /dev/null +++ b/.eslintrc @@ -0,0 +1,19 @@ +{ + "root": true, + "parser": "babel-eslint", + "parserOptions": { + "sourceType": "module" + }, + "env": { + "browser": true, + }, + "extends": "standard", + "plugins": [ + "html" + ], + "rules": { + "arrow-parens": 0, + "generator-star-spacing': 0 + } +} + diff --git a/.gitignore b/.gitignore index 57052d1..db4123c 100644 --- a/.gitignore +++ b/.gitignore @@ -3,10 +3,21 @@ node_modules bower_components -# Editors +Thumbs.db +.DS_Store + +dist/ +npm-debug.log* +yarn-debug.log* +yarn-error.log* + +# Editor directories and files .idea +*.suo +*.ntvs* +*.njsproj +*.sln *.iml - -# OS metadata -.DS_Store -Thumbs.db \ No newline at end of file +.vscode +settings.json +jsconfig.json diff --git a/.tern-project b/.tern-project new file mode 100644 index 0000000..607c1fc --- /dev/null +++ b/.tern-project @@ -0,0 +1,13 @@ +{ + "plugins": { + "es_modules": {}, + "node": {} + }, + "libs": [ + "ecma5", + "ecma6", + "react", + "browser" + ], + "ecmaVersion": 6 +} diff --git a/README.md b/README.md index b61f4c5..dac8af5 100644 --- a/README.md +++ b/README.md @@ -2,21 +2,28 @@ [![Join the chat at https://gitter.im/vuejs-auth/vue-authenticate](https://badges.gitter.im/vue-authenticate/Lobby.svg)](https://gitter.im/vuejs-auth/vue-authenticate?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) -**vue-authenticate** is easily configurable solution for [Vue.js](https://vuejs.org/) that provides local login/registration as well as Social login using Github, Facebook, Google and Twitter OAuth providers (only those 4 tested for now). +**vue-authenticate** is easily configurable solution for [Vue.js](https://vuejs.org/) that provides local login/registration as well as Social login using Github, Facebook, Google and other OAuth providers. -The best part about this library is that it is not strictly coupled to one request handling library like [vue-resource](https://github.com/pagekit/vue-resource). You will be able to use it with different libraries. -For now it is tested to work with [vue-resource](https://github.com/pagekit/vue-resource) (default library) and [axios](https://github.com/mzabriskie/axios) (using [vue-axios](https://github.com/imcvampire/vue-axios) wrapper). -This library was inspired by well known authentication library for Angular called [Satellizer](https://github.com/sahat/satellizer) developed by [Sahat Yalkabov](http://sahatyalkabov.com). They share almost identical configuration and API so you can easily switch from Angular to Vue.js project. +The best part about this library is that it is not strictly coupled to one request handling library like [vue-axios](https://github.com/imcvampire/vue-axios). You will be able to use it with different libraries. -**DISCLAIMER** +For now it is tested to work with [vue-resource](https://github.com/pagekit/vue-resource) and [axios](https://github.com/mzabriskie/axios) (using [vue-axios](https://github.com/imcvampire/vue-axios) wrapper). -For now, this package only supports ES6 import usage, but soon will have standalone ES5 build. +**WARNING** From version 1.3.0 default request library is `axios` using `vue-axios` wrapper plugin. +This library was inspired by well known authentication library for Angular called [Satellizer](https://github.com/sahat/satellizer) developed by [Sahat Yalkabov](http://sahatyalkabov.com). They share almost identical configuration and API so you can easily switch from Angular to Vue.js project. +## Supported OAuth providers and configurations -*DEMO app comming soon...* +1. Facebook (https://github.com/dgrubelic/vue-authenticate/blob/master/src/options.js#L21) +2. Google (https://github.com/dgrubelic/vue-authenticate/blob/master/src/options.js#L34) +3. Github (https://github.com/dgrubelic/vue-authenticate/blob/master/src/options.js#L49) +4. Instagram (https://github.com/dgrubelic/vue-authenticate/blob/master/src/options.js#L61) +5. Twitter (https://github.com/dgrubelic/vue-authenticate/blob/master/src/options.js#L72) +6. Bitbucket (https://github.com/dgrubelic/vue-authenticate/blob/master/src/options.js#L81) +7. LinkedIn (https://github.com/dgrubelic/vue-authenticate/blob/master/src/options.js#L93) +8. Microsoft Live (https://github.com/dgrubelic/vue-authenticate/blob/master/src/options.js#L106) ## Instalation ```bash @@ -188,12 +195,15 @@ new Vue({ ### Custom request and response interceptors You can easily setup custom request and response interceptors if you use different request handling library. -Fro example, if you use **axios** in your app, request and response interceptors would look something like this: -**Important**: You must set both `request` and `response` interceptors if your uses request handling library other than `vue-resource` (default library). +**Important**: You must set both `request` and `response` interceptors at all times. ```javascript +/** +* This is example for request and response interceptors for axios library +*/ + Vue.use(VueAuthenticate, { bindRequestInterceptor: function () { this.$http.interceptors.request.use((config) => { diff --git a/dist/vue-authenticate.common.js b/dist/vue-authenticate.common.js index 1657311..60fe0cb 100644 --- a/dist/vue-authenticate.common.js +++ b/dist/vue-authenticate.common.js @@ -1,5 +1,5 @@ /*! - * vue-authenticate v1.2.7 + * vue-authenticate v1.3.0 * https://github.com/dgrubelic/vue-authenticate * Released under the MIT License. */ @@ -451,10 +451,40 @@ var defaultOptions = { logoutUrl: null, storageType: 'localStorage', storageNamespace: 'vue-authenticate', - requestDataKey: 'body', - responseDataKey: 'body', - bindRequestInterceptor: null, - bindResponseInterceptor: null, + requestDataKey: 'data', + responseDataKey: 'data', + + /** + * Default request interceptor for Axios library + * @context {VueAuthenticate} + */ + bindRequestInterceptor: function () { + var this$1 = this; + + this.$http.interceptors.request.use(function (config) { + if (this$1.isAuthenticated()) { + config.headers['Authorization'] = [ + this$1.options.tokenType, this$1.getToken() + ].join(' '); + } else { + delete config.headers['Authorization']; + } + return config + }); + }, + + /** + * Default response interceptor for Axios library + * @contect {VueAuthenticate} + */ + bindResponseInterceptor: function () { + var this$1 = this; + + this.$http.interceptors.response.use(function (response) { + this$1.setToken(response); + return response + }); + }, providers: { facebook: { @@ -1032,8 +1062,6 @@ OAuth2.prototype._stringifyRequestParams = function _stringifyRequestParams () { }; var VueAuthenticate = function VueAuthenticate($http, overrideOptions) { - var this$1 = this; - var options = objectExtend({}, defaultOptions); options = objectExtend(options, overrideOptions); var storage = StorageFactory(options); @@ -1075,28 +1103,7 @@ var VueAuthenticate = function VueAuthenticate($http, overrideOptions) { this.options.bindRequestInterceptor.call(this, this); this.options.bindResponseInterceptor.call(this, this); } else { - // By default, request and response interceptors are for vue-resource - this.$http.interceptors.push(function (request, next) { - if (this$1.isAuthenticated()) { - request.headers.set('Authorization', [ - this$1.options.tokenType, this$1.getToken() - ].join(' ')); - } else { - request.headers.delete('Authorization'); - } - - next(function (response) { - try { - var responseJson = JSON.parse(response[this$1.options.responseDataKey]); - if (responseJson[this$1.options.tokenName]) { - this$1.setToken(responseJson); - delete responseJson[this$1.options.tokenName]; - return responseJson - } - } catch(e) {} - return response - }); - }); + throw new Error('Both request and response interceptors must be functions') } }; diff --git a/dist/vue-authenticate.es2015.js b/dist/vue-authenticate.es2015.js index e4810ae..c41d9b3 100644 --- a/dist/vue-authenticate.es2015.js +++ b/dist/vue-authenticate.es2015.js @@ -1,5 +1,5 @@ /*! - * vue-authenticate v1.2.7 + * vue-authenticate v1.3.0 * https://github.com/dgrubelic/vue-authenticate * Released under the MIT License. */ @@ -449,10 +449,40 @@ var defaultOptions = { logoutUrl: null, storageType: 'localStorage', storageNamespace: 'vue-authenticate', - requestDataKey: 'body', - responseDataKey: 'body', - bindRequestInterceptor: null, - bindResponseInterceptor: null, + requestDataKey: 'data', + responseDataKey: 'data', + + /** + * Default request interceptor for Axios library + * @context {VueAuthenticate} + */ + bindRequestInterceptor: function () { + var this$1 = this; + + this.$http.interceptors.request.use(function (config) { + if (this$1.isAuthenticated()) { + config.headers['Authorization'] = [ + this$1.options.tokenType, this$1.getToken() + ].join(' '); + } else { + delete config.headers['Authorization']; + } + return config + }); + }, + + /** + * Default response interceptor for Axios library + * @contect {VueAuthenticate} + */ + bindResponseInterceptor: function () { + var this$1 = this; + + this.$http.interceptors.response.use(function (response) { + this$1.setToken(response); + return response + }); + }, providers: { facebook: { @@ -1030,8 +1060,6 @@ OAuth2.prototype._stringifyRequestParams = function _stringifyRequestParams () { }; var VueAuthenticate = function VueAuthenticate($http, overrideOptions) { - var this$1 = this; - var options = objectExtend({}, defaultOptions); options = objectExtend(options, overrideOptions); var storage = StorageFactory(options); @@ -1073,28 +1101,7 @@ var VueAuthenticate = function VueAuthenticate($http, overrideOptions) { this.options.bindRequestInterceptor.call(this, this); this.options.bindResponseInterceptor.call(this, this); } else { - // By default, request and response interceptors are for vue-resource - this.$http.interceptors.push(function (request, next) { - if (this$1.isAuthenticated()) { - request.headers.set('Authorization', [ - this$1.options.tokenType, this$1.getToken() - ].join(' ')); - } else { - request.headers.delete('Authorization'); - } - - next(function (response) { - try { - var responseJson = JSON.parse(response[this$1.options.responseDataKey]); - if (responseJson[this$1.options.tokenName]) { - this$1.setToken(responseJson); - delete responseJson[this$1.options.tokenName]; - return responseJson - } - } catch(e) {} - return response - }); - }); + throw new Error('Both request and response interceptors must be functions') } }; diff --git a/dist/vue-authenticate.js b/dist/vue-authenticate.js index c7f2444..ef18ac5 100644 --- a/dist/vue-authenticate.js +++ b/dist/vue-authenticate.js @@ -1,5 +1,5 @@ /*! - * vue-authenticate v1.2.7 + * vue-authenticate v1.3.0 * https://github.com/dgrubelic/vue-authenticate * Released under the MIT License. */ @@ -455,10 +455,40 @@ var defaultOptions = { logoutUrl: null, storageType: 'localStorage', storageNamespace: 'vue-authenticate', - requestDataKey: 'body', - responseDataKey: 'body', - bindRequestInterceptor: null, - bindResponseInterceptor: null, + requestDataKey: 'data', + responseDataKey: 'data', + + /** + * Default request interceptor for Axios library + * @context {VueAuthenticate} + */ + bindRequestInterceptor: function () { + var this$1 = this; + + this.$http.interceptors.request.use(function (config) { + if (this$1.isAuthenticated()) { + config.headers['Authorization'] = [ + this$1.options.tokenType, this$1.getToken() + ].join(' '); + } else { + delete config.headers['Authorization']; + } + return config + }); + }, + + /** + * Default response interceptor for Axios library + * @contect {VueAuthenticate} + */ + bindResponseInterceptor: function () { + var this$1 = this; + + this.$http.interceptors.response.use(function (response) { + this$1.setToken(response); + return response + }); + }, providers: { facebook: { @@ -1036,8 +1066,6 @@ OAuth2.prototype._stringifyRequestParams = function _stringifyRequestParams () { }; var VueAuthenticate = function VueAuthenticate($http, overrideOptions) { - var this$1 = this; - var options = objectExtend({}, defaultOptions); options = objectExtend(options, overrideOptions); var storage = StorageFactory(options); @@ -1079,28 +1107,7 @@ var VueAuthenticate = function VueAuthenticate($http, overrideOptions) { this.options.bindRequestInterceptor.call(this, this); this.options.bindResponseInterceptor.call(this, this); } else { - // By default, request and response interceptors are for vue-resource - this.$http.interceptors.push(function (request, next) { - if (this$1.isAuthenticated()) { - request.headers.set('Authorization', [ - this$1.options.tokenType, this$1.getToken() - ].join(' ')); - } else { - request.headers.delete('Authorization'); - } - - next(function (response) { - try { - var responseJson = JSON.parse(response[this$1.options.responseDataKey]); - if (responseJson[this$1.options.tokenName]) { - this$1.setToken(responseJson); - delete responseJson[this$1.options.tokenName]; - return responseJson - } - } catch(e) {} - return response - }); - }); + throw new Error('Both request and response interceptors must be functions') } }; diff --git a/dist/vue-authenticate.min.js b/dist/vue-authenticate.min.js index 3f8453f..4c9ec95 100644 --- a/dist/vue-authenticate.min.js +++ b/dist/vue-authenticate.min.js @@ -1,7 +1,7 @@ /*! - * vue-authenticate v1.2.7 + * vue-authenticate v1.3.0 * https://github.com/dgrubelic/vue-authenticate * Released under the MIT License. */ -!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.VueAuthenticate=e()}(this,function(){"use strict";function t(t){return t.replace(/([\:\-\_]+(.))/g,function(t,e,o,r){return r?o.toUpperCase():o})}function e(t){return null!==t&&"object"==typeof t}function o(t){return"string"==typeof t}function r(t){return"function"==typeof t}function n(t,e){return null==t||null==e?t:(Object.keys(e).forEach(function(o){"[object Object]"==Object.prototype.toString.call(e[o])?"[object Object]"!=Object.prototype.toString.call(t[o])?t[o]=e[o]:t[o]=n(t[o],e[o]):t[o]=e[o]}),t)}function i(t,e){if(/^(?:[a-z]+:)?\/\//i.test(e))return e;var o=[t,e].join("/");return function(t){return t.replace(/[\/]+/g,"/").replace(/\/\?/g,"?").replace(/\/\#/g,"#").replace(/\:\//g,"://")}(o)}function s(t){var e="https:"===t.protocol;return t.protocol+"//"+t.hostname+":"+(t.port||(e?"443":"80"))+(/^\//.test(t.pathname)?t.pathname:"/"+t.pathname)}function a(t){var e,o,r={};return(t||"").split("&").forEach(function(t){t&&(o=t.split("="),e=decodeURIComponent(o[0]),r[e]=!o[1]||decodeURIComponent(o[1]))}),r}function p(t){var e;if("undefined"!=typeof module&&module.exports)try{e=require("buffer").Buffer}catch(t){}var o=String.fromCharCode,r=new RegExp(["[À-ß][€-¿]","[à-ï][€-¿]{2}","[ð-÷][€-¿]{3}"].join("|"),"g"),n=function(t){switch(t.length){case 4:var e=(7&t.charCodeAt(0))<<18|(63&t.charCodeAt(1))<<12|(63&t.charCodeAt(2))<<6|63&t.charCodeAt(3),r=e-65536;return o(55296+(r>>>10))+o(56320+(1023&r));case 3:return o((15&t.charCodeAt(0))<<12|(63&t.charCodeAt(1))<<6|63&t.charCodeAt(2));default:return o((31&t.charCodeAt(0))<<6|63&t.charCodeAt(1))}},i=function(t){return t.replace(r,n)};return(e?function(t){return(t.constructor===e.constructor?t:new e(t,"base64")).toString()}:function(t){return i(atob(t))})(String(t).replace(/[-_]/g,function(t){return"-"===t?"+":"/"}).replace(/[^A-Za-z0-9\+\/]/g,""))}function u(){}function c(t,e){return function(){t.apply(e,arguments)}}function l(t){if("object"!=typeof this)throw new TypeError("Promises must be constructed via new");if("function"!=typeof t)throw new TypeError("not a function");this._state=0,this._handled=!1,this._value=void 0,this._deferreds=[],v(t,this)}function h(t,e){for(;3===t._state;)t=t._value;if(0===t._state)return void t._deferreds.push(e);t._handled=!0,l._immediateFn(function(){var o=1===t._state?e.onFulfilled:e.onRejected;if(null===o)return void(1===t._state?d:f)(e.promise,t._value);var r;try{r=o(t._value)}catch(t){return void f(e.promise,t)}d(e.promise,r)})}function d(t,e){try{if(e===t)throw new TypeError("A promise cannot be resolved with itself.");if(e&&("object"==typeof e||"function"==typeof e)){var o=e.then;if(e instanceof l)return t._state=3,t._value=e,void g(t);if("function"==typeof o)return void v(c(o,e),t)}t._state=1,t._value=e,g(t)}catch(e){f(t,e)}}function f(t,e){t._state=2,t._value=e,g(t)}function g(t){2===t._state&&0===t._deferreds.length&&l._immediateFn(function(){t._handled||l._unhandledRejectionFn(t._value)});for(var e=0,o=t._deferreds.length;e>>10))+o(56320+(1023&r));case 3:return o((15&t.charCodeAt(0))<<12|(63&t.charCodeAt(1))<<6|63&t.charCodeAt(2));default:return o((31&t.charCodeAt(0))<<6|63&t.charCodeAt(1))}},i=function(t){return t.replace(r,n)};return(e?function(t){return(t.constructor===e.constructor?t:new e(t,"base64")).toString()}:function(t){return i(atob(t))})(String(t).replace(/[-_]/g,function(t){return"-"===t?"+":"/"}).replace(/[^A-Za-z0-9\+\/]/g,""))}function p(){}function c(t,e){return function(){t.apply(e,arguments)}}function h(t){if("object"!=typeof this)throw new TypeError("Promises must be constructed via new");if("function"!=typeof t)throw new TypeError("not a function");this._state=0,this._handled=!1,this._value=void 0,this._deferreds=[],v(t,this)}function l(t,e){for(;3===t._state;)t=t._value;if(0===t._state)return void t._deferreds.push(e);t._handled=!0,h._immediateFn(function(){var o=1===t._state?e.onFulfilled:e.onRejected;if(null===o)return void(1===t._state?d:f)(e.promise,t._value);var r;try{r=o(t._value)}catch(t){return void f(e.promise,t)}d(e.promise,r)})}function d(t,e){try{if(e===t)throw new TypeError("A promise cannot be resolved with itself.");if(e&&("object"==typeof e||"function"==typeof e)){var o=e.then;if(e instanceof h)return t._state=3,t._value=e,void g(t);if("function"==typeof o)return void v(c(o,e),t)}t._state=1,t._value=e,g(t)}catch(e){f(t,e)}}function f(t,e){t._state=2,t._value=e,g(t)}function g(t){2===t._state&&0===t._deferreds.length&&h._immediateFn(function(){t._handled||h._unhandledRejectionFn(t._value)});for(var e=0,o=t._deferreds.length;e @@ -61,8 +73,9 @@ - + + - \ No newline at end of file + diff --git a/example/index.js b/example/index.js index 4b52b78..e491c00 100644 --- a/example/index.js +++ b/example/index.js @@ -1,9 +1,9 @@ -Vue.http.options.root = 'http://localhost:3000'; +axios.defaults.baseURL = 'http://localhost:4000'; Vue.use(VueRouter) -Vue.use(VueResource) Vue.use(VueAuthenticate, { - baseUrl: 'http://localhost:4000', + tokenName: 'access_token', + baseUrl: 'http://localhost:4000', providers: { // Define OAuth providers config } @@ -21,8 +21,16 @@ var router = new VueRouter({ response: null } }, + computed: { + isAuthenticated: function () { + console.log(this.$auth.isAuthenticated()); + return this.$auth.isAuthenticated(); + } + }, template: `
+
You are successfully authenticated
+ @@ -46,7 +54,8 @@ var router = new VueRouter({ `, methods: { - authLogin: function() { + authLogin: function () { + var this_ = this; let user = { email: 'john.doe@domain.com', password: 'pass123456' @@ -57,13 +66,14 @@ var router = new VueRouter({ } this.$auth.login(user).then(function (response) { - this.response = response + this_.response = response - console.log(this.$auth.isAuthenticated()) + console.log(this_.$auth.isAuthenticated()) }) }, - authRegister: function() { + authRegister: function () { + var this_ = this; let user = { name: 'John Doe', email: 'john.doe@domain.com', @@ -75,9 +85,9 @@ var router = new VueRouter({ } this.$auth.register(user).then(function (response) { - this.response = response + this_.response = response - console.log(this.$auth.isAuthenticated()) + console.log(this_.$auth.isAuthenticated()) }) }, @@ -129,6 +139,7 @@ var router = new VueRouter({ this_.response = authResponse } }).catch(function (err) { + console.log(err) this_.response = err }) } diff --git a/example/server.js b/example/server.js index 73459b0..bee2c94 100644 --- a/example/server.js +++ b/example/server.js @@ -67,9 +67,7 @@ function loginAuth(req, res) { name: 'John Doe', email: 'john.doe@domain.com', created_at: new Date(), - - token: 'eyJpZCI6MSwiZW1haWwiOiJqb2huLmRvZUBkb21haW4uY29tIiwibmFtZSI6IkpvaG4gRG9lIiwiYWxnIjoiSFMyNTYifQ.eyJpZCI6MSwiZW1haWwiOiJqb2huLmRvZUBkb21haW4uY29tIiwibmFtZSI6IkpvaG4gRG9lIn0.CyXHbjCBjA4uLuOwefCGbFw1ulQtF-QfS9-X0fFUCGE' - + access_token: 'eyJpZCI6MSwiZW1haWwiOiJqb2huLmRvZUBkb21haW4uY29tIiwibmFtZSI6IkpvaG4gRG9lIiwiYWxnIjoiSFMyNTYifQ.eyJpZCI6MSwiZW1haWwiOiJqb2huLmRvZUBkb21haW4uY29tIiwibmFtZSI6IkpvaG4gRG9lIn0.CyXHbjCBjA4uLuOwefCGbFw1ulQtF-QfS9-X0fFUCGE' }) } @@ -79,9 +77,7 @@ function registerAuth(req, res) { name: 'John Doe', email: 'john.doe@domain.com', created_at: new Date(), - - token: 'eyJpZCI6MSwiZW1haWwiOiJqb2huLmRvZUBkb21haW4uY29tIiwibmFtZSI6IkpvaG4gRG9lIiwiYWxnIjoiSFMyNTYifQ.eyJpZCI6MSwiZW1haWwiOiJqb2huLmRvZUBkb21haW4uY29tIiwibmFtZSI6IkpvaG4gRG9lIn0.CyXHbjCBjA4uLuOwefCGbFw1ulQtF-QfS9-X0fFUCGE' - + access_token: 'eyJpZCI6MSwiZW1haWwiOiJqb2huLmRvZUBkb21haW4uY29tIiwibmFtZSI6IkpvaG4gRG9lIiwiYWxnIjoiSFMyNTYifQ.eyJpZCI6MSwiZW1haWwiOiJqb2huLmRvZUBkb21haW4uY29tIiwibmFtZSI6IkpvaG4gRG9lIn0.CyXHbjCBjA4uLuOwefCGbFw1ulQtF-QfS9-X0fFUCGE' }) } diff --git a/example/vue-authenticate.js b/example/vue-authenticate.js index c7f2444..ef18ac5 100644 --- a/example/vue-authenticate.js +++ b/example/vue-authenticate.js @@ -1,5 +1,5 @@ /*! - * vue-authenticate v1.2.7 + * vue-authenticate v1.3.0 * https://github.com/dgrubelic/vue-authenticate * Released under the MIT License. */ @@ -455,10 +455,40 @@ var defaultOptions = { logoutUrl: null, storageType: 'localStorage', storageNamespace: 'vue-authenticate', - requestDataKey: 'body', - responseDataKey: 'body', - bindRequestInterceptor: null, - bindResponseInterceptor: null, + requestDataKey: 'data', + responseDataKey: 'data', + + /** + * Default request interceptor for Axios library + * @context {VueAuthenticate} + */ + bindRequestInterceptor: function () { + var this$1 = this; + + this.$http.interceptors.request.use(function (config) { + if (this$1.isAuthenticated()) { + config.headers['Authorization'] = [ + this$1.options.tokenType, this$1.getToken() + ].join(' '); + } else { + delete config.headers['Authorization']; + } + return config + }); + }, + + /** + * Default response interceptor for Axios library + * @contect {VueAuthenticate} + */ + bindResponseInterceptor: function () { + var this$1 = this; + + this.$http.interceptors.response.use(function (response) { + this$1.setToken(response); + return response + }); + }, providers: { facebook: { @@ -1036,8 +1066,6 @@ OAuth2.prototype._stringifyRequestParams = function _stringifyRequestParams () { }; var VueAuthenticate = function VueAuthenticate($http, overrideOptions) { - var this$1 = this; - var options = objectExtend({}, defaultOptions); options = objectExtend(options, overrideOptions); var storage = StorageFactory(options); @@ -1079,28 +1107,7 @@ var VueAuthenticate = function VueAuthenticate($http, overrideOptions) { this.options.bindRequestInterceptor.call(this, this); this.options.bindResponseInterceptor.call(this, this); } else { - // By default, request and response interceptors are for vue-resource - this.$http.interceptors.push(function (request, next) { - if (this$1.isAuthenticated()) { - request.headers.set('Authorization', [ - this$1.options.tokenType, this$1.getToken() - ].join(' ')); - } else { - request.headers.delete('Authorization'); - } - - next(function (response) { - try { - var responseJson = JSON.parse(response[this$1.options.responseDataKey]); - if (responseJson[this$1.options.tokenName]) { - this$1.setToken(responseJson); - delete responseJson[this$1.options.tokenName]; - return responseJson - } - } catch(e) {} - return response - }); - }); + throw new Error('Both request and response interceptors must be functions') } }; diff --git a/package.json b/package.json index f751858..b87c26f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vue-authenticate", - "version": "1.2.7", + "version": "1.3.0", "description": "Authentication library for Vue.js", "main": "dist/vue-authenticate.js", "module": "dist/vue-authenticate.es2015.js", @@ -25,7 +25,8 @@ }, "license": "MIT", "dependencies": { - "vue": "^2.2.5" + "vue": "^2.2.5", + "vue-axios": "^2.0.2" }, "devDependencies": { "axios": "^0.15.3", diff --git a/src/authenticate.js b/src/authenticate.js index 8ef2df2..f1c322a 100644 --- a/src/authenticate.js +++ b/src/authenticate.js @@ -48,28 +48,7 @@ export default class VueAuthenticate { this.options.bindRequestInterceptor.call(this, this) this.options.bindResponseInterceptor.call(this, this) } else { - // By default, request and response interceptors are for vue-resource - this.$http.interceptors.push((request, next) => { - if (this.isAuthenticated()) { - request.headers.set('Authorization', [ - this.options.tokenType, this.getToken() - ].join(' ')) - } else { - request.headers.delete('Authorization') - } - - next((response) => { - try { - var responseJson = JSON.parse(response[this.options.responseDataKey]) - if (responseJson[this.options.tokenName]) { - this.setToken(responseJson) - delete responseJson[this.options.tokenName] - return responseJson - } - } catch(e) {} - return response - }) - }) + throw new Error('Both request and response interceptors must be functions') } } @@ -252,4 +231,4 @@ export default class VueAuthenticate { }) }) } -} \ No newline at end of file +} diff --git a/src/options.js b/src/options.js index f54605d..a4f130d 100644 --- a/src/options.js +++ b/src/options.js @@ -12,10 +12,36 @@ export default { logoutUrl: null, storageType: 'localStorage', storageNamespace: 'vue-authenticate', - requestDataKey: 'body', - responseDataKey: 'body', - bindRequestInterceptor: null, - bindResponseInterceptor: null, + requestDataKey: 'data', + responseDataKey: 'data', + + /** + * Default request interceptor for Axios library + * @context {VueAuthenticate} + */ + bindRequestInterceptor: function () { + this.$http.interceptors.request.use((config) => { + if (this.isAuthenticated()) { + config.headers['Authorization'] = [ + this.options.tokenType, this.getToken() + ].join(' ') + } else { + delete config.headers['Authorization'] + } + return config + }) + }, + + /** + * Default response interceptor for Axios library + * @contect {VueAuthenticate} + */ + bindResponseInterceptor: function () { + this.$http.interceptors.response.use((response) => { + this.setToken(response) + return response + }) + }, providers: { facebook: {