UNPKG

@rahulsolanki_ct/linkedin-login

Version:

Package for Linked In Log In feature using OAuth 2.0

3 lines (2 loc) 3.21 kB
import{useRef as e,useCallback as r,useEffect as n,useState as t}from"react";import{jsx as o}from"react/jsx-runtime";var c="linkedin_oauth2_state";function i(t){var o=t.redirectUri,i=t.clientId,a=t.onSuccess,s=t.onError,d=t.scope,u=void 0===d?"r_emailaddress":d,l=t.state,w=void 0===l?"":l,p=t.closePopupMessage,g=void 0===p?"User closed the popup":p,v=e(null),f=e(null),h=r((function(e){var r=localStorage.getItem(c);if(e.origin===window.location.origin)if(e.data.errorMessage&&"Linked In"===e.data.from){if(e.data.state!==r)return void(v.current&&v.current.close());s&&s(e.data),v.current&&v.current.close()}else if(e.data.code&&"Linked In"===e.data.from){if(e.data.state!==r)return void(v.current&&v.current.close());a&&a(e.data.code),v.current&&v.current.close()}}),[s,a]);n((function(){return function(){window.removeEventListener("message",h,!1),v.current&&(v.current.close(),v.current=null),f.current&&(window.clearInterval(f.current),f.current=null)}}),[h]),n((function(){return window.addEventListener("message",h,!1),function(){window.removeEventListener("message",h,!1)}}),[h]);var m=function(){var e="&scope=".concat(encodeURI(u)),r=w||function(e){void 0===e&&(e=20);for(var r="",n="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",t=n.length,o=0;o<e;o++)r+=n.charAt(Math.floor(Math.random()*t));return r}();return localStorage.setItem(c,r),"https://www.linkedin.com/oauth/v2/authorization?response_type=code&client_id=".concat(i,"&redirect_uri=").concat(o).concat(e,"&state=").concat(r)};return{linkedInLogin:function(){var e;null===(e=v.current)||void 0===e||e.close(),v.current=window.open(m(),"_blank",function(e){var r=e.width,n=void 0===r?600:r,t=e.height,o=void 0===t?600:t,c=screen.width/2-n/2,i=screen.height/2-o/2;return"left=".concat(c,",top=").concat(i,",width=").concat(n,",height=").concat(o)}({width:600,height:600})),f.current&&(window.clearInterval(f.current),f.current=null),f.current=window.setInterval((function(){try{v.current&&v.current.closed&&(window.clearInterval(f.current),f.current=null,s&&s({error:"user_closed_popup",errorMessage:g}))}catch(e){window.clearInterval(f.current),f.current=null}}),1e3)}}}function a(e){return(0,e.children)({linkedInLogin:i({redirectUri:e.redirectUri,clientId:e.clientId,onSuccess:e.onSuccess,onError:e.onError,state:e.state,scope:e.scope,closePopupMessage:e.closePopupMessage}).linkedInLogin})}function s(){var e=t(""),r=e[0],i=e[1];return n((function(){var e=function(e){for(var r=e.substring(1).split("&"),n={},t=0;t<r.length;t++){var o=r[t].split("=");o.length>1&&(n[decodeURIComponent(o[0])]=decodeURIComponent(o[1]))}return n}(window.location.search);if(e.state!==localStorage.getItem(c))i("State does not match");else if(e.error){var r=e.error_description||"Login failed. Please try again.";window.opener&&window.opener.postMessage({error:e.error,state:e.state,errorMessage:r,from:"Linked In"},window.location.origin),"user_cancelled_login"===e.error&&window.close()}e.code&&window.opener&&window.opener.postMessage({code:e.code,state:e.state,from:"Linked In"},window.location.origin)}),[]),o("div",{children:r})}export{a as LinkedIn,s as LinkedInCallback,i as useLinkedIn}; //# sourceMappingURL=index.esm.js.map