1- import React , { Component } from ' react' ;
2- import PropTypes from ' prop-types' ;
1+ import React , { Component } from " react" ;
2+ import PropTypes from " prop-types" ;
33
4- const getPopupPositionProperties = ( {
5- width = 600 ,
6- height = 600 ,
7- } ) => {
8- const left = ( screen . width / 2 ) - ( width / 2 ) ;
9- const top = ( screen . height / 2 ) - ( height / 2 ) ;
4+ const getPopupPositionProperties = ( { width = 600 , height = 600 } ) => {
5+ const left = screen . width / 2 - width / 2 ;
6+ const top = screen . height / 2 - height / 2 ;
107 return `left=${ left } ,top=${ top } ,width=${ width } ,height=${ height } ` ;
11- }
8+ } ;
129
13- const generateRandomString = ( length = 20 ) => {
14- var result = '' ;
15- var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' ;
10+ const generateRandomString = ( length = 20 ) => {
11+ var result = "" ;
12+ var characters =
13+ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" ;
1614 var charactersLength = characters . length ;
17- for ( var i = 0 ; i < length ; i ++ ) {
18- result += characters . charAt ( Math . floor ( Math . random ( ) * charactersLength ) ) ;
15+ for ( var i = 0 ; i < length ; i ++ ) {
16+ result += characters . charAt ( Math . floor ( Math . random ( ) * charactersLength ) ) ;
1917 }
2018 return result ;
21- }
19+ } ;
2220
23- const LINKEDIN_OAUTH2_STATE = ' linkedin_oauth2_state'
21+ const LINKEDIN_OAUTH2_STATE = " linkedin_oauth2_state" ;
2422
2523export class LinkedIn extends Component {
2624 static propTypes = {
@@ -32,41 +30,46 @@ export class LinkedIn extends Component {
3230 clientId : PropTypes . string . isRequired ,
3331 redirectUri : PropTypes . string . isRequired ,
3432 renderElement : PropTypes . func ,
35- }
33+ } ;
3634
3735 componentWillUnmount ( ) {
38- window . removeEventListener ( ' message' , this . receiveMessage , false ) ;
36+ window . removeEventListener ( " message" , this . receiveMessage , false ) ;
3937 if ( this . popup && ! this . popup . closed ) this . popup . close ( ) ;
4038 }
4139
4240 getUrl = ( ) => {
43- const { redirectUri, clientId, scope, supportIE, redirectPath } = this . props ;
41+ const { redirectUri, clientId, scope, supportIE, redirectPath } =
42+ this . props ;
4443 const scopeParam = `&scope=${ supportIE ? scope : encodeURI ( scope ) } ` ;
4544 const state = generateRandomString ( ) ;
4645 localStorage . setItem ( LINKEDIN_OAUTH2_STATE , state ) ;
4746 const linkedInAuthenLink = `https://www.linkedin.com/oauth/v2/authorization?response_type=code&client_id=${ clientId } &redirect_uri=${ redirectUri } ${ scopeParam } &state=${ state } ` ;
4847 if ( supportIE ) {
49- const redirectLink = `${ window . location . origin } ${ redirectPath } ?linkedin_redirect_url=${ encodeURIComponent ( linkedInAuthenLink ) } ` ;
48+ const redirectLink = `${
49+ window . location . origin
50+ } ${ redirectPath } ?linkedin_redirect_url=${ encodeURIComponent (
51+ linkedInAuthenLink
52+ ) } `;
5053 return redirectLink ;
5154 }
5255 return linkedInAuthenLink ;
53- }
56+ } ;
5457
5558 receiveMessage = ( event ) => {
5659 const state = localStorage . getItem ( LINKEDIN_OAUTH2_STATE ) ;
5760 if ( event . origin === window . location . origin ) {
58- if ( event . data . errorMessage && event . data . from === ' Linked In' ) {
61+ if ( event . data . errorMessage && event . data . from === " Linked In" ) {
5962 // Prevent CSRF attack by testing state
6063 if ( event . data . state !== state ) {
6164 this . popup && this . popup . close ( ) ;
6265 return ;
6366 }
6467 this . props . onFailure ( event . data ) ;
6568 this . popup && this . popup . close ( ) ;
66- } else if ( event . data . code && event . data . from === ' Linked In' ) {
69+ } else if ( event . data . code && event . data . from === " Linked In" ) {
6770 // Prevent CSRF attack by testing state
6871 if ( event . data . state !== state ) {
69- console . error ( ' State does not match' ) ;
72+ console . error ( " State does not match" ) ;
7073 this . popup && this . popup . close ( ) ;
7174 return ;
7275 }
@@ -81,32 +84,42 @@ export class LinkedIn extends Component {
8184 e . preventDefault ( ) ;
8285 }
8386 this . props . onClick && this . props . onClick ( ) ;
84- this . popup = window . open ( this . getUrl ( ) , '_blank' , getPopupPositionProperties ( { width : 600 , height : 600 } ) ) ;
85- window . removeEventListener ( 'message' , this . receiveMessage , false ) ;
86- window . addEventListener ( 'message' , this . receiveMessage , false ) ;
87- }
88-
87+ this . popup = window . open (
88+ this . getUrl ( ) ,
89+ "_blank" ,
90+ getPopupPositionProperties ( { width : 600 , height : 600 } )
91+ ) ;
92+ window . removeEventListener ( "message" , this . receiveMessage , false ) ;
93+ window . addEventListener ( "message" , this . receiveMessage , false ) ;
94+ } ;
8995
9096 render ( ) {
9197 const { className, disabled, children, renderElement, style } = this . props ;
9298 if ( renderElement ) {
93- return renderElement ( { onClick : this . handleConnectLinkedInClick , disabled } )
99+ return renderElement ( {
100+ onClick : this . handleConnectLinkedInClick ,
101+ disabled,
102+ } ) ;
94103 }
95104 return (
96105 < button
97106 type = "button"
98107 onClick = { this . handleConnectLinkedInClick }
99108 className = { className }
100109 disabled = { disabled }
101- style = { style ? style : {
102- background : 'none' ,
103- color : 'inherit' ,
104- border : 'none' ,
105- padding : 0 ,
106- cursor : 'pointer' ,
107- font : 'inherit' ,
108- outline : 'inherit' ,
109- } }
110+ style = {
111+ style
112+ ? style
113+ : {
114+ background : "none" ,
115+ color : "inherit" ,
116+ border : "none" ,
117+ padding : 0 ,
118+ cursor : "pointer" ,
119+ font : "inherit" ,
120+ outline : "inherit" ,
121+ }
122+ }
110123 >
111124 { children }
112125 </ button >
@@ -116,9 +129,15 @@ export class LinkedIn extends Component {
116129
117130LinkedIn . defaultProps = {
118131 disabled : false ,
119- children : ( < img src = { require ( '../assets/linkedin.png' ) } alt = "Log in with Linked In" style = { { maxWidth : '180px' } } /> ) ,
132+ children : (
133+ < img
134+ src = { require ( "../assets/linkedin.png" ) }
135+ alt = "Log in with Linked In"
136+ style = { { maxWidth : "180px" } }
137+ />
138+ ) ,
120139 supportIE : false ,
121- redirectPath : ' /linkedin' ,
122- scope : ' r_emailaddress' ,
140+ redirectPath : " /linkedin" ,
141+ scope : " r_emailaddress" ,
123142} ;
124143export default LinkedIn ;
0 commit comments