UNPKG

@rahulsolanki_ct/linkedin-login

Version:

Package for Linked In Log In feature using OAuth 2.0

3 lines (2 loc) 3.27 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),r=require("react/jsx-runtime"),n="linkedin_oauth2_state";function t(r){var t=r.redirectUri,o=r.clientId,c=r.onSuccess,i=r.onError,a=r.scope,s=void 0===a?"r_emailaddress":a,u=r.state,d=void 0===u?"":u,l=r.closePopupMessage,w=void 0===l?"User closed the popup":l,f=e.useRef(null),p=e.useRef(null),v=e.useCallback((function(e){var r=localStorage.getItem(n);if(e.origin===window.location.origin)if(e.data.errorMessage&&"Linked In"===e.data.from){if(e.data.state!==r)return void(f.current&&f.current.close());i&&i(e.data),f.current&&f.current.close()}else if(e.data.code&&"Linked In"===e.data.from){if(e.data.state!==r)return void(f.current&&f.current.close());c&&c(e.data.code),f.current&&f.current.close()}}),[i,c]);e.useEffect((function(){return function(){window.removeEventListener("message",v,!1),f.current&&(f.current.close(),f.current=null),p.current&&(window.clearInterval(p.current),p.current=null)}}),[v]),e.useEffect((function(){return window.addEventListener("message",v,!1),function(){window.removeEventListener("message",v,!1)}}),[v]);var g=function(){var e="&scope=".concat(encodeURI(s)),r=d||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(n,r),"https://www.linkedin.com/oauth/v2/authorization?response_type=code&client_id=".concat(o,"&redirect_uri=").concat(t).concat(e,"&state=").concat(r)};return{linkedInLogin:function(){var e;null===(e=f.current)||void 0===e||e.close(),f.current=window.open(g(),"_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})),p.current&&(window.clearInterval(p.current),p.current=null),p.current=window.setInterval((function(){try{f.current&&f.current.closed&&(window.clearInterval(p.current),p.current=null,i&&i({error:"user_closed_popup",errorMessage:w}))}catch(e){window.clearInterval(p.current),p.current=null}}),1e3)}}}exports.LinkedIn=function(e){return(0,e.children)({linkedInLogin:t({redirectUri:e.redirectUri,clientId:e.clientId,onSuccess:e.onSuccess,onError:e.onError,state:e.state,scope:e.scope,closePopupMessage:e.closePopupMessage}).linkedInLogin})},exports.LinkedInCallback=function(){var t=e.useState(""),o=t[0],c=t[1];return e.useEffect((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(n))c("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)}),[]),r.jsx("div",{children:o})},exports.useLinkedIn=t; //# sourceMappingURL=index.js.map