npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@1nd/react-social-login

v4.0.0

Published

React Component for Login via Social Providers

Downloads

4

Readme

React Social Login · NPM version · Standard - JavaScript Style Guide · Dependencies · License: MIT

React Social Login is an HOC which provides social login through multiple providers.

Currently supports Amazon, Facebook, GitHub, Google, Instagram and LinkedIn as providers (more to come!)

We aren't using Google+ Api. Library will work fine even after Google+ deprecation.

Motivation

  • Having a component that doesn't dictates the HTML
  • All-in-One login component for different social providers
  • Takes care of warnings from provider's SDKs when multiple instances are placed
  • Kind of re-birth of my previous .Net driven similar open source - SocialAuth.NET

Scopes

While this library does a good job of fetching basic profile, at times you might need to get additional attributes from providers like DateOfBirth, HomeTowm, etc. which aren't returned by default. Scopes are purely provider dependent and their documentation is the best place to look for supported scopes and literal to be passed as argument. You can pass those scopes using the scope tag. For example, if you want birth date from Facebook (which isn't returned by default), you'd add following scope to your tag:

 scope='user_birthday,user_hometown'

Below are some links to official scopes guide for a few providers:

Online demo

See https://deepakaggarwal7.github.io/react-social-login.

Demo

Edit appId props with your own ones in demo/index.js file and build demo:

$ npm start

You can then view the demo at https://localhost:8080.

For GitHub provider, see GitHub specifics first.

Install

$ npm install --save react-social-login

Usage

Create the component of your choice and transform it into a SocialLogin component.

SocialButton.js

import React from 'react'
import SocialLogin from 'react-social-login'

const Button = ({ children, triggerLogin, ...props }) => (
  <button onClick={triggerLogin} {...props}>
    { children }
  </button>
)

export default SocialLogin(Button)

Then, use it like a normal component.

index.js

import React from 'react'
import ReactDOM from 'react-dom'

import SocialButton from './SocialButton'

const handleSocialLogin = (user) => {
  console.log(user)
}

const handleSocialLoginFailure = (err) => {
  console.error(err)
}

ReactDOM.render(
  <div>
    <SocialButton
      provider='facebook'
      appId='YOUR_APP_ID'
      onLoginSuccess={handleSocialLogin}
      onLoginFailure={handleSocialLoginFailure}
    >
      Login with Facebook
    </SocialButton>
  </div>,
  document.getElementById('app')
)

Reference

Raw component props (before transform):

| Prop | Default | Type / Values | Description | |---|---|---|---| | appId | — | string | Your app identifier (see find my appId) | | autoCleanUri | false | boolean | Enable auto URI cleaning with OAuth callbacks | | autoLogin | false | boolean | Enable auto login on componentDidMount | | gatekeeper | — | string | Gatekeeper URL to use for GitHub OAuth support (see GitHub specifics) | | getInstance | — | function | Return node ref like ref function would normally do (react known issue) | | onLoginFailure | — | function | Callback on login fail | | onLoginSuccess | — | function | Callback on login success | | onLogoutFailure | — | function | Callback on logout fail (google only) | | onLogoutSuccess | — | function | Callback on logout success | | provider | — | amazon, facebook, github, google, instagram, linkedin | Social provider to use | | redirect | - | string | URL to redirect after login (available for github and instagram only) | | scope | - | array, string | An array or string of scopes to be granted on login. | | any other prop | — | — | Any other prop will be forwarded to your component |

Note about redirect: if you are redirecting on root (eg: https://localhost:8080), you have to omit the trailing slash.

Transformed component props:

| Prop | Type | Description | |---|---|---| | triggerLogin | function | Function to trigger login process, usually attached to an event listener | | triggerLogout | function | Function to trigger logout process, usually attached to a container handling login state | | all your props | — | All props from your original component, minus SocialLogin specific props |

Logout

To implement logout, we need a container handling login state and triggering logout function from a ref to SocialLogin component.

As it is implemented in the demo, we have two components working together to trigger logout:

Here is how they work together:

  1. Demo is displaying UserCard only if user is logged
  2. UserCard gets forwarded a logout function
  3. UserCard calls forwarded logout prop on click on the logout button
  4. logout function triggers triggerLogout prop from a ref to SocialLogin component

Old component support

We decided to keep the old behavior as a fallback, it only supports facebook, google and linkedin providers and is available as a named export:

import React from 'react'
import ReactDOM from 'react-dom'
import { OldSocialLogin as SocialLogin } from 'react-social-login'

const handleSocialLogin = (user, err) => {
  console.log(user)
  console.log(err)
}

ReactDOM.render(
  <div>
    <SocialLogin
      provider='facebook'
      appId='YOUR_APP_ID'
      callback={handleSocialLogin}
    >
      <button>Login with Google</button>
    </SocialLogin>
  </div>,
  document.getElementById('app')
)

Build

Though not mandatory, it is recommended to use latest npm5 to match lockfile versions.

$ npm install
$ npm run build

Miscellaneous

Find my appId

Amazon

See Amazon developers documentation.

Facebook

See facebook for developers documentation.

GitHub (see GitHub specifics)

Google

See Google Sign-In for Websites guide.

Instagram

See Instagram developers documentation.

LinkedIn

See Where can I find my API key? section on the FAQ.

GitHub specifics

GitHub provider is implemented in two different modes:

GitHub Personal Tokens mode

Actually, this one is more a hacky way to get user profile than a way to really connect your app like OAuth does.

Plus, it requires from users to create their personal token from their GitHub account, which is not a good experience for them.

This mode is the default if you do not provide gatekeeper prop and will try to use the appId prop to get user data. Anyway, we strongly advise you to use the GitHub OAuth authentication flow.

GitHub OAuth

If you provide a gatekeeper prop, this mode will be active and will use a server of your own to fetch GitHub OAuth access token. This is a know issue of GitHub.

The simplest way to setup this mode is to use the Gatekeeper project. Just follow setup instructions then tell RSL to use it:

<SocialLogin
  provider='github'
  gatekeeper='http://localhost:9999'
  appId='YOUR_GITHUB_CLIENT_ID'
  redirect='http://localhost:8080'
>
  Login with GitHub OAuth
</SocialLogin>

You can also implement it your own way but you must use the same routing than Gatekeeper (/authenticate/:code) and return a JSON response containing a token or error property (it will also throw if it doesn't find token).

Special instructions to run demo

RSL demo is served over https with webpack-dev-server. This is a requirement of Amazon Login SDK. Gatekeeper is served over insecure http so you will have to serve the demo through http also to work with GitHub (but it will break Amazon):

$ npm run start:insecure

Tests

TBD

Main contributors

Kind contributors

  • Kamran Ahmed : custom Google scopes
  • Adrien Cohen : multiple Google buttons
  • Jason Loo : full google auth response
  • Andri Janusson : LinkedIn public profile URL
  • Adrien Pascal : SSR support