|
| 1 | +### MIGRATION TO VERSION 2 |
| 2 | + |
| 3 | +`react-linkedin-login-oauth2` is rewritten using typescript and updated to use functional component with hooks. The API also changed to make it easier to integrate. In version 1, it comes with a default `Signin with Linked In` button. In version 2, you can use your own `Signin with Linked In` button. You cans still use the default image, which is bundled within the package at `react-linkedin-login-oauth2/assets/linkedin.png`. |
| 4 | + |
| 5 | +- The `onSuccess` callback now have `code` as an argument (version 1 is `{code}`) |
| 6 | +- The callback to handle error changed name from `onFailure` to `onError` |
| 7 | + |
| 8 | +Given that in version 1, your code is similar to bellow: |
| 9 | + |
| 10 | +```javascript |
| 11 | +import { LinkedIn } from 'react-linkedin-login-oauth2'; |
| 12 | + |
| 13 | +function LoginWithLinked() { |
| 14 | + const handleSuccess = (data) => { |
| 15 | + console.log(data.code); |
| 16 | + }; |
| 17 | + const handleFailure = (error) => { |
| 18 | + console.log(error.errorMessage); |
| 19 | + }; |
| 20 | + return ( |
| 21 | + <LinkedIn |
| 22 | + clientId="your-client-id" |
| 23 | + onSuccess={handleSuccess} |
| 24 | + onFailure={handleFailure} |
| 25 | + redirectUri="http://localhost:3000/linkedin" |
| 26 | + renderElement={({ onClick, disabled }) => ( |
| 27 | + <button onClick={onClick} disabled={disabled}> |
| 28 | + Signin with Linkedin |
| 29 | + </button> |
| 30 | + )} |
| 31 | + /> |
| 32 | + ); |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +In version 2, you can use it with hooks like this: |
| 37 | + |
| 38 | +```javascript |
| 39 | +import { useLinkedIn } from 'react-linkedin-login-oauth2'; |
| 40 | +// You can still use default image provided by the package |
| 41 | +import linkedin from 'react-linkedin-login-oauth2/assets/linkedin.png'; |
| 42 | + |
| 43 | +function LoginWithLinked() { |
| 44 | + const { linkedInLogin } = useLinkedIn({ |
| 45 | + clientId: 'your-client-id', |
| 46 | + redirectUri: 'http://localhost:3000/linkedin', |
| 47 | + onSuccess: (code) => { |
| 48 | + // Change from `data.code` to `code` |
| 49 | + console.log(code); |
| 50 | + }, |
| 51 | + // Change from `onFailure` to `onError` |
| 52 | + onError: (error) => { |
| 53 | + console.log(error); |
| 54 | + }, |
| 55 | + }); |
| 56 | + |
| 57 | + return ( |
| 58 | + <img |
| 59 | + src={linkedin} |
| 60 | + alt="Log in with Linked In" |
| 61 | + style={{ maxWidth: '180px' }} |
| 62 | + // Pass `linkedInLogin` to `onClick` handler |
| 63 | + onClick={linkedInLogin} |
| 64 | + /> |
| 65 | + ); |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +If you do not like hooks, you can use render props: |
| 70 | + |
| 71 | +```diff |
| 72 | +import { LinkedIn } from 'react-linkedin-login-oauth2'; |
| 73 | + |
| 74 | +function LoginWithLinked() { |
| 75 | + const handleSuccess = (data) => { |
| 76 | + console.log(data.code); |
| 77 | + }; |
| 78 | + const handleFailure = (error) => { |
| 79 | + console.log(error.errorMessage); |
| 80 | + }; |
| 81 | + return ( |
| 82 | + <LinkedIn |
| 83 | + clientId="your-client-id" |
| 84 | + onSuccess={handleSuccess} |
| 85 | + onFailure={handleFailure} |
| 86 | + redirectUri="http://localhost:3000/linkedin" |
| 87 | +- renderElement={({ onClick, disabled }) => ( |
| 88 | +- <button onClick={onClick} disabled={disabled}> |
| 89 | ++ > |
| 90 | ++ {({ linkedInLogin }) => ( |
| 91 | ++ <button onClick={linkedInLogin}> |
| 92 | + Signin with Linkedin |
| 93 | + </button> |
| 94 | + )} |
| 95 | +- /> |
| 96 | ++ </LinkedIn> |
| 97 | + ); |
| 98 | +} |
| 99 | +``` |
0 commit comments