1.0.0 • Published 8 months ago

strapi-linkedin-auth v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

Social Login using LinkedIn Plugin for Strapi

Introduction

This plugin enables LinkedIn social login using OpenID Connect in Strapi. It handles the OAuth 2.0 authorization flow, retrieves user information from LinkedIn, and creates or updates the user in Strapi's database.

Installation

  1. Install our plugin into your Strapi project:

npm install strapi-linkedin-auth

  1. Install required dependencies:

npm install

npm install axios react-icons

  1. Set up environment variables:

Add the following to your .env file:

LINKEDIN_CLIENT_ID=<your-client-id>
LINKEDIN_CLIENT_SECRET=<your-client-secret>
REDIRECT_URI=<your-redirect-uri>

Your ".env" file should look like this:

HOST=0.0.0.0
PORT=1337
APP_KEYS=your_app_key_1,your_app_key_2,your_app_key_3,your_app_key_4
API_TOKEN_SALT=your_api_token_salt
ADMIN_JWT_SECRET=your_admin_jwt_secret
TRANSFER_TOKEN_SALT=your_transfer_token_salt
# Database
DATABASE_CLIENT=sqlite
DATABASE_FILENAME=.tmp/data.db

JWT_SECRET=your_jwt_secret

LINKEDIN_CLIENT_ID= your_client_id
LINKEDIN_CLIENT_SECRET= your_client_secret
REDIRECT_URI = your_redirect_url

Your "root folder config/plugins.js" should look like this:

module.exports = {
  "linkedin-auth": {
    enabled: true,
    resolve: "./src/plugins/linkedin-auth",
  },
};

Plugin Structure

  • linkedin-auth/admin/src/pages/homepage/index.js: frontend component for LinkedIn login in the Strapi admin panel.
  • linkedin-auth/server/controllers/linkedin-auth.js: Handles LinkedIn OAuth flow and user creation/updating.
  • linkedin-auth/server/routes/index.js: Defines API routes for LinkedIn authentication.

Endpoints

  • GET /linkedin-auth/linkedin-config: Fetch LinkedIn client ID and redirect URI.
  • POST /linkedin-auth/token: Exchange authorization code for access token and retrieve user info.

LinkedIn's Endpoints that we have used:

Handling LinkedIn Sign-In:

const handleLinkedInSignIn = () => {
  const url = `https://www.linkedin.com/oauth/v2/authorization?client_id=${clientId}&redirect_uri=${redirectUri}&response_type=code&scope=openid%20profile%20email`;
  window.location.href = url;
};

Exchanging Authorization Code for Token:

const response = await axios.post(
  "https://www.linkedin.com/oauth/v2/accessToken",
  new URLSearchParams({
    grant_type: "authorization_code",
    code: code,
    redirect_uri: process.env.REDIRECT_URI,
    client_id: process.env.LINKEDIN_CLIENT_ID,
    client_secret: process.env.LINKEDIN_CLIENT_SECRET,
  }).toString(),
  {
    headers: {
      "Content-Type": "application/x-www-form-urlencoded",
    },
    httpsAgent,
  }
);

const accessToken = response.data.access_token;

Required Permissions

  • openid: Authenticate the user.
  • profile: Retrieve the user's LinkedIn profile.
  • email: Retrieve the user's email address.

Response Data

After successful LinkedIn login, the following data is returned:

{
  "isValid": true,
  "message": "LinkedIn validated successfully, user created or exists",
  "jwt": "<jwt-token>",
  "user": {
    "id": "<user-id>",
    "username": "<user-name>",
    "email": "<user-email>"
  }
}

Usage

  1. Start Strapi:

npm run develop

  1. Navigate to the LinkedIn login page in the Strapi admin panel.

  2. Click on "Sign in with LinkedIn" to initiate the login process.

  3. After successful authentication, the user will be created or updated in the Strapi database, and a JWT token will be issued.

Additional Information

(https://learn.microsoft.com/en-us/linkedin/consumer/integrations/self-serve/sign-in-with-linkedin-v2?context=linkedin%2Fconsumer%2Fcontext) and (https://docs.microsoft.com/en-us/linkedin/shared/authentication/authentication?context=linkedin/context)

Flow Diagram

Here is the LinkedIn Flow Diagram:

LinkedIn Flow Diagram

Video Demonstrations

React Frontend

React Frontend

  • Filename: sample_react_frontend.mp4
  • Description: Demonstrates the React frontend setup.

Strapi Homepage Frontend

Strapi Homepage Frontend

  • Filename: sample_strapi_homepage_frontend.mp4
  • Description: Shows the frontend for the Strapi homepage.

Normal Login Strapi Homepage

Normal Login Strapi Homepage

  • Filename: normal_login_strapi_homepage.mp4
  • Description: Demonstrates the normal login functionality on the Strapi homepage.

GitHub Repository

You can find the source code and contribute to the project on GitHub: strapi-linkedin-auth

Contributing

We welcome contributions to this plugin. If you have any improvements or bug fixes, please open an issue or submit a pull request on the GitHub repository.