UNPKG

react-oauth2

Version:

![npm](https://img.shields.io/npm/v/react-oauth2) ![GitHub](https://img.shields.io/github/license/maxifjaved/react-oauth2) ![npm bundle size](https://img.shields.io/bundlephobia/minzip/react-oauth2) ![npm type definitions](https://img.shields.io/npm/types

3 lines (2 loc) 15.8 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t,e=(t=require("react"))&&"object"==typeof t&&"default"in t?t.default:t;function r(t,e,r,n,o,i,a){try{var c=t[i](a),u=c.value}catch(t){return void r(t)}c.done?e(u):Promise.resolve(u).then(n,o)}function n(t){return function(){var e=this,n=arguments;return new Promise((function(o,i){var a=t.apply(e,n);function c(t){r(a,o,i,c,u,"next",t)}function u(t){r(a,o,i,c,u,"throw",t)}c(void 0)}))}}function o(){return o=Object.assign?Object.assign.bind():function(t){for(var e=1;arguments.length>e;e++){var r=arguments[e];for(var n in r)({}).hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t},o.apply(null,arguments)}function i(t,e){if(null==t)return{};var r={};for(var n in t)if({}.hasOwnProperty.call(t,n)){if(e.includes(n))continue;r[n]=t[n]}return r}function a(){a=function(){return e};var t,e={},r=Object.prototype,n=r.hasOwnProperty,o=Object.defineProperty||function(t,e,r){t[e]=r.value},i="function"==typeof Symbol?Symbol:{},c=i.iterator||"@@iterator",u=i.asyncIterator||"@@asyncIterator",s=i.toStringTag||"@@toStringTag";function l(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{l({},"")}catch(t){l=function(t,e,r){return t[e]=r}}function h(t,e,r,n){var i=Object.create((e&&e.prototype instanceof g?e:g).prototype),a=new I(n||[]);return o(i,"_invoke",{value:O(t,r,a)}),i}function f(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}e.wrap=h;var p="suspendedStart",d="suspendedYield",v="executing",y="completed",w={};function g(){}function b(){}function m(){}var x={};l(x,c,(function(){return this}));var k=Object.getPrototypeOf,E=k&&k(k(N([])));E&&E!==r&&n.call(E,c)&&(x=E);var L=m.prototype=g.prototype=Object.create(x);function _(t){["next","throw","return"].forEach((function(e){l(t,e,(function(t){return this._invoke(e,t)}))}))}function j(t,e){function r(o,i,a,c){var u=f(t[o],t,i);if("throw"!==u.type){var s=u.arg,l=s.value;return l&&"object"==typeof l&&n.call(l,"__await")?e.resolve(l.__await).then((function(t){r("next",t,a,c)}),(function(t){r("throw",t,a,c)})):e.resolve(l).then((function(t){s.value=t,a(s)}),(function(t){return r("throw",t,a,c)}))}c(u.arg)}var i;o(this,"_invoke",{value:function(t,n){function o(){return new e((function(e,o){r(t,n,e,o)}))}return i=i?i.then(o,o):o()}})}function O(e,r,n){var o=p;return function(i,a){if(o===v)throw Error("Generator is already running");if(o===y){if("throw"===i)throw a;return{value:t,done:!0}}for(n.method=i,n.arg=a;;){var c=n.delegate;if(c){var u=U(c,n);if(u){if(u===w)continue;return u}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if(o===p)throw o=y,n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);o=v;var s=f(e,r,n);if("normal"===s.type){if(o=n.done?y:d,s.arg===w)continue;return{value:s.arg,done:n.done}}"throw"===s.type&&(o=y,n.method="throw",n.arg=s.arg)}}}function U(e,r){var n=r.method,o=e.iterator[n];if(o===t)return r.delegate=null,"throw"===n&&e.iterator.return&&(r.method="return",r.arg=t,U(e,r),"throw"===r.method)||"return"!==n&&(r.method="throw",r.arg=new TypeError("The iterator does not provide a '"+n+"' method")),w;var i=f(o,e.iterator,r.arg);if("throw"===i.type)return r.method="throw",r.arg=i.arg,r.delegate=null,w;var a=i.arg;return a?a.done?(r[e.resultName]=a.value,r.next=e.nextLoc,"return"!==r.method&&(r.method="next",r.arg=t),r.delegate=null,w):a:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,w)}function P(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function S(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function I(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(P,this),this.reset(!0)}function N(e){if(e||""===e){var r=e[c];if(r)return r.call(e);if("function"==typeof e.next)return e;if(!isNaN(e.length)){var o=-1,i=function r(){for(;++o<e.length;)if(n.call(e,o))return r.value=e[o],r.done=!1,r;return r.value=t,r.done=!0,r};return i.next=i}}throw new TypeError(typeof e+" is not iterable")}return b.prototype=m,o(L,"constructor",{value:m,configurable:!0}),o(m,"constructor",{value:b,configurable:!0}),b.displayName=l(m,s,"GeneratorFunction"),e.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===b||"GeneratorFunction"===(e.displayName||e.name))},e.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,m):(t.__proto__=m,l(t,s,"GeneratorFunction")),t.prototype=Object.create(L),t},e.awrap=function(t){return{__await:t}},_(j.prototype),l(j.prototype,u,(function(){return this})),e.AsyncIterator=j,e.async=function(t,r,n,o,i){void 0===i&&(i=Promise);var a=new j(h(t,r,n,o),i);return e.isGeneratorFunction(r)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},_(L),l(L,s,"Generator"),l(L,c,(function(){return this})),l(L,"toString",(function(){return"[object Generator]"})),e.keys=function(t){var e=Object(t),r=[];for(var n in e)r.push(n);return r.reverse(),function t(){for(;r.length;){var n=r.pop();if(n in e)return t.value=n,t.done=!1,t}return t.done=!0,t}},e.values=N,I.prototype={constructor:I,reset:function(e){if(this.prev=0,this.next=0,this.sent=this._sent=t,this.done=!1,this.delegate=null,this.method="next",this.arg=t,this.tryEntries.forEach(S),!e)for(var r in this)"t"===r.charAt(0)&&n.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=t)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(e){if(this.done)throw e;var r=this;function o(n,o){return c.type="throw",c.arg=e,r.next=n,o&&(r.method="next",r.arg=t),!!o}for(var i=this.tryEntries.length-1;i>=0;--i){var a=this.tryEntries[i],c=a.completion;if("root"===a.tryLoc)return o("end");if(this.prev>=a.tryLoc){var u=n.call(a,"catchLoc"),s=n.call(a,"finallyLoc");if(u&&s){if(a.catchLoc>this.prev)return o(a.catchLoc,!0);if(a.finallyLoc>this.prev)return o(a.finallyLoc)}else if(u){if(a.catchLoc>this.prev)return o(a.catchLoc,!0)}else{if(!s)throw Error("try statement without catch or finally");if(a.finallyLoc>this.prev)return o(a.finallyLoc)}}}},abrupt:function(t,e){for(var r=this.tryEntries.length-1;r>=0;--r){var o=this.tryEntries[r];if(this.prev>=o.tryLoc&&n.call(o,"finallyLoc")&&o.finallyLoc>this.prev){var i=o;break}}i&&("break"===t||"continue"===t)&&e>=i.tryLoc&&i.finallyLoc>=e&&(i=null);var a=i?i.completion:{};return a.type=t,a.arg=e,i?(this.method="next",this.next=i.finallyLoc,w):this.complete(a)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),w},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),S(r),w}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n=r.completion;if("throw"===n.type){var o=n.arg;S(r)}return o}}throw Error("illegal catch attempt")},delegateYield:function(e,r,n){return this.delegate={iterator:N(e),resultName:r,nextLoc:n},"next"===this.method&&(this.arg=t),w}},e}function c(t){var e=new URLSearchParams({client_id:t.clientId,redirect_uri:t.redirectUri,scope:t.scope,display:"popup",response_type:"code"});return t.authorizationUrl.includes("google")?(e.append("access_type","offline"),e.append("prompt","select_account consent")):t.authorizationUrl.includes("facebook")&&e.append("auth_type","rerequest"),Promise.resolve({url:t.authorizationUrl+"?"+e,config:t})}function u(t){var e=t.url,r=t.config;return new Promise((function(t){var n=r.width||500,o=r.height||500,i={width:n,height:o,top:window.screenY+(window.outerHeight-o)/2.5,left:window.screenX+(window.outerWidth-n)/2},a=Object.entries(i).map((function(t){return t[0]+"="+t[1]})).join(","),c=window.open(e,"_blank",a);c&&"about:blank"===e&&(c.document.body.innerHTML="Loading..."),t({window:c,config:r})}))}function s(t){var e=t.window,r=t.config,n=t.requestToken;return new Promise((function(t,o){if(n){var i=new URLSearchParams(n);e.location.href=r.authorizationUrl+"?"+i}var a=setInterval((function(){if(!e||e.closed)return clearInterval(a),void o(Error("Popup window was closed"));try{if(e.location.href.startsWith(r.redirectUri)){var n=new URLSearchParams(e.location.search.substring(1).replace(/\/$/,"")),i=new URLSearchParams(e.location.hash.substring(1).replace(/[\/$]/,"")),c={};if([n,i].forEach((function(t){t.forEach((function(t,e){c[e]=t}))})),c.error)return clearInterval(a),void o({error:c.error});if(0===Object.keys(c).length)return clearInterval(a),void o({error:"OAuth redirect has occurred but no query or hash parameters were found."});clearInterval(a),t({oauthData:c,config:r,window:e,interval:a})}}catch(t){}}),500)}))}function l(t){return h.apply(this,arguments)}function h(){return(h=n(a().mark((function t(e){var r,n,o;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=e.interval,o=e.profile,(r=e.window)&&!r.closed&&r.close(),n&&clearInterval(n),t.abrupt("return",{profile:o});case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function f(t){return p.apply(this,arguments)}function p(){return(p=n(a().mark((function t(e){var r;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,c(e).then(u).then(s);case 3:return r=t.sent,t.next=6,d(r);case 6:return t.abrupt("return",l(t.sent));case 10:throw t.prev=10,t.t0=t.catch(0),console.error("Facebook login error:",t.t0),t.t0;case 14:case"end":return t.stop()}}),t,null,[[0,10]])})))).apply(this,arguments)}function d(t){return v.apply(this,arguments)}function v(){return(v=n(a().mark((function t(e){var r,n,i,c,u,s,l,h,f,p;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.window,n=e.interval,i=o({},e.oauthData,e.config),u="https://graph.facebook.com/"+(c="v18.0")+"/oauth/access_token",s=new URLSearchParams({code:i.code,client_id:i.clientId,client_secret:i.clientSecret,redirect_uri:i.redirectUri}),t.next=7,fetch(u+"?"+s).then((function(t){if(!t.ok)throw Error("Token request failed: "+t.statusText);return t.json()}));case 7:if(!("error"in(l=t.sent))){t.next=10;break}throw Error(l.error);case 10:return h="https://graph.facebook.com/"+c+"/me",f=new URLSearchParams({fields:i.scope,access_token:l.access_token}),t.next=14,fetch(h+"?"+f).then((function(t){if(!t.ok)throw Error("Profile request failed: "+t.statusText);return t.json()}));case 14:if(!("error"in(p=t.sent))){t.next=17;break}throw Error(p.error);case 17:return t.abrupt("return",{window:r,interval:n,profile:p});case 18:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function y(t){return w.apply(this,arguments)}function w(){return(w=n(a().mark((function t(e){var r;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,c(e).then(u).then(s);case 3:return r=t.sent,t.next=6,g(r);case 6:return t.abrupt("return",l(t.sent));case 10:throw t.prev=10,t.t0=t.catch(0),console.error("Google login error:",t.t0),t.t0;case 14:case"end":return t.stop()}}),t,null,[[0,10]])})))).apply(this,arguments)}function g(t){return b.apply(this,arguments)}function b(){return(b=n(a().mark((function t(e){var r,n,i,c,u,s;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.window,n=e.interval,i=o({},e.oauthData,e.config),c="https://openidconnect.googleapis.com/v1/userinfo",t.next=6,fetch("https://oauth2.googleapis.com/token",{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:new URLSearchParams({code:i.code,client_id:i.clientId,client_secret:i.clientSecret,redirect_uri:i.redirectUri,grant_type:"authorization_code"})}).then((function(t){if(!t.ok)throw Error("Token request failed: "+t.statusText);return t.json()}));case 6:if(!("error"in(u=t.sent))){t.next=9;break}throw Error(u.error);case 9:return t.next=11,fetch(c,{headers:{Authorization:"Bearer "+u.access_token}}).then((function(t){if(!t.ok)throw Error("Profile request failed: "+t.statusText);return t.json()}));case 11:if(!("error"in(s=t.sent))){t.next=14;break}throw Error(s.error);case 14:return t.abrupt("return",{window:r,interval:n,profile:s});case 15:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var m=["url","clientId","clientSecret","redirectUri","authorizationUrl","scope","width","height","className","style","disabled","children","callback"],x=["style","className","disabled"],k=["url","clientId","clientSecret","redirectUri","authorizationUrl","scope","width","height","className","style","disabled","children","callback"],E=["style","className","disabled"],L=["openid","email","profile"];exports.FacebookOauth2=function(t){var r,c=t.url,u=void 0===c?"http://localhost:3000/":c,s=t.clientId,l=t.clientSecret,h=t.redirectUri,p=void 0===h?"http://localhost:3000/":h,d=t.authorizationUrl,v=void 0===d?"https://www.facebook.com/v18.0/dialog/oauth":d,y=t.scope,w=void 0===y?"email,public_profile":y,g=t.width,b=void 0===g?580:g,k=t.height,E=void 0===k?400:k,L=t.className,_=t.style,j=t.disabled,O=t.children,U=t.callback,P=i(t,m),S=function(){var t=n(a().mark((function t(){var e;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,e={url:u,clientId:s,clientSecret:l,redirectUri:p,authorizationUrl:v,scope:w,width:b,height:E},t.next=4,f(e);case 4:U(null,t.sent),t.next=11;break;case 8:t.prev=8,t.t0=t.catch(0),U(t.t0 instanceof Error?t.t0:Error("Facebook login failed"),null);case 11:case"end":return t.stop()}}),t,null,[[0,8]])})));return function(){return t.apply(this,arguments)}}();return e.createElement("button",Object.assign({type:"button"},(r={},x.forEach((function(t){var e=P[t];void 0!==e&&(r[t]=e)})),r),{className:"facebook-oauth-button "+(L||""),style:o({display:"inline-flex",alignItems:"center",justifyContent:"center",padding:"8px 16px",fontSize:"14px",fontWeight:500,border:"none",borderRadius:"4px",backgroundColor:"#1877f2",color:"white",cursor:j?"not-allowed":"pointer",opacity:j?.7:1,transition:"background-color 0.2s ease"},_),onClick:S,disabled:j}),O)},exports.GoogleOauth2=function(t){var r,c=t.url,u=void 0===c?"http://localhost:3000/":c,s=t.clientId,l=t.clientSecret,h=t.redirectUri,f=void 0===h?"http://localhost:3000/":h,p=t.authorizationUrl,d=void 0===p?"https://accounts.google.com/o/oauth2/v2/auth":p,v=t.scope,w=void 0===v?L:v,g=t.width,b=void 0===g?580:g,m=t.height,x=void 0===m?400:m,_=t.className,j=t.style,O=t.disabled,U=t.children,P=t.callback,S=i(t,k),I=function(){var t=n(a().mark((function t(){var e;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,e={url:u,clientId:s,clientSecret:l,redirectUri:f,authorizationUrl:d,scope:Array.isArray(w)?w.join(" "):w,width:b,height:x},t.next=4,y(e);case 4:P(null,t.sent),t.next=11;break;case 8:t.prev=8,t.t0=t.catch(0),P(t.t0 instanceof Error?t.t0:Error("Google login failed"),null);case 11:case"end":return t.stop()}}),t,null,[[0,8]])})));return function(){return t.apply(this,arguments)}}();return e.createElement("button",Object.assign({type:"button"},(r={},E.forEach((function(t){var e=S[t];void 0!==e&&(r[t]=e)})),r),{className:"google-oauth-button "+(_||""),style:o({display:"inline-flex",alignItems:"center",justifyContent:"center",padding:"8px 16px",fontSize:"14px",fontWeight:500,border:"1px solid #dadce0",borderRadius:"4px",backgroundColor:"white",color:"#3c4043",cursor:O?"not-allowed":"pointer",opacity:O?.7:1,transition:"background-color 0.2s ease, box-shadow 0.2s ease",boxShadow:"0 1px 2px rgba(0, 0, 0, 0.1)"},j),onClick:I,disabled:O}),U)}; //# sourceMappingURL=react-oauth2.cjs.production.min.js.map