@ordergroove/auth
Advanced tools
Comparing version 2.1.0 to 2.1.1
@@ -1,2 +0,2 @@ | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.auth=t():(e.OG=e.OG||{},e.OG.auth=t())}(window,(function(){return function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=0)}([function(e,t,n){"use strict";n.r(t),n.d(t,"ogAuthRegExp",(function(){return r})),n.d(t,"readAuthCookie",(function(){return o})),n.d(t,"parseAuth",(function(){return u})),n.d(t,"iframeLoad",(function(){return i})),n.d(t,"isJsonResponse",(function(){return c})),n.d(t,"resolveAuth",(function(){return f}));const r=/^og_auth=/,o=(e=r)=>(document.cookie.split(/;\s*/).find(t=>t.match(e))||"").replace(r,""),u=e=>{if("object"==typeof e)return e;const t=(e||"").split("|");return 3===t.length?{sig_field:t[0],ts:parseInt(t[1],10),sig:t[2]}:null},i=e=>new Promise((t,n)=>{const r=document.createElement("iframe");r.style.setProperty("display","none","important"),document.body.appendChild(r),r.onload=t,r.onerror=n,r.src=e}),c=e=>-1!==(e.headers.get("content-type")||"").indexOf("application/json"),f=(e,t=o,n=i)=>Promise.resolve(t()||fetch(e).then(r=>r.status>=200&&r.status<300?t()||(c(r)?r.json():Promise.resolve(n(e)).then(t)):null)).then(u).then(e=>null===e?Promise.reject(new Error("Unauthorized")):e);t.default=f}])})); | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.auth=t():(e.OG=e.OG||{},e.OG.auth=t())}(window,(function(){return function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=0)}([function(e,t,n){"use strict";n.r(t),n.d(t,"ogAuthRegExp",(function(){return r})),n.d(t,"readAuthCookie",(function(){return o})),n.d(t,"parseAuth",(function(){return u})),n.d(t,"iframeLoad",(function(){return i})),n.d(t,"isJsonResponse",(function(){return f})),n.d(t,"resolveAuth",(function(){return c}));const r=/^og_auth=/,o=(e=r)=>(document.cookie.split(/;\s*/).find(t=>t.match(e))||"").replace(r,""),u=e=>{if("object"==typeof e)return e;const t=(e||"").split("|");return 3===t.length?{sig_field:t[0],ts:parseInt(t[1],10),sig:t[2]}:null},i=e=>new Promise((t,n)=>{const r=document.createElement("iframe");r.style.setProperty("display","none","important"),document.body.appendChild(r),r.onload=t,r.onerror=n,r.src=e}),f=e=>-1!==(e.headers.get("content-type")||"").indexOf("application/json"),c=(e,t=o,n=i)=>Promise.resolve(t()||e&&"string"==typeof e&&fetch(e).then(r=>r.status>=200&&r.status<300?t()||(f(r)?r.json():Promise.resolve(n(e)).then(t)):null)).then(u).then(e=>null===e?Promise.reject(new Error("Unauthorized")):e);t.default=c}])})); | ||
//# sourceMappingURL=auth.js.map |
{ | ||
"name": "@ordergroove/auth", | ||
"version": "2.1.0", | ||
"version": "2.1.1", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/auth.js", |
@@ -62,12 +62,14 @@ export const ogAuthRegExp = /^og_auth=/; | ||
_readAuthCookie() || | ||
fetch(authUrl).then(response => { | ||
// https://github.com/github/fetch/issues/386#issuecomment-243145797 | ||
// detect if cookie was written by latest request | ||
if (response.status >= 200 && response.status < 300) | ||
return ( | ||
_readAuthCookie() || | ||
(isJsonResponse(response) ? response.json() : Promise.resolve(_iframeLoad(authUrl)).then(_readAuthCookie)) | ||
); | ||
return null; | ||
}) | ||
(authUrl && | ||
typeof authUrl === 'string' && | ||
fetch(authUrl).then(response => { | ||
// https://github.com/github/fetch/issues/386#issuecomment-243145797 | ||
// detect if cookie was written by latest request | ||
if (response.status >= 200 && response.status < 300) | ||
return ( | ||
_readAuthCookie() || | ||
(isJsonResponse(response) ? response.json() : Promise.resolve(_iframeLoad(authUrl)).then(_readAuthCookie)) | ||
); | ||
return null; | ||
})) | ||
) | ||
@@ -74,0 +76,0 @@ .then(parseAuth) |
@@ -70,3 +70,3 @@ // import fetchMock from 'fetch-mock'; | ||
it('should call fetch if no cookie', async () => { | ||
it('should call fetch given no cookie and authUrl is non-empty string', async () => { | ||
const readOgAuthCookie = jasmine.createSpy('readOgAuthCookie'); | ||
@@ -77,2 +77,16 @@ resolveAuth('/auth', readOgAuthCookie); | ||
}); | ||
it('should not call fetch given no cookie and authUrl is a non-string', async () => { | ||
const readOgAuthCookie = jasmine.createSpy('readOgAuthCookie'); | ||
resolveAuth(undefined, readOgAuthCookie); | ||
expect(readOgAuthCookie).toHaveBeenCalled(); | ||
expect(fetchMock.calls().length).toEqual(0); | ||
}); | ||
it('should not call fetch given no cookie and authUrl is an empty string', async () => { | ||
const readOgAuthCookie = jasmine.createSpy('readOgAuthCookie'); | ||
resolveAuth('', readOgAuthCookie); | ||
expect(readOgAuthCookie).toHaveBeenCalled(); | ||
expect(fetchMock.calls().length).toEqual(0); | ||
}); | ||
}); | ||
@@ -79,0 +93,0 @@ |
Sorry, the diff of this file is not supported yet
25836
242