Giter Club home page Giter Club logo

react-linkedin-login-oauth2's Introduction

React Linked In Login Using OAuth 2.0

react-linkedin-login-oauth2 VERSION 2 IS OUT. THIS IS MIGRATION GUIDE FROM 1 TO 2.

All Contributors PRs Welcome

npm package npm

Demo: https://stupefied-goldberg-b44ee5.netlify.app/

This package is used to get authorization code for Linked In Log in feature using OAuth2 in a easy way. After have the authorization code, you can exchange to an access token by sending it to the server to continue to get information needed. For more details, please see at Authorization Code Flow (3-legged OAuth)
See Usage and Demo for instruction.

Table of contents

Changelog

See CHANGELOG.md

Installation

npm install --save react-linkedin-login-oauth2@latest

Overview

We will trigger linkedInLogin by using useLinkedIn (recommended) or LinkedIn (using render props technique) after click on Sign in with LinkedIn button, a popup window will show up and ask for the permission. After we accepted, the pop up window will redirect to redirectUri (should be LinkedInCallback component) then notice its opener about the authorization code Linked In provides us. You can use react-router-dom or Next.js's file system routing

Usage

First, we create a button and provide required props:

import React, { useState } from 'react';

import { useLinkedIn } from 'react-linkedin-login-oauth2';
// You can use provided image shipped by this package or using your own
import linkedin from 'react-linkedin-login-oauth2/assets/linkedin.png';

function LinkedInPage() {
  const { linkedInLogin } = useLinkedIn({
    clientId: '86vhj2q7ukf83q',
    redirectUri: `${window.location.origin}/linkedin`, // for Next.js, you can use `${typeof window === 'object' && window.location.origin}/linkedin`
    onSuccess: (code) => {
      console.log(code);
    },
    onError: (error) => {
      console.log(error);
    },
  });

  return (
    <img
      onClick={linkedInLogin}
      src={linkedin}
      alt="Sign in with Linked In"
      style={{ maxWidth: '180px', cursor: 'pointer' }}
    />
  );
}

If you don't want to use hooks. This library offer render props option:

import React, { useState } from 'react';

import { LinkedIn } from 'react-linkedin-login-oauth2';
// You can use provided image shipped by this package or using your own
import linkedin from 'react-linkedin-login-oauth2/assets/linkedin.png';

function LinkedInPage() {
  return (
    <LinkedIn
      clientId="86vhj2q7ukf83q"
      redirectUri={`${window.location.origin}/linkedin`}
      onSuccess={(code) => {
        console.log(code);
      }}
      onError={(error) => {
        console.log(error);
      }}
    >
      {({ linkedInLogin }) => (
        <img
          onClick={linkedInLogin}
          src={linkedin}
          alt="Sign in with Linked In"
          style={{ maxWidth: '180px', cursor: 'pointer' }}
        />
      )}
    </LinkedIn>
  );
}

Then we point redirect_url to LinkedInCallback. You can use react-router-dom or Next.js's file system routing

  • react-router-dom:
import React from 'react';
import { LinkedInCallback } from 'react-linkedin-login-oauth2';

import { BrowserRouter, Route } from 'react-router-dom';

function Demo() {
  return (
    <BrowserRouter>
      <Route exact path="/linkedin" component={LinkedInCallback} />
    </BrowserRouter>
  );
}
  • Next.js's file system routing:
// pages/linkedin.js
import { LinkedInCallback } from 'react-linkedin-login-oauth2';
export default function LinkedInPage() {
  return <LinkedInCallback />;
}

Support IE

  • Support for IE is dropped from version 2

Demo

Props

  • LinkedIn component:
Parameter value is required default
clientId string yes
redirectUri string yes
onSuccess function yes
onError function no
state string no randomly generated string (recommend to keep default value)
scope string no 'r_emailaddress'
See your app scope here. If there are more than one, delimited by a space
children function no Require if using LinkedIn component (render props)

Reference: https://docs.microsoft.com/en-us/linkedin/shared/authentication/authorization-code-flow?context=linkedin/context#step-2-request-an-authorization-code

  • LinkedInCallback component:
    No parameters needed

Issues

Please create an issue at https://github.com/nvh95/react-linkedin-login-oauth2/issues. I will spend time to help you.

Failed to minify the code from this file: ./node_modules/react-linkedin-login-oauth2/node_modules/query-string/index.js:8

Please upgrade react-linkedin-login-oauth2 to latest version following

npm install --save react-linkedin-login-oauth2

Known issue

Contributors โœจ

Thanks goes to these wonderful people (emoji key):


Hung Viet Nguyen

๐Ÿ’ป

Nguyแป…n Duy Khรกnh

๐Ÿ’ป

YBeck

๐Ÿ’ป

Mehdi Raza

๐Ÿค”

Phillip Denness

๐Ÿ›

dsp.iam

๐Ÿ›

Vitalii Bulyzhyn

๐Ÿ’ป

Pradeep Reddy Guduru

๐Ÿ›

Uric Bonatti Cardoso

๐Ÿ›

faisalur-rehman

๐Ÿ“–

Ruslan

๐Ÿ’ป

This project follows the all-contributors specification. Contributions of any kind welcome!

react-linkedin-login-oauth2's People

Contributors

allcontributors[bot] avatar asanovr avatar nvh95 avatar songuku95 avatar vitalii-bulyzhyn avatar ybeck avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.