Remix Auth Linkedin Strategy

2.0.1 · active · verified Wed Apr 22

This library provides a LinkedIn authentication strategy for `remix-auth`, enabling users to log into Remix applications via their LinkedIn accounts. It extends `remix-auth-oauth2` and is currently at version 2.0.1, shipping with full TypeScript definitions for robust development. The package maintains a steady release cadence, with recent updates focused on dependency upgrades and critical API changes. A significant update in version 2.0.0 involved transitioning from LinkedIn's legacy OAuth 2.0 flow to the newer OpenID Connect standard, reflecting LinkedIn's evolving authentication protocols. This change required corresponding adjustments in application setup and profile handling. `remix-auth-linkedin` supports both Node.js and Cloudflare runtimes, making it versatile for various Remix deployment environments. Developers must register an OAuth application on the LinkedIn Developers page to obtain the necessary `clientID` and `clientSecret` for integration. It seamlessly integrates with `remix-auth`'s session management, leveraging `createCookieSessionStorage` or similar for authentication persistence.

Common errors

Warnings

Install

Imports

Quickstart

This code snippet demonstrates the setup of `LinkedinStrategy` with `remix-auth`, including session storage configuration and the essential authentication callback, providing a foundation for user login workflows.

import { createCookieSessionStorage } from '@remix-run/node';
import { Authenticator } from 'remix-auth';
import { LinkedinStrategy } from "remix-auth-linkedin";

// Personalize this options for your usage.
const cookieOptions = {
	path: '/',
	httpOnly: true,
	sameSite: 'lax' as const,
	maxAge: 24 * 60 * 60 * 1000 * 30,
	secrets: [process.env.SESSION_SECRET ?? 'fallback_secret_for_dev_only'], // Use env variable for production
	secure: process.env.NODE_ENV !== 'development',
};

const sessionStorage = createCookieSessionStorage({
	cookie: cookieOptions,
});

export const authenticator = new Authenticator<string>(sessionStorage, {
	throwOnError: true,
});

const linkedinStrategy = new LinkedinStrategy(
   {
      clientID: process.env.LINKEDIN_CLIENT_ID ?? 'YOUR_CLIENT_ID_FROM_ENV',
      clientSecret: process.env.LINKEDIN_CLIENT_SECRET ?? 'YOUR_CLIENT_SECRET_FROM_ENV',
      callbackURL: process.env.LINKEDIN_CALLBACK_URL ?? 'https://example.com/auth/linkedin/callback',
   },
   async ({accessToken, refreshToken, extraParams, profile, context}) => {
      // In a real application, you would interact with your database or API here
      // For example, finding or creating a user based on the LinkedIn profile
      console.log('LinkedIn Profile:', profile.id, profile.displayName, profile.emails[0].value);
      console.log('Access Token:', accessToken);
      // Simulate user data storage
      return { id: profile.id, email: profile.emails[0].value, name: profile.displayName };
   }
);

authenticator.use(linkedinStrategy, 'linkedin');

// app/routes/login.tsx
// export default function Login() {
//    return (
//       <form action="/auth/linkedin" method="post">
//          <button>Login with Linkedin</button>
//       </form>
//    )
// }

// app/routes/auth/linkedin.tsx
// import { ActionFunction } from '@remix-run/node';
// import { authenticator } from '~/linkedin.server'; // Adjust path
//
// export let loader: ActionFunction = () => { throw new Response(null, { status: 404 }); }; // Loader is not strictly needed here
// export let action: ActionFunction = ({ request }) => {
//    return authenticator.authenticate('linkedin', request);
// };

// app/routes/auth/linkedin/callback.tsx
// import { LoaderFunction } from '@remix-run/node';
// import { authenticator } from '~/linkedin.server'; // Adjust path
//
// export let loader: LoaderFunction = ({ request }) => {
//    return authenticator.authenticate('linkedin', request, {
//       successRedirect: '/dashboard',
//       failureRedirect: '/login',
//    });
// };

view raw JSON →