@tasoskakour/react-use-oauth2 2.0.0 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cjs/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";var e=require("react/jsx-runtime"),r=require("react"),n=function(){return n=Object.assign||function(e){for(var r,n=1,t=arguments.length;n<t;n++)for(var o in r=arguments[n])Object.prototype.hasOwnProperty.call(r,o)&&(e[o]=r[o]);return e},n.apply(this,arguments)};function t(e,r,n,t){return new(n||(n=Promise))((function(o,a){function u(e){try{c(t.next(e))}catch(e){a(e)}}function i(e){try{c(t.throw(e))}catch(e){a(e)}}function c(e){var r;e.done?o(e.value):(r=e.value,r instanceof n?r:new n((function(e){e(r)}))).then(u,i)}c((t=t.apply(e,r||[])).next())}))}function o(e,r){var n,t,o,a,u={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return a={next:i(0),throw:i(1),return:i(2)},"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a;function i(i){return function(c){return function(i){if(n)throw new TypeError("Generator is already executing.");for(;a&&(a=0,i[0]&&(u=0)),u;)try{if(n=1,t&&(o=2&i[0]?t.return:i[0]?t.throw||((o=t.return)&&o.call(t),0):t.next)&&!(o=o.call(t,i[1])).done)return o;switch(t=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return u.label++,{value:i[1],done:!1};case 5:u.label++,t=i[1],i=[0];continue;case 7:i=u.ops.pop(),u.trys.pop();continue;default:if(!(o=u.trys,(o=o.length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){u=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){u.label=i[1];break}if(6===i[0]&&u.label<o[1]){u.label=o[1],o=i;break}if(o&&u.label<o[2]){u.label=o[2],u.ops.push(i);break}o[2]&&u.ops.pop(),u.trys.pop();continue}i=r.call(e,u)}catch(e){i=[6,e],t=0}finally{n=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,c])}}}"function"==typeof SuppressedError&&SuppressedError;var a="react-use-oauth2-state-key",u="react-use-oauth2-response",i=["GET","POST","PUT","PATCH"],c=function(e){return new URLSearchParams(e).toString()},s=function(e){var r=new URLSearchParams(e);return Object.fromEntries(r.entries())},l=function(e,r){return e.postMessage(r)},d=function(e,r,n){clearInterval(e.current),r.current&&"function"==typeof r.current.close&&function(e){var r;null===(r=e.current)||void 0===r||r.close()}(r),sessionStorage.removeItem(a),window.removeEventListener("message",n)},f=function(e,r,t,o,a){var u=e.split("?")[0],i=s(e.split("?")[1]);return"".concat(u,"?").concat(c(n(n({},i),{client_id:r,grant_type:"authorization_code",code:t,redirect_uri:o,state:a})))},p=!1;const h=new Map;function v(e,n){if(void 0===r.useSyncExternalStore)throw new TypeError('You are using React 17 or below. Install with "npm install use-local-storage-state@17".');const[t]=r.useState(null==n?void 0:n.defaultValue);if("undefined"==typeof window)return[t,()=>{},{isPersistent:!0,removeItem:()=>{}}];const o=null==n?void 0:n.serializer;return function(e,n,t=!0,o=g,a=JSON.stringify){h.has(e)||void 0===n||null!==m((()=>localStorage.getItem(e)))||m((()=>localStorage.setItem(e,a(n))));const u=r.useRef({item:null,parsed:n}),i=r.useSyncExternalStore(r.useCallback((r=>{const n=n=>{e===n&&r()};return w.add(n),()=>{w.delete(n)}}),[e]),(()=>{var r;const t=null!==(r=m((()=>localStorage.getItem(e))))&&void 0!==r?r:null;if(h.has(e))u.current={item:t,parsed:h.get(e)};else if(t!==u.current.item){let e;try{e=null===t?n:o(t)}catch(r){e=n}u.current={item:t,parsed:e}}return u.current.parsed}),(()=>n)),c=r.useCallback((r=>{const n=r instanceof Function?r(u.current.parsed):r;try{localStorage.setItem(e,a(n)),h.delete(e)}catch(r){h.set(e,n)}y(e)}),[e,a]);return r.useEffect((()=>{if(!t)return;const r=r=>{r.storageArea===m((()=>localStorage))&&r.key===e&&y(e)};return window.addEventListener("storage",r),()=>window.removeEventListener("storage",r)}),[e,t]),r.useMemo((()=>[i,c,{isPersistent:i===n||!h.has(e),removeItem(){m((()=>localStorage.removeItem(e))),h.delete(e),y(e)}}]),[e,c,i,n])}(e,t,null==n?void 0:n.storageSync,null==o?void 0:o.parse,null==o?void 0:o.stringify)}const w=new Set;function y(e){for(const r of[...w])r(e)}function g(e){return"undefined"===e?void 0:JSON.parse(e)}function m(e){try{return e()}catch(e){return}}exports.OAuthPopup=function(t){var o=t.Component,i=void 0===o?e.jsx("div",{style:{margin:"12px"},"data-testid":"popup-loading",children:"Loading..."}):o;return r.useEffect((function(){if(!p){p=!0;var e=n(n({},s(window.location.search.split("?")[1])),s(window.location.hash.split("#")[1])),r=null==e?void 0:e.state,t=null==e?void 0:e.error,o=null===window||void 0===window?void 0:window.opener;if(!function(e){return null!=e}(o))throw new Error("No window opener");var i,c,d=r&&(i=o.sessionStorage,c=r,i.getItem(a)===c);if(!t&&d)l(o,{type:u,payload:e});else{var f=t?decodeURI(t):"".concat(d?"OAuth error: An error has occured.":"OAuth error: State mismatch.");l(o,{type:u,error:f})}}}),[]),i},exports.useOAuth2=function(e){var s=e.authorizeUrl,l=e.clientId,p=e.redirectUri,h=e.scope,w=void 0===h?"":h,y=e.responseType,g=e.extraQueryParameters,m=void 0===g?{}:g,b=e.onSuccess,S=e.onError;!function(e){var r=e.authorizeUrl,n=e.clientId,t=e.redirectUri,o=e.responseType,a=e.extraQueryParameters,u=void 0===a?{}:a,c=e.onSuccess,s=e.onError;if(!(r&&n&&t&&o))throw new Error("Missing required props for useOAuth2. Required props are: {authorizeUrl, clientId, redirectUri, responseType}");if("code"===o&&!e.exchangeCodeForTokenQuery&&!e.exchangeCodeForTokenQueryFn)throw new Error('Either `exchangeCodeForTokenQuery` or `exchangeCodeForTokenQueryFn` is required for responseType of "code" for useOAuth2.');if("code"===o&&e.exchangeCodeForTokenQuery&&!e.exchangeCodeForTokenQuery.url)throw new Error("Value `exchangeCodeForTokenQuery.url` is missing.");if("code"===o&&e.exchangeCodeForTokenQuery&&!["GET","POST","PUT","PATCH"].includes(e.exchangeCodeForTokenQuery.method))throw new Error("Invalid `exchangeCodeForTokenQuery.method` value. It can be one of ".concat(i.join(", "),"."));if("object"!=typeof u)throw new TypeError("extraQueryParameters must be an object for useOAuth2.");if(c&&"function"!=typeof c)throw new TypeError("onSuccess callback must be a function for useOAuth2.");if(s&&"function"!=typeof s)throw new TypeError("onError callback must be a function for useOAuth2.")}(e);var T=r.useRef(m),E=r.useRef(),x=r.useRef(),k=r.useRef("code"===y&&e.exchangeCodeForTokenQuery),C=r.useRef("code"===y&&e.exchangeCodeForTokenQueryFn),P=r.useState({loading:!1,error:null}),I=P[0],O=I.loading,F=I.error,A=P[1],Q=v("".concat(y,"-").concat(s,"-").concat(l,"-").concat(w),{defaultValue:null}),U=Q[0],R=Q[1],j=Q[2],L=j.removeItem,_=j.isPersistent,q=r.useCallback((function(){A({loading:!0,error:null});var e,r,i,h,v,g=(e="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",r=new Uint8Array(40),window.crypto.getRandomValues(r),r=r.map((function(r){return e.codePointAt(r%62)})),String.fromCharCode.apply(null,r));function m(e){var r,n,a,i,c;return t(this,void 0,void 0,(function(){var t,s,h,v,w;return o(this,(function(o){switch(o.label){case 0:if((null===(r=null==e?void 0:e.data)||void 0===r?void 0:r.type)!==u)return[2];o.label=1;case 1:return o.trys.push([1,13,14,15]),"error"in e.data?(t=(null===(n=e.data)||void 0===n?void 0:n.error)||"Unknown Error occured.",A({loading:!1,error:t}),S?[4,S(t)]:[3,3]):[3,4];case 2:o.sent(),o.label=3;case 3:return[3,12];case 4:return s=null===(a=null==e?void 0:e.data)||void 0===a?void 0:a.payload,"code"!==y?[3,10]:(h=C.current,v=k.current,h&&"function"==typeof h?[4,h(null===(i=e.data)||void 0===i?void 0:i.payload)]:[3,6]);case 5:return s=o.sent(),[3,10];case 6:return v?[4,fetch(f(v.url,l,null==s?void 0:s.code,p,g),{method:null!==(c=v.method)&&void 0!==c?c:"POST",headers:v.headers||{}})]:[3,9];case 7:return[4,o.sent().json()];case 8:return s=o.sent(),[3,10];case 9:throw new Error("useOAuth2: You must provide `exchangeCodeForTokenQuery` or `exchangeCodeForTokenQueryFn`");case 10:return A({loading:!1,error:null}),R(s),b?[4,b(s)]:[3,12];case 11:o.sent(),o.label=12;case 12:return[3,15];case 13:return w=o.sent(),console.error(w),A({loading:!1,error:w.toString()}),[3,15];case 14:return d(x,E,m),[7];case 15:return[2]}}))}))}return function(e,r){e.setItem(a,r)}(sessionStorage,g),E.current=(i=function(e,r,t,o,a,u,i){void 0===i&&(i={});var s=c(n({response_type:u,client_id:r,redirect_uri:t,scope:o,state:a},i));return"".concat(e,"?").concat(s)}(s,l,p,w,g,y,T.current),h=window.outerHeight/2+window.screenY-350,v=window.outerWidth/2+window.screenX-300,window.open(i,"OAuth2 Popup","height=".concat(700,",width=").concat(600,",top=").concat(h,",left=").concat(v))),window.addEventListener("message",m),x.current=setInterval((function(){var e,r,t;(!(null===(e=E.current)||void 0===e?void 0:e.window)||(null===(t=null===(r=E.current)||void 0===r?void 0:r.window)||void 0===t?void 0:t.closed))&&(A((function(e){return n(n({},e),{loading:!1})})),console.warn("Warning: Popup was closed before completing authentication."),d(x,E,m))}),250),function(){window.removeEventListener("message",m),x.current&&clearInterval(x.current)}}),[s,l,p,w,y,b,S,A,R]);return{data:U,loading:O,error:F,getAuth:q,logout:r.useCallback((function(){L(),A({loading:!1,error:null})}),[L]),isPersistent:_}};
1
+ "use strict";var e=require("react/jsx-runtime"),r=require("react"),n=function(){return n=Object.assign||function(e){for(var r,n=1,t=arguments.length;n<t;n++)for(var o in r=arguments[n])Object.prototype.hasOwnProperty.call(r,o)&&(e[o]=r[o]);return e},n.apply(this,arguments)};function t(e,r,n,t){return new(n||(n=Promise))((function(o,a){function u(e){try{c(t.next(e))}catch(e){a(e)}}function i(e){try{c(t.throw(e))}catch(e){a(e)}}function c(e){var r;e.done?o(e.value):(r=e.value,r instanceof n?r:new n((function(e){e(r)}))).then(u,i)}c((t=t.apply(e,r||[])).next())}))}function o(e,r){var n,t,o,a,u={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return a={next:i(0),throw:i(1),return:i(2)},"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a;function i(i){return function(c){return function(i){if(n)throw new TypeError("Generator is already executing.");for(;a&&(a=0,i[0]&&(u=0)),u;)try{if(n=1,t&&(o=2&i[0]?t.return:i[0]?t.throw||((o=t.return)&&o.call(t),0):t.next)&&!(o=o.call(t,i[1])).done)return o;switch(t=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return u.label++,{value:i[1],done:!1};case 5:u.label++,t=i[1],i=[0];continue;case 7:i=u.ops.pop(),u.trys.pop();continue;default:if(!(o=u.trys,(o=o.length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){u=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){u.label=i[1];break}if(6===i[0]&&u.label<o[1]){u.label=o[1],o=i;break}if(o&&u.label<o[2]){u.label=o[2],u.ops.push(i);break}o[2]&&u.ops.pop(),u.trys.pop();continue}i=r.call(e,u)}catch(e){i=[6,e],t=0}finally{n=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,c])}}}"function"==typeof SuppressedError&&SuppressedError;var a="react-use-oauth2-state-key",u="react-use-oauth2-response",i=["GET","POST","PUT","PATCH"],c=function(e){return new URLSearchParams(e).toString()},s=function(e){var r=new URLSearchParams(e);return Object.fromEntries(r.entries())},l=function(e,r){return e.postMessage(r)},d=function(e,r,n){clearInterval(e.current),r.current&&"function"==typeof r.current.close&&function(e){var r;null===(r=e.current)||void 0===r||r.close()}(r),sessionStorage.removeItem(a),window.removeEventListener("message",n)},f=function(e,r,t,o,a){var u=e.split("?")[0],i=s(e.split("?")[1]);return"".concat(u,"?").concat(c(n(n({},i),{client_id:r,grant_type:"authorization_code",code:t,redirect_uri:o,state:a})))},p=!1;const h=new Map;function v(e,n){if(void 0===r.useSyncExternalStore)throw new TypeError('You are using React 17 or below. Install with "npm install use-local-storage-state@17".');const[t]=r.useState(null==n?void 0:n.defaultValue);if("undefined"==typeof window)return[t,()=>{},{isPersistent:!0,removeItem:()=>{}}];const o=null==n?void 0:n.serializer;return function(e,n,t=!0,o=g,a=JSON.stringify){h.has(e)||void 0===n||null!==m((()=>localStorage.getItem(e)))||m((()=>localStorage.setItem(e,a(n))));const u=r.useRef({item:null,parsed:n}),i=r.useSyncExternalStore(r.useCallback((r=>{const n=n=>{e===n&&r()};return w.add(n),()=>{w.delete(n)}}),[e]),(()=>{var r;const t=null!==(r=m((()=>localStorage.getItem(e))))&&void 0!==r?r:null;if(h.has(e))u.current={item:t,parsed:h.get(e)};else if(t!==u.current.item){let e;try{e=null===t?n:o(t)}catch(r){e=n}u.current={item:t,parsed:e}}return u.current.parsed}),(()=>n)),c=r.useCallback((r=>{const n=r instanceof Function?r(u.current.parsed):r;try{localStorage.setItem(e,a(n)),h.delete(e)}catch(r){h.set(e,n)}y(e)}),[e,a]);return r.useEffect((()=>{if(!t)return;const r=r=>{r.storageArea===m((()=>localStorage))&&r.key===e&&y(e)};return window.addEventListener("storage",r),()=>window.removeEventListener("storage",r)}),[e,t]),r.useMemo((()=>[i,c,{isPersistent:i===n||!h.has(e),removeItem(){m((()=>localStorage.removeItem(e))),h.delete(e),y(e)}}]),[e,c,i,n])}(e,t,null==n?void 0:n.storageSync,null==o?void 0:o.parse,null==o?void 0:o.stringify)}const w=new Set;function y(e){for(const r of[...w])r(e)}function g(e){return"undefined"===e?void 0:JSON.parse(e)}function m(e){try{return e()}catch(e){return}}exports.OAuthPopup=function(t){var o=t.Component,i=void 0===o?e.jsx("div",{style:{margin:"12px"},"data-testid":"popup-loading",children:"Loading..."}):o;return r.useEffect((function(){if(!p){p=!0;var e=n(n({},s(window.location.search.split("?")[1])),s(window.location.hash.split("#")[1])),r=null==e?void 0:e.state,t=null==e?void 0:e.error,o=null===window||void 0===window?void 0:window.opener;if(!function(e){return null!=e}(o))throw new Error("No window opener");var i,c,d=r&&(i=o.sessionStorage,c=r,i.getItem(a)===c);if(!t&&d)l(o,{type:u,payload:e});else{var f=t?decodeURI(t):"".concat(d?"OAuth error: An error has occured.":"OAuth error: State mismatch.");l(o,{type:u,error:f})}}}),[]),i},exports.useOAuth2=function(e){var s=e.authorizeUrl,l=e.clientId,p=e.redirectUri,h=e.scope,w=void 0===h?"":h,y=e.responseType,g=e.extraQueryParameters,m=void 0===g?{}:g,b=e.onSuccess,S=e.onError;!function(e){var r=e.authorizeUrl,n=e.clientId,t=e.redirectUri,o=e.responseType,a=e.extraQueryParameters,u=void 0===a?{}:a,c=e.onSuccess,s=e.onError;if(!(r&&n&&t&&o))throw new Error("Missing required props for useOAuth2. Required props are: {authorizeUrl, clientId, redirectUri, responseType}");if("code"===o&&!e.exchangeCodeForTokenQuery&&!e.exchangeCodeForTokenQueryFn)throw new Error('Either `exchangeCodeForTokenQuery` or `exchangeCodeForTokenQueryFn` is required for responseType of "code" for useOAuth2.');if("code"===o&&e.exchangeCodeForTokenQuery&&!e.exchangeCodeForTokenQuery.url)throw new Error("Value `exchangeCodeForTokenQuery.url` is missing.");if("code"===o&&e.exchangeCodeForTokenQuery&&!["GET","POST","PUT","PATCH"].includes(e.exchangeCodeForTokenQuery.method))throw new Error("Invalid `exchangeCodeForTokenQuery.method` value. It can be one of ".concat(i.join(", "),"."));if("object"!=typeof u)throw new TypeError("extraQueryParameters must be an object for useOAuth2.");if(c&&"function"!=typeof c)throw new TypeError("onSuccess callback must be a function for useOAuth2.");if(s&&"function"!=typeof s)throw new TypeError("onError callback must be a function for useOAuth2.")}(e);var T=r.useRef(m),E=r.useRef(),x=r.useRef(),k=r.useRef("code"===y&&e.exchangeCodeForTokenQuery),C=r.useRef("code"===y&&e.exchangeCodeForTokenQueryFn),P=r.useState({loading:!1,error:null}),I=P[0],O=I.loading,F=I.error,A=P[1],Q=v("".concat(y,"-").concat(s,"-").concat(l,"-").concat(w),{defaultValue:null}),U=Q[0],R=Q[1],j=Q[2],L=j.removeItem,_=j.isPersistent,q=r.useCallback((function(){A({loading:!0,error:null});var e,r,i,h,v,g=(e="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",r=new Uint8Array(40),window.crypto.getRandomValues(r),r=r.map((function(r){return e.codePointAt(r%62)})),String.fromCharCode.apply(null,r));function m(e){var r,n,a,i,c;return t(this,void 0,void 0,(function(){var t,s,h,v,w;return o(this,(function(o){switch(o.label){case 0:if((null===(r=null==e?void 0:e.data)||void 0===r?void 0:r.type)!==u)return[2];o.label=1;case 1:return o.trys.push([1,13,16,17]),"error"in e.data?(t=(null===(n=e.data)||void 0===n?void 0:n.error)||"Unknown Error occured.",A({loading:!1,error:t}),S?[4,S(t)]:[3,3]):[3,4];case 2:o.sent(),o.label=3;case 3:return[3,12];case 4:return s=null===(a=null==e?void 0:e.data)||void 0===a?void 0:a.payload,"code"!==y?[3,10]:(h=C.current,v=k.current,h&&"function"==typeof h?[4,h(null===(i=e.data)||void 0===i?void 0:i.payload)]:[3,6]);case 5:return s=o.sent(),[3,10];case 6:return v?[4,fetch(f(v.url,l,null==s?void 0:s.code,p,g),{method:null!==(c=v.method)&&void 0!==c?c:"POST",headers:v.headers||{}})]:[3,9];case 7:return[4,o.sent().json()];case 8:return s=o.sent(),[3,10];case 9:throw new Error("useOAuth2: You must provide `exchangeCodeForTokenQuery` or `exchangeCodeForTokenQueryFn`");case 10:return A({loading:!1,error:null}),R(s),b?[4,b(s)]:[3,12];case 11:o.sent(),o.label=12;case 12:return[3,17];case 13:return w=o.sent(),console.error(w),A({loading:!1,error:w.toString()}),S?[4,S(w.toString())]:[3,15];case 14:o.sent(),o.label=15;case 15:return[3,17];case 16:return d(x,E,m),[7];case 17:return[2]}}))}))}return function(e,r){e.setItem(a,r)}(sessionStorage,g),E.current=(i=function(e,r,t,o,a,u,i){void 0===i&&(i={});var s=c(n({response_type:u,client_id:r,redirect_uri:t,scope:o,state:a},i));return"".concat(e,"?").concat(s)}(s,l,p,w,g,y,T.current),h=window.outerHeight/2+window.screenY-350,v=window.outerWidth/2+window.screenX-300,window.open(i,"OAuth2 Popup","height=".concat(700,",width=").concat(600,",top=").concat(h,",left=").concat(v))),window.addEventListener("message",m),x.current=setInterval((function(){var e,r,t;(!(null===(e=E.current)||void 0===e?void 0:e.window)||(null===(t=null===(r=E.current)||void 0===r?void 0:r.window)||void 0===t?void 0:t.closed))&&(A((function(e){return n(n({},e),{loading:!1})})),console.warn("Warning: Popup was closed before completing authentication."),d(x,E,m))}),250),function(){window.removeEventListener("message",m),x.current&&clearInterval(x.current)}}),[s,l,p,w,y,b,S,A,R]);return{data:U,loading:O,error:F,getAuth:q,logout:r.useCallback((function(){L(),A({loading:!1,error:null})}),[L]),isPersistent:_}};
package/dist/esm/index.js CHANGED
@@ -1 +1 @@
1
- import{jsx as e}from"react/jsx-runtime";import{useEffect as r,useSyncExternalStore as n,useState as o,useRef as t,useCallback as a,useMemo as i}from"react";var c=function(){return c=Object.assign||function(e){for(var r,n=1,o=arguments.length;n<o;n++)for(var t in r=arguments[n])Object.prototype.hasOwnProperty.call(r,t)&&(e[t]=r[t]);return e},c.apply(this,arguments)};function u(e,r,n,o){return new(n||(n=Promise))((function(t,a){function i(e){try{u(o.next(e))}catch(e){a(e)}}function c(e){try{u(o.throw(e))}catch(e){a(e)}}function u(e){var r;e.done?t(e.value):(r=e.value,r instanceof n?r:new n((function(e){e(r)}))).then(i,c)}u((o=o.apply(e,r||[])).next())}))}function s(e,r){var n,o,t,a,i={label:0,sent:function(){if(1&t[0])throw t[1];return t[1]},trys:[],ops:[]};return a={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a;function c(c){return function(u){return function(c){if(n)throw new TypeError("Generator is already executing.");for(;a&&(a=0,c[0]&&(i=0)),i;)try{if(n=1,o&&(t=2&c[0]?o.return:c[0]?o.throw||((t=o.return)&&t.call(o),0):o.next)&&!(t=t.call(o,c[1])).done)return t;switch(o=0,t&&(c=[2&c[0],t.value]),c[0]){case 0:case 1:t=c;break;case 4:return i.label++,{value:c[1],done:!1};case 5:i.label++,o=c[1],c=[0];continue;case 7:c=i.ops.pop(),i.trys.pop();continue;default:if(!(t=i.trys,(t=t.length>0&&t[t.length-1])||6!==c[0]&&2!==c[0])){i=0;continue}if(3===c[0]&&(!t||c[1]>t[0]&&c[1]<t[3])){i.label=c[1];break}if(6===c[0]&&i.label<t[1]){i.label=t[1],t=c;break}if(t&&i.label<t[2]){i.label=t[2],i.ops.push(c);break}t[2]&&i.ops.pop(),i.trys.pop();continue}c=r.call(e,i)}catch(e){c=[6,e],o=0}finally{n=t=0}if(5&c[0])throw c[1];return{value:c[0]?c[1]:void 0,done:!0}}([c,u])}}}"function"==typeof SuppressedError&&SuppressedError;var l="react-use-oauth2-state-key",d="react-use-oauth2-response",f=["GET","POST","PUT","PATCH"],p=function(e){return new URLSearchParams(e).toString()},h=function(e){var r=new URLSearchParams(e);return Object.fromEntries(r.entries())},v=function(e,r){return e.postMessage(r)},w=function(e,r,n){clearInterval(e.current),r.current&&"function"==typeof r.current.close&&function(e){var r;null===(r=e.current)||void 0===r||r.close()}(r),sessionStorage.removeItem(l),window.removeEventListener("message",n)},y=function(e,r,n,o,t){var a=e.split("?")[0],i=h(e.split("?")[1]);return"".concat(a,"?").concat(p(c(c({},i),{client_id:r,grant_type:"authorization_code",code:n,redirect_uri:o,state:t})))},g=!1,m=function(n){var o=n.Component,t=void 0===o?e("div",{style:{margin:"12px"},"data-testid":"popup-loading",children:"Loading..."}):o;return r((function(){if(!g){g=!0;var e=c(c({},h(window.location.search.split("?")[1])),h(window.location.hash.split("#")[1])),r=null==e?void 0:e.state,n=null==e?void 0:e.error,o=null===window||void 0===window?void 0:window.opener;if(!function(e){return null!=e}(o))throw new Error("No window opener");var t,a,i=r&&(t=o.sessionStorage,a=r,t.getItem(l)===a);if(!n&&i)v(o,{type:d,payload:e});else{var u=n?decodeURI(n):"".concat(i?"OAuth error: An error has occured.":"OAuth error: State mismatch.");v(o,{type:d,error:u})}}}),[]),t};const T=new Map;function b(e,c){if(void 0===n)throw new TypeError('You are using React 17 or below. Install with "npm install use-local-storage-state@17".');const[u]=o(null==c?void 0:c.defaultValue);if("undefined"==typeof window)return[u,()=>{},{isPersistent:!0,removeItem:()=>{}}];const s=null==c?void 0:c.serializer;return function(e,o,c=!0,u=x,s=JSON.stringify){T.has(e)||void 0===o||null!==k((()=>localStorage.getItem(e)))||k((()=>localStorage.setItem(e,s(o))));const l=t({item:null,parsed:o}),d=n(a((r=>{const n=n=>{e===n&&r()};return S.add(n),()=>{S.delete(n)}}),[e]),(()=>{var r;const n=null!==(r=k((()=>localStorage.getItem(e))))&&void 0!==r?r:null;if(T.has(e))l.current={item:n,parsed:T.get(e)};else if(n!==l.current.item){let e;try{e=null===n?o:u(n)}catch(r){e=o}l.current={item:n,parsed:e}}return l.current.parsed}),(()=>o)),f=a((r=>{const n=r instanceof Function?r(l.current.parsed):r;try{localStorage.setItem(e,s(n)),T.delete(e)}catch(r){T.set(e,n)}E(e)}),[e,s]);return r((()=>{if(!c)return;const r=r=>{r.storageArea===k((()=>localStorage))&&r.key===e&&E(e)};return window.addEventListener("storage",r),()=>window.removeEventListener("storage",r)}),[e,c]),i((()=>[d,f,{isPersistent:d===o||!T.has(e),removeItem(){k((()=>localStorage.removeItem(e))),T.delete(e),E(e)}}]),[e,f,d,o])}(e,u,null==c?void 0:c.storageSync,null==s?void 0:s.parse,null==s?void 0:s.stringify)}const S=new Set;function E(e){for(const r of[...S])r(e)}function x(e){return"undefined"===e?void 0:JSON.parse(e)}function k(e){try{return e()}catch(e){return}}var I=function(e){var r=e.authorizeUrl,n=e.clientId,i=e.redirectUri,h=e.scope,v=void 0===h?"":h,g=e.responseType,m=e.extraQueryParameters,T=void 0===m?{}:m,S=e.onSuccess,E=e.onError;!function(e){var r=e.authorizeUrl,n=e.clientId,o=e.redirectUri,t=e.responseType,a=e.extraQueryParameters,i=void 0===a?{}:a,c=e.onSuccess,u=e.onError;if(!(r&&n&&o&&t))throw new Error("Missing required props for useOAuth2. Required props are: {authorizeUrl, clientId, redirectUri, responseType}");if("code"===t&&!e.exchangeCodeForTokenQuery&&!e.exchangeCodeForTokenQueryFn)throw new Error('Either `exchangeCodeForTokenQuery` or `exchangeCodeForTokenQueryFn` is required for responseType of "code" for useOAuth2.');if("code"===t&&e.exchangeCodeForTokenQuery&&!e.exchangeCodeForTokenQuery.url)throw new Error("Value `exchangeCodeForTokenQuery.url` is missing.");if("code"===t&&e.exchangeCodeForTokenQuery&&!["GET","POST","PUT","PATCH"].includes(e.exchangeCodeForTokenQuery.method))throw new Error("Invalid `exchangeCodeForTokenQuery.method` value. It can be one of ".concat(f.join(", "),"."));if("object"!=typeof i)throw new TypeError("extraQueryParameters must be an object for useOAuth2.");if(c&&"function"!=typeof c)throw new TypeError("onSuccess callback must be a function for useOAuth2.");if(u&&"function"!=typeof u)throw new TypeError("onError callback must be a function for useOAuth2.")}(e);var x=t(T),k=t(),I=t(),P=t("code"===g&&e.exchangeCodeForTokenQuery),C=t("code"===g&&e.exchangeCodeForTokenQueryFn),F=o({loading:!1,error:null}),O=F[0],Q=O.loading,A=O.error,U=F[1],j=b("".concat(g,"-").concat(r,"-").concat(n,"-").concat(v),{defaultValue:null}),L=j[0],R=j[1],_=j[2],z=_.removeItem,V=_.isPersistent,q=a((function(){U({loading:!0,error:null});var e,o,t,a,f,h=(e="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",o=new Uint8Array(40),window.crypto.getRandomValues(o),o=o.map((function(r){return e.codePointAt(r%62)})),String.fromCharCode.apply(null,o));function m(e){var r,o,t,a,c;return u(this,void 0,void 0,(function(){var u,l,f,p,v;return s(this,(function(s){switch(s.label){case 0:if((null===(r=null==e?void 0:e.data)||void 0===r?void 0:r.type)!==d)return[2];s.label=1;case 1:return s.trys.push([1,13,14,15]),"error"in e.data?(u=(null===(o=e.data)||void 0===o?void 0:o.error)||"Unknown Error occured.",U({loading:!1,error:u}),E?[4,E(u)]:[3,3]):[3,4];case 2:s.sent(),s.label=3;case 3:return[3,12];case 4:return l=null===(t=null==e?void 0:e.data)||void 0===t?void 0:t.payload,"code"!==g?[3,10]:(f=C.current,p=P.current,f&&"function"==typeof f?[4,f(null===(a=e.data)||void 0===a?void 0:a.payload)]:[3,6]);case 5:return l=s.sent(),[3,10];case 6:return p?[4,fetch(y(p.url,n,null==l?void 0:l.code,i,h),{method:null!==(c=p.method)&&void 0!==c?c:"POST",headers:p.headers||{}})]:[3,9];case 7:return[4,s.sent().json()];case 8:return l=s.sent(),[3,10];case 9:throw new Error("useOAuth2: You must provide `exchangeCodeForTokenQuery` or `exchangeCodeForTokenQueryFn`");case 10:return U({loading:!1,error:null}),R(l),S?[4,S(l)]:[3,12];case 11:s.sent(),s.label=12;case 12:return[3,15];case 13:return v=s.sent(),console.error(v),U({loading:!1,error:v.toString()}),[3,15];case 14:return w(I,k,m),[7];case 15:return[2]}}))}))}return function(e,r){e.setItem(l,r)}(sessionStorage,h),k.current=(t=function(e,r,n,o,t,a,i){void 0===i&&(i={});var u=p(c({response_type:a,client_id:r,redirect_uri:n,scope:o,state:t},i));return"".concat(e,"?").concat(u)}(r,n,i,v,h,g,x.current),a=window.outerHeight/2+window.screenY-350,f=window.outerWidth/2+window.screenX-300,window.open(t,"OAuth2 Popup","height=".concat(700,",width=").concat(600,",top=").concat(a,",left=").concat(f))),window.addEventListener("message",m),I.current=setInterval((function(){var e,r,n;(!(null===(e=k.current)||void 0===e?void 0:e.window)||(null===(n=null===(r=k.current)||void 0===r?void 0:r.window)||void 0===n?void 0:n.closed))&&(U((function(e){return c(c({},e),{loading:!1})})),console.warn("Warning: Popup was closed before completing authentication."),w(I,k,m))}),250),function(){window.removeEventListener("message",m),I.current&&clearInterval(I.current)}}),[r,n,i,v,g,S,E,U,R]);return{data:L,loading:Q,error:A,getAuth:q,logout:a((function(){z(),U({loading:!1,error:null})}),[z]),isPersistent:V}};export{m as OAuthPopup,I as useOAuth2};
1
+ import{jsx as e}from"react/jsx-runtime";import{useEffect as r,useSyncExternalStore as n,useState as o,useRef as t,useCallback as a,useMemo as i}from"react";var c=function(){return c=Object.assign||function(e){for(var r,n=1,o=arguments.length;n<o;n++)for(var t in r=arguments[n])Object.prototype.hasOwnProperty.call(r,t)&&(e[t]=r[t]);return e},c.apply(this,arguments)};function u(e,r,n,o){return new(n||(n=Promise))((function(t,a){function i(e){try{u(o.next(e))}catch(e){a(e)}}function c(e){try{u(o.throw(e))}catch(e){a(e)}}function u(e){var r;e.done?t(e.value):(r=e.value,r instanceof n?r:new n((function(e){e(r)}))).then(i,c)}u((o=o.apply(e,r||[])).next())}))}function s(e,r){var n,o,t,a,i={label:0,sent:function(){if(1&t[0])throw t[1];return t[1]},trys:[],ops:[]};return a={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a;function c(c){return function(u){return function(c){if(n)throw new TypeError("Generator is already executing.");for(;a&&(a=0,c[0]&&(i=0)),i;)try{if(n=1,o&&(t=2&c[0]?o.return:c[0]?o.throw||((t=o.return)&&t.call(o),0):o.next)&&!(t=t.call(o,c[1])).done)return t;switch(o=0,t&&(c=[2&c[0],t.value]),c[0]){case 0:case 1:t=c;break;case 4:return i.label++,{value:c[1],done:!1};case 5:i.label++,o=c[1],c=[0];continue;case 7:c=i.ops.pop(),i.trys.pop();continue;default:if(!(t=i.trys,(t=t.length>0&&t[t.length-1])||6!==c[0]&&2!==c[0])){i=0;continue}if(3===c[0]&&(!t||c[1]>t[0]&&c[1]<t[3])){i.label=c[1];break}if(6===c[0]&&i.label<t[1]){i.label=t[1],t=c;break}if(t&&i.label<t[2]){i.label=t[2],i.ops.push(c);break}t[2]&&i.ops.pop(),i.trys.pop();continue}c=r.call(e,i)}catch(e){c=[6,e],o=0}finally{n=t=0}if(5&c[0])throw c[1];return{value:c[0]?c[1]:void 0,done:!0}}([c,u])}}}"function"==typeof SuppressedError&&SuppressedError;var l="react-use-oauth2-state-key",d="react-use-oauth2-response",f=["GET","POST","PUT","PATCH"],p=function(e){return new URLSearchParams(e).toString()},h=function(e){var r=new URLSearchParams(e);return Object.fromEntries(r.entries())},v=function(e,r){return e.postMessage(r)},w=function(e,r,n){clearInterval(e.current),r.current&&"function"==typeof r.current.close&&function(e){var r;null===(r=e.current)||void 0===r||r.close()}(r),sessionStorage.removeItem(l),window.removeEventListener("message",n)},g=function(e,r,n,o,t){var a=e.split("?")[0],i=h(e.split("?")[1]);return"".concat(a,"?").concat(p(c(c({},i),{client_id:r,grant_type:"authorization_code",code:n,redirect_uri:o,state:t})))},y=!1,m=function(n){var o=n.Component,t=void 0===o?e("div",{style:{margin:"12px"},"data-testid":"popup-loading",children:"Loading..."}):o;return r((function(){if(!y){y=!0;var e=c(c({},h(window.location.search.split("?")[1])),h(window.location.hash.split("#")[1])),r=null==e?void 0:e.state,n=null==e?void 0:e.error,o=null===window||void 0===window?void 0:window.opener;if(!function(e){return null!=e}(o))throw new Error("No window opener");var t,a,i=r&&(t=o.sessionStorage,a=r,t.getItem(l)===a);if(!n&&i)v(o,{type:d,payload:e});else{var u=n?decodeURI(n):"".concat(i?"OAuth error: An error has occured.":"OAuth error: State mismatch.");v(o,{type:d,error:u})}}}),[]),t};const b=new Map;function T(e,c){if(void 0===n)throw new TypeError('You are using React 17 or below. Install with "npm install use-local-storage-state@17".');const[u]=o(null==c?void 0:c.defaultValue);if("undefined"==typeof window)return[u,()=>{},{isPersistent:!0,removeItem:()=>{}}];const s=null==c?void 0:c.serializer;return function(e,o,c=!0,u=x,s=JSON.stringify){b.has(e)||void 0===o||null!==k((()=>localStorage.getItem(e)))||k((()=>localStorage.setItem(e,s(o))));const l=t({item:null,parsed:o}),d=n(a((r=>{const n=n=>{e===n&&r()};return S.add(n),()=>{S.delete(n)}}),[e]),(()=>{var r;const n=null!==(r=k((()=>localStorage.getItem(e))))&&void 0!==r?r:null;if(b.has(e))l.current={item:n,parsed:b.get(e)};else if(n!==l.current.item){let e;try{e=null===n?o:u(n)}catch(r){e=o}l.current={item:n,parsed:e}}return l.current.parsed}),(()=>o)),f=a((r=>{const n=r instanceof Function?r(l.current.parsed):r;try{localStorage.setItem(e,s(n)),b.delete(e)}catch(r){b.set(e,n)}E(e)}),[e,s]);return r((()=>{if(!c)return;const r=r=>{r.storageArea===k((()=>localStorage))&&r.key===e&&E(e)};return window.addEventListener("storage",r),()=>window.removeEventListener("storage",r)}),[e,c]),i((()=>[d,f,{isPersistent:d===o||!b.has(e),removeItem(){k((()=>localStorage.removeItem(e))),b.delete(e),E(e)}}]),[e,f,d,o])}(e,u,null==c?void 0:c.storageSync,null==s?void 0:s.parse,null==s?void 0:s.stringify)}const S=new Set;function E(e){for(const r of[...S])r(e)}function x(e){return"undefined"===e?void 0:JSON.parse(e)}function k(e){try{return e()}catch(e){return}}var I=function(e){var r=e.authorizeUrl,n=e.clientId,i=e.redirectUri,h=e.scope,v=void 0===h?"":h,y=e.responseType,m=e.extraQueryParameters,b=void 0===m?{}:m,S=e.onSuccess,E=e.onError;!function(e){var r=e.authorizeUrl,n=e.clientId,o=e.redirectUri,t=e.responseType,a=e.extraQueryParameters,i=void 0===a?{}:a,c=e.onSuccess,u=e.onError;if(!(r&&n&&o&&t))throw new Error("Missing required props for useOAuth2. Required props are: {authorizeUrl, clientId, redirectUri, responseType}");if("code"===t&&!e.exchangeCodeForTokenQuery&&!e.exchangeCodeForTokenQueryFn)throw new Error('Either `exchangeCodeForTokenQuery` or `exchangeCodeForTokenQueryFn` is required for responseType of "code" for useOAuth2.');if("code"===t&&e.exchangeCodeForTokenQuery&&!e.exchangeCodeForTokenQuery.url)throw new Error("Value `exchangeCodeForTokenQuery.url` is missing.");if("code"===t&&e.exchangeCodeForTokenQuery&&!["GET","POST","PUT","PATCH"].includes(e.exchangeCodeForTokenQuery.method))throw new Error("Invalid `exchangeCodeForTokenQuery.method` value. It can be one of ".concat(f.join(", "),"."));if("object"!=typeof i)throw new TypeError("extraQueryParameters must be an object for useOAuth2.");if(c&&"function"!=typeof c)throw new TypeError("onSuccess callback must be a function for useOAuth2.");if(u&&"function"!=typeof u)throw new TypeError("onError callback must be a function for useOAuth2.")}(e);var x=t(b),k=t(),I=t(),P=t("code"===y&&e.exchangeCodeForTokenQuery),C=t("code"===y&&e.exchangeCodeForTokenQueryFn),F=o({loading:!1,error:null}),O=F[0],Q=O.loading,A=O.error,U=F[1],j=T("".concat(y,"-").concat(r,"-").concat(n,"-").concat(v),{defaultValue:null}),L=j[0],R=j[1],_=j[2],z=_.removeItem,V=_.isPersistent,q=a((function(){U({loading:!0,error:null});var e,o,t,a,f,h=(e="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",o=new Uint8Array(40),window.crypto.getRandomValues(o),o=o.map((function(r){return e.codePointAt(r%62)})),String.fromCharCode.apply(null,o));function m(e){var r,o,t,a,c;return u(this,void 0,void 0,(function(){var u,l,f,p,v;return s(this,(function(s){switch(s.label){case 0:if((null===(r=null==e?void 0:e.data)||void 0===r?void 0:r.type)!==d)return[2];s.label=1;case 1:return s.trys.push([1,13,16,17]),"error"in e.data?(u=(null===(o=e.data)||void 0===o?void 0:o.error)||"Unknown Error occured.",U({loading:!1,error:u}),E?[4,E(u)]:[3,3]):[3,4];case 2:s.sent(),s.label=3;case 3:return[3,12];case 4:return l=null===(t=null==e?void 0:e.data)||void 0===t?void 0:t.payload,"code"!==y?[3,10]:(f=C.current,p=P.current,f&&"function"==typeof f?[4,f(null===(a=e.data)||void 0===a?void 0:a.payload)]:[3,6]);case 5:return l=s.sent(),[3,10];case 6:return p?[4,fetch(g(p.url,n,null==l?void 0:l.code,i,h),{method:null!==(c=p.method)&&void 0!==c?c:"POST",headers:p.headers||{}})]:[3,9];case 7:return[4,s.sent().json()];case 8:return l=s.sent(),[3,10];case 9:throw new Error("useOAuth2: You must provide `exchangeCodeForTokenQuery` or `exchangeCodeForTokenQueryFn`");case 10:return U({loading:!1,error:null}),R(l),S?[4,S(l)]:[3,12];case 11:s.sent(),s.label=12;case 12:return[3,17];case 13:return v=s.sent(),console.error(v),U({loading:!1,error:v.toString()}),E?[4,E(v.toString())]:[3,15];case 14:s.sent(),s.label=15;case 15:return[3,17];case 16:return w(I,k,m),[7];case 17:return[2]}}))}))}return function(e,r){e.setItem(l,r)}(sessionStorage,h),k.current=(t=function(e,r,n,o,t,a,i){void 0===i&&(i={});var u=p(c({response_type:a,client_id:r,redirect_uri:n,scope:o,state:t},i));return"".concat(e,"?").concat(u)}(r,n,i,v,h,y,x.current),a=window.outerHeight/2+window.screenY-350,f=window.outerWidth/2+window.screenX-300,window.open(t,"OAuth2 Popup","height=".concat(700,",width=").concat(600,",top=").concat(a,",left=").concat(f))),window.addEventListener("message",m),I.current=setInterval((function(){var e,r,n;(!(null===(e=k.current)||void 0===e?void 0:e.window)||(null===(n=null===(r=k.current)||void 0===r?void 0:r.window)||void 0===n?void 0:n.closed))&&(U((function(e){return c(c({},e),{loading:!1})})),console.warn("Warning: Popup was closed before completing authentication."),w(I,k,m))}),250),function(){window.removeEventListener("message",m),I.current&&clearInterval(I.current)}}),[r,n,i,v,y,S,E,U,R]);return{data:L,loading:Q,error:A,getAuth:q,logout:a((function(){z(),U({loading:!1,error:null})}),[z]),isPersistent:V}};export{m as OAuthPopup,I as useOAuth2};
package/package.json CHANGED
@@ -12,7 +12,7 @@
12
12
  "nodejs",
13
13
  "oauth2"
14
14
  ],
15
- "version": "2.0.0",
15
+ "version": "2.0.2",
16
16
  "description": "A React hook that handles OAuth2 authorization flow.",
17
17
  "license": "MIT",
18
18
  "homepage": "https://github.com/tasoskakour/react-use-oauth2#readme",
@@ -50,7 +50,7 @@
50
50
  ]
51
51
  },
52
52
  "peerDependencies": {
53
- "react": "^18.0.0"
53
+ "react": "^18.0.0 || ^19.0.0"
54
54
  },
55
55
  "devDependencies": {
56
56
  "@babel/core": "^7.21.4",
@@ -83,9 +83,9 @@
83
83
  "jest": "^29.7.0",
84
84
  "jest-environment-jsdom": "^29.7.0",
85
85
  "jest-fetch-mock": "^3.0.3",
86
- "jest-puppeteer": "^9.0.0",
86
+ "jest-puppeteer": "^11.0.0",
87
87
  "lint-staged": "^13.2.2",
88
- "puppeteer": "21.0.0",
88
+ "puppeteer": "^24.0.0",
89
89
  "react-dom": "^18.2.0",
90
90
  "react-router-dom": "^6.11.2",
91
91
  "rollup": "^3.29.4",