diff --git a/browser/gotrue.js b/browser/gotrue.js index 6851c541..1c93c5f8 100644 --- a/browser/gotrue.js +++ b/browser/gotrue.js @@ -1 +1 @@ -(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o0&&arguments[0]!==undefined?arguments[0]:{};return _extends({},this.defaultHeaders,{"Content-Type":"application/json"},_headers)}},{key:"parseJsonResponse",value:function parseJsonResponse(response){return response.json().then(function(json){if(!response.ok){return Promise.reject(new JSONHTTPError(response,json))}var pagination=(0,_pagination.getPagination)(response);return pagination?{pagination:pagination,items:json}:json})}},{key:"request",value:function request(path){var _this4=this;var options=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};var headers=this.headers(options.headers||{});if(this._sameOrigin){options.credentials=options.credentials||"same-origin"}return fetch(this.apiURL+path,_extends({},options,{headers:headers})).then(function(response){var contentType=response.headers.get("Content-Type");if(contentType&&contentType.match(/json/)){return _this4.parseJsonResponse(response)}if(!response.ok){return response.text().then(function(data){return Promise.reject(new TextHTTPError(response,data))})}return response.text().then(function(data){data})})}}]);return API}();exports.default=API},{"./pagination":2}],2:[function(require,module,exports){"use strict";Object.defineProperty(exports,"__esModule",{value:true});var _slicedToArray=function(){function sliceIterator(arr,i){var _arr=[];var _n=true;var _d=false;var _e=undefined;try{for(var _i=arr[Symbol.iterator](),_s;!(_n=(_s=_i.next()).done);_n=true){_arr.push(_s.value);if(i&&_arr.length===i)break}}catch(err){_d=true;_e=err}finally{try{if(!_n&&_i["return"])_i["return"]()}finally{if(_d)throw _e}}return _arr}return function(arr,i){if(Array.isArray(arr)){return arr}else if(Symbol.iterator in Object(arr)){return sliceIterator(arr,i)}else{throw new TypeError("Invalid attempt to destructure non-iterable instance")}}}();exports.getPagination=getPagination;function getPagination(response){var links=response.headers.get("Link");var pagination={};if(links==null){return null}links=links.split(",");var total=response.headers.get("X-Total-Count");for(var i=0,len=links.length;i1&&arguments[1]!==undefined?arguments[1]:{};return this.user._request("/admin/users/"+user.id,{method:"PUT",body:JSON.stringify(attributes)})}},{key:"createUser",value:function createUser(email,password){var attributes=arguments.length>2&&arguments[2]!==undefined?arguments[2]:{};attributes.email=email;attributes.password=password;return this.user._request("/admin/users",{method:"POST",body:JSON.stringify(attributes)})}},{key:"deleteUser",value:function deleteUser(user){return this.user._request("/admin/users/"+user.id,{method:"DELETE"})}}]);return Admin}();exports.default=Admin},{}],4:[function(require,module,exports){"use strict";Object.defineProperty(exports,"__esModule",{value:true});var _createClass=function(){function defineProperties(target,props){for(var i=0;i0&&arguments[0]!==undefined?arguments[0]:{},_ref$APIUrl=_ref.APIUrl,APIUrl=_ref$APIUrl===undefined?defaultApiURL:_ref$APIUrl,_ref$audience=_ref.audience,audience=_ref$audience===undefined?"":_ref$audience,_ref$setCookie=_ref.setCookie,setCookie=_ref$setCookie===undefined?false:_ref$setCookie;_classCallCheck(this,GoTrue);if(APIUrl.match(HTTPRegexp)){console.warn("Warning:\n\nDO NOT USE HTTP IN PRODUCTION FOR GOTRUE EVER!\nGoTrue REQUIRES HTTPS to work securely.")}if(audience){this.audience=audience}console.log("setCookie: %o",setCookie);this.setCookie=setCookie;this.api=new _microApiClient2.default(APIUrl)}_createClass(GoTrue,[{key:"_request",value:function _request(path){var options=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};options.headers=options.headers||{};var aud=options.audience||this.audience;if(aud){options.headers["X-JWT-AUD"]=aud}return this.api.request(path,options).catch(function(err){if(err instanceof _microApiClient.JSONHTTPError&&err.json){if(err.json.msg){err.message=err.json.msg}else if(err.json.error){err.message=err.json.error+": "+err.json.error_description}}return Promise.reject(err)})}},{key:"settings",value:function settings(){return this._request("/settings")}},{key:"signup",value:function signup(email,password,data){return this._request("/signup",{method:"POST",body:JSON.stringify({email:email,password:password,data:data})})}},{key:"login",value:function login(email,password,remember){var _this=this;this._setRememberHeaders(remember);return this._request("/token",{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:"grant_type=password&username="+encodeURIComponent(email)+"&password="+encodeURIComponent(password)}).then(function(response){_user2.default.removeSavedSession();return _this.createUser(response,remember)})}},{key:"loginExternalUrl",value:function loginExternalUrl(provider){return this.api.apiURL+"/authorize?provider="+provider}},{key:"confirm",value:function confirm(token,remember){this._setRememberHeaders(remember);return this.verify("signup",token,remember)}},{key:"requestPasswordRecovery",value:function requestPasswordRecovery(email){return this._request("/recover",{method:"POST",body:JSON.stringify({email:email})})}},{key:"recover",value:function recover(token,remember){this._setRememberHeaders(remember);return this.verify("recovery",token,remember)}},{key:"acceptInvite",value:function acceptInvite(token,password,remember){var _this2=this;this._setRememberHeaders(remember);return this._request("/verify",{method:"POST",body:JSON.stringify({token:token,password:password,type:"signup"})}).then(function(response){return _this2.createUser(response,remember)})}},{key:"acceptInviteExternalUrl",value:function acceptInviteExternalUrl(provider,token){return this.api.apiURL+"/authorize?provider="+provider+"&invite_token="+token}},{key:"createUser",value:function createUser(tokenResponse){var remember=arguments.length>1&&arguments[1]!==undefined?arguments[1]:false;this._setRememberHeaders(remember);var user=new _user2.default(this.api,tokenResponse,this.audience);return user.getUserData().then(function(user){if(remember){user._saveSession()}return user})}},{key:"currentUser",value:function currentUser(){var user=_user2.default.recoverSession(this.api);user&&this._setRememberHeaders(user._fromStorage);return user}},{key:"verify",value:function verify(type,token,remember){var _this3=this;this._setRememberHeaders(remember);return this._request("/verify",{method:"POST",body:JSON.stringify({token:token,type:type})}).then(function(response){return _this3.createUser(response,remember)})}},{key:"_setRememberHeaders",value:function _setRememberHeaders(remember){if(this.setCookie){this.api.defaultHeaders=this.api.defaultHeaders||{};this.api.defaultHeaders["X-Use-Cookie"]=remember?"1":"session"}}}]);return GoTrue}();exports.default=GoTrue;if(typeof window!=="undefined"){window.GoTrue=GoTrue}},{"./user":5,"micro-api-client":1}],5:[function(require,module,exports){"use strict";Object.defineProperty(exports,"__esModule",{value:true});var _extends=Object.assign||function(target){for(var i=1;i1&&arguments[1]!==undefined?arguments[1]:{};options.headers=options.headers||{};var aud=options.audience||this.audience;if(aud){options.headers["X-JWT-AUD"]=aud}return this.jwt().then(function(token){return _this3.api.request(path,_extends({headers:Object.assign(options.headers,{Authorization:"Bearer "+token})},options)).catch(function(err){if(err instanceof _microApiClient.JSONHTTPError&&err.json){if(err.json.msg){err.message=err.json.msg}else if(err.json.error){err.message=err.json.error+": "+err.json.error_description}}return Promise.reject(err)})})}},{key:"getUserData",value:function getUserData(){return this._request("/user").then(this._saveUserData.bind(this)).then(this._refreshSavedSession.bind(this))}},{key:"_saveUserData",value:function _saveUserData(attributes,fromStorage){for(var key in attributes){if(key in User.prototype||key in forbiddenUpdateAttributes){continue}this[key]=attributes[key]}if(fromStorage){this._fromStorage=true}return this}},{key:"_processTokenResponse",value:function _processTokenResponse(tokenResponse){this.token=tokenResponse;var claims=void 0;try{claims=JSON.parse(atob(tokenResponse.access_token.split(".")[1]));this.token.expires_at=claims.exp*1e3}catch(e){console.error(new Error("Gotrue-js: Failed to parse tokenResponse claims"));console.error(tokenResponse)}}},{key:"_refreshSavedSession",value:function _refreshSavedSession(){if(localStorage.getItem(storageKey)){this._saveSession()}return this}},{key:"_saveSession",value:function _saveSession(){localStorage.setItem(storageKey,JSON.stringify(this._details));return this}},{key:"tokenDetails",value:function tokenDetails(){return this.token}},{key:"clearSession",value:function clearSession(){User.removeSavedSession();this.token=null;currentUser=null}},{key:"admin",get:function get(){return new _admin2.default(this)}},{key:"_details",get:function get(){var userCopy={};for(var key in this){if(key in User.prototype||key in forbiddenSaveAttributes){continue}userCopy[key]=this[key]}return userCopy}}],[{key:"removeSavedSession",value:function removeSavedSession(){localStorage.removeItem(storageKey)}},{key:"recoverSession",value:function recoverSession(apiInstance){if(currentUser){return currentUser}var json=localStorage.getItem(storageKey);if(json){try{var data=JSON.parse(json);var url=data.url,token=data.token,audience=data.audience;if(!url||!token){return null}var api=apiInstance||new _microApiClient2.default(url,{});return new User(api,token,audience)._saveUserData(data,true)}catch(ex){console.error(new Error("Gotrue-js: Error recovering session: "+ex));return null}}return null}}]);return User}();exports.default=User},{"./admin":3,"micro-api-client":1}]},{},[4]); +(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o0&&arguments[0]!==undefined?arguments[0]:{};return _extends({},this.defaultHeaders,{"Content-Type":"application/json"},_headers)}},{key:"parseJsonResponse",value:function parseJsonResponse(response){return response.json().then(function(json){if(!response.ok){return Promise.reject(new JSONHTTPError(response,json))}var pagination=(0,_pagination.getPagination)(response);return pagination?{pagination:pagination,items:json}:json})}},{key:"request",value:function request(path){var _this4=this;var options=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};var headers=this.headers(options.headers||{});if(this._sameOrigin){options.credentials=options.credentials||"same-origin"}return fetch(this.apiURL+path,_extends({},options,{headers:headers})).then(function(response){var contentType=response.headers.get("Content-Type");if(contentType&&contentType.match(/json/)){return _this4.parseJsonResponse(response)}if(!response.ok){return response.text().then(function(data){return Promise.reject(new TextHTTPError(response,data))})}return response.text().then(function(data){data})})}}]);return API}();exports.default=API},{"./pagination":2}],2:[function(require,module,exports){"use strict";Object.defineProperty(exports,"__esModule",{value:true});var _slicedToArray=function(){function sliceIterator(arr,i){var _arr=[];var _n=true;var _d=false;var _e=undefined;try{for(var _i=arr[Symbol.iterator](),_s;!(_n=(_s=_i.next()).done);_n=true){_arr.push(_s.value);if(i&&_arr.length===i)break}}catch(err){_d=true;_e=err}finally{try{if(!_n&&_i["return"])_i["return"]()}finally{if(_d)throw _e}}return _arr}return function(arr,i){if(Array.isArray(arr)){return arr}else if(Symbol.iterator in Object(arr)){return sliceIterator(arr,i)}else{throw new TypeError("Invalid attempt to destructure non-iterable instance")}}}();exports.getPagination=getPagination;function getPagination(response){var links=response.headers.get("Link");var pagination={};if(links==null){return null}links=links.split(",");var total=response.headers.get("X-Total-Count");for(var i=0,len=links.length;i1&&arguments[1]!==undefined?arguments[1]:{};return this.user._request("/admin/users/"+user.id,{method:"PUT",body:JSON.stringify(attributes)})}},{key:"createUser",value:function createUser(email,password){var attributes=arguments.length>2&&arguments[2]!==undefined?arguments[2]:{};attributes.email=email;attributes.password=password;return this.user._request("/admin/users",{method:"POST",body:JSON.stringify(attributes)})}},{key:"deleteUser",value:function deleteUser(user){return this.user._request("/admin/users/"+user.id,{method:"DELETE"})}}]);return Admin}();exports.default=Admin},{}],4:[function(require,module,exports){"use strict";Object.defineProperty(exports,"__esModule",{value:true});var _createClass=function(){function defineProperties(target,props){for(var i=0;i0&&arguments[0]!==undefined?arguments[0]:{},_ref$APIUrl=_ref.APIUrl,APIUrl=_ref$APIUrl===undefined?defaultApiURL:_ref$APIUrl,_ref$audience=_ref.audience,audience=_ref$audience===undefined?"":_ref$audience,_ref$setCookie=_ref.setCookie,setCookie=_ref$setCookie===undefined?false:_ref$setCookie;_classCallCheck(this,GoTrue);if(APIUrl.match(HTTPRegexp)){console.warn("Warning:\n\nDO NOT USE HTTP IN PRODUCTION FOR GOTRUE EVER!\nGoTrue REQUIRES HTTPS to work securely.")}if(audience){this.audience=audience}console.log("setCookie: %o",setCookie);this.setCookie=setCookie;this.api=new _microApiClient2.default(APIUrl)}_createClass(GoTrue,[{key:"_request",value:function _request(path){var options=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};options.headers=options.headers||{};var aud=options.audience||this.audience;if(aud){options.headers["X-JWT-AUD"]=aud}return this.api.request(path,options).catch(function(err){if(err instanceof _microApiClient.JSONHTTPError&&err.json){if(err.json.msg){err.message=err.json.msg}else if(err.json.error){err.message=err.json.error+": "+err.json.error_description}}return Promise.reject(err)})}},{key:"settings",value:function settings(){return this._request("/settings")}},{key:"signup",value:function signup(email,password,data){return this._request("/signup",{method:"POST",body:JSON.stringify({email:email,password:password,data:data})})}},{key:"login",value:function login(email,password,remember){var _this=this;this._setRememberHeaders(remember);return this._request("/token",{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:"grant_type=password&username="+encodeURIComponent(email)+"&password="+encodeURIComponent(password)}).then(function(response){_user2.default.removeSavedSession();return _this.createUser(response,remember)})}},{key:"loginExternalUrl",value:function loginExternalUrl(provider){return this.api.apiURL+"/authorize?provider="+provider}},{key:"confirm",value:function confirm(token,remember){this._setRememberHeaders(remember);return this.verify("signup",token,remember)}},{key:"requestPasswordRecovery",value:function requestPasswordRecovery(email){return this._request("/recover",{method:"POST",body:JSON.stringify({email:email})})}},{key:"recover",value:function recover(token,remember){this._setRememberHeaders(remember);return this.verify("recovery",token,remember)}},{key:"acceptInvite",value:function acceptInvite(token,password,remember){var _this2=this;this._setRememberHeaders(remember);return this._request("/verify",{method:"POST",body:JSON.stringify({token:token,password:password,type:"signup"})}).then(function(response){return _this2.createUser(response,remember)})}},{key:"acceptInviteExternalUrl",value:function acceptInviteExternalUrl(provider,token){return this.api.apiURL+"/authorize?provider="+provider+"&invite_token="+token}},{key:"createUser",value:function createUser(tokenResponse){var remember=arguments.length>1&&arguments[1]!==undefined?arguments[1]:false;this._setRememberHeaders(remember);var user=new _user2.default(this.api,tokenResponse,this.audience);return user.getUserData().then(function(user){if(remember){user._saveSession()}return user})}},{key:"currentUser",value:function currentUser(){var user=_user2.default.recoverSession(this.api);user&&this._setRememberHeaders(user._fromStorage);return user}},{key:"verify",value:function verify(type,token,remember){var _this3=this;this._setRememberHeaders(remember);return this._request("/verify",{method:"POST",body:JSON.stringify({token:token,type:type})}).then(function(response){return _this3.createUser(response,remember)})}},{key:"_setRememberHeaders",value:function _setRememberHeaders(remember){if(this.setCookie){this.api.defaultHeaders=this.api.defaultHeaders||{};this.api.defaultHeaders["X-Use-Cookie"]=remember?"1":"session"}}}]);return GoTrue}();exports.default=GoTrue;if(typeof window!=="undefined"){window.GoTrue=GoTrue}},{"./user":5,"micro-api-client":1}],5:[function(require,module,exports){"use strict";Object.defineProperty(exports,"__esModule",{value:true});var _extends=Object.assign||function(target){for(var i=1;i1&&arguments[1]!==undefined?arguments[1]:{};options.headers=options.headers||{};var aud=options.audience||this.audience;if(aud){options.headers["X-JWT-AUD"]=aud}return this.jwt().then(function(token){return _this3.api.request(path,_extends({headers:Object.assign(options.headers,{Authorization:"Bearer "+token})},options)).catch(function(err){if(err instanceof _microApiClient.JSONHTTPError&&err.json){if(err.json.msg){err.message=err.json.msg}else if(err.json.error){err.message=err.json.error+": "+err.json.error_description}}return Promise.reject(err)})})}},{key:"getUserData",value:function getUserData(){return this._request("/user").then(this._saveUserData.bind(this)).then(this._refreshSavedSession.bind(this))}},{key:"_saveUserData",value:function _saveUserData(attributes,fromStorage){for(var key in attributes){if(key in User.prototype||key in forbiddenUpdateAttributes){continue}this[key]=attributes[key]}if(fromStorage){this._fromStorage=true}return this}},{key:"_processTokenResponse",value:function _processTokenResponse(tokenResponse){this.token=tokenResponse;var claims=void 0;try{claims=JSON.parse(urlBase64Decode(tokenResponse.access_token.split(".")[1]));this.token.expires_at=claims.exp*1e3}catch(e){console.error(new Error("Gotrue-js: Failed to parse tokenResponse claims: "+tokenResponse))}}},{key:"_refreshSavedSession",value:function _refreshSavedSession(){if(localStorage.getItem(storageKey)){this._saveSession()}return this}},{key:"_saveSession",value:function _saveSession(){localStorage.setItem(storageKey,JSON.stringify(this._details));return this}},{key:"tokenDetails",value:function tokenDetails(){return this.token}},{key:"clearSession",value:function clearSession(){User.removeSavedSession();this.token=null;currentUser=null}},{key:"admin",get:function get(){return new _admin2.default(this)}},{key:"_details",get:function get(){var userCopy={};for(var key in this){if(key in User.prototype||key in forbiddenSaveAttributes){continue}userCopy[key]=this[key]}return userCopy}}],[{key:"removeSavedSession",value:function removeSavedSession(){localStorage.removeItem(storageKey)}},{key:"recoverSession",value:function recoverSession(apiInstance){if(currentUser){return currentUser}var json=localStorage.getItem(storageKey);if(json){try{var data=JSON.parse(json);var url=data.url,token=data.token,audience=data.audience;if(!url||!token){return null}var api=apiInstance||new _microApiClient2.default(url,{});return new User(api,token,audience)._saveUserData(data,true)}catch(ex){console.error(new Error("Gotrue-js: Error recovering session: "+ex));return null}}return null}}]);return User}();exports.default=User;function urlBase64Decode(str){var output=str.replace(/-/g,"+").replace(/_/g,"/");switch(output.length%4){case 0:break;case 2:output+="==";break;case 3:output+="=";break;default:throw"Illegal base64url string!"}var result=window.atob(output);try{return decodeURIComponent(escape(result))}catch(err){return result}}},{"./admin":3,"micro-api-client":1}]},{},[4]); diff --git a/package.json b/package.json index 38a8f0f4..6ab4e53e 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "gotrue-js", "description": "GoTrue API client for JavaScript", - "version": "0.9.18", + "version": "0.9.19", "author": "Mathias Biilmann Christensen", "bugs": "https://github.com/netlify/micro-api-client-lib/issues", "dependencies": {