LinkedIn auth is broken #509

Closed
opened 2026-03-13 07:50:20 -05:00 by GiteaMirror · 11 comments
Owner

Originally created by @dhruv-m1 on GitHub (Jan 1, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

Try to integrate the Linkedin provider.

Current vs. Expected behavior

Current behavior is a HTTP 500 on the callback url:

ERROR [Better Auth]: TypeError [TypeError: fetch failed] {
  [cause]: [Error: Socket connection timeout] {
    code: 'ERR_SOCKET_CONNECTION_TIMEOUT'
  }
}

image

Expected is a HTTP 302 redirect

What version of Better Auth are you using?

1.1.8

Provide environment information

- OS: Windows 11
- Next.js 15 running locally (localhost) in the Node.js (v20.2.0) runtime

Which area(s) are affected? (Select all that apply)

Package

Auth config (if applicable)

export const auth = betterAuth({
  database: prismaAdapter(prisma, {
    provider: "mongodb",
  }),
  session: {
    expiresIn: 60 * 60 * 24 * 30, // 30 days
    updateAge: 60 * 60 * 24, // 1 day (every 1 day the session expiration is updated)
    cookieCache: {
      enabled: true,
      maxAge: 5 * 60 // Cache duration is 5 seconds
    }
  },
  user: {
    additionalFields: {
      premium: {
        type: "boolean",
        required: false,
      },
    },
    changeEmail: {
      enabled: true,
      sendChangeEmailVerification: async ({ newEmail, url }) => {
        await sendEmail({
          to: newEmail,
          subject: '[Development] Verify your email change',
          react: await VerifyUserEmail({name: "[this service is currently disabled]", id: "1", email: newEmail, url}),
        })
      }
    }
  },
  socialProviders: {
    linkedin: {
      clientId: process.env.LINKEDIN_CLIENT_ID as string,
      clientSecret: process.env.LINKEDIN_CLIENT_SECRET as string,
    },
    google: {
      clientId: process.env.GOOGLE_CLIENT_ID as string,
      clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,
    },
  },
  plugins: [openAPI(), admin({
    impersonationSessionDuration: 60 * 60 * 24 * 7, // 7 days
  })], // api/auth/reference
  emailAndPassword: {
    enabled: true,
    requireEmailVerification: true,
    sendResetPassword: async ({ user, url }) => {
      await sendEmail({
        to: user.email,
        subject: "Password Reset",
        react: await ResetPasswordEmail({name: user.name, id: user.id, email: user.email, url}),
      });
    },
  },
  emailVerification: {
    sendOnSignUp: true,
    autoSignInAfterVerification: true,
    sendVerificationEmail: async ({ user, token }) => {
      const verificationUrl = `${process.env.BETTER_AUTH_URL}/api/auth/verify-email?token=${token}&callbackURL=${process.env.EMAIL_VERIFICATION_CALLBACK_URL}`;
      sendEmail({
        to: user.email,
        subject: "Email Verification",
        react: await VerifyUserEmail({name: user.name, id: user.id, email: user.email, url: verificationUrl}),
      });
    },
  }
} satisfies BetterAuthOptions);

export type Session = typeof auth.$Infer.Session;
Originally created by @dhruv-m1 on GitHub (Jan 1, 2025). ### Is this suited for github? - [X] Yes, this is suited for github ### To Reproduce Try to integrate the Linkedin provider. ### Current vs. Expected behavior Current behavior is a HTTP 500 on the callback url: ``` ERROR [Better Auth]: TypeError [TypeError: fetch failed] { [cause]: [Error: Socket connection timeout] { code: 'ERR_SOCKET_CONNECTION_TIMEOUT' } } ``` ![image](https://github.com/user-attachments/assets/23480cf0-a245-4ea2-a9ad-a38becbdf1b4) Expected is a HTTP 302 redirect ### What version of Better Auth are you using? 1.1.8 ### Provide environment information ```bash - OS: Windows 11 - Next.js 15 running locally (localhost) in the Node.js (v20.2.0) runtime ``` ### Which area(s) are affected? (Select all that apply) Package ### Auth config (if applicable) ```typescript export const auth = betterAuth({ database: prismaAdapter(prisma, { provider: "mongodb", }), session: { expiresIn: 60 * 60 * 24 * 30, // 30 days updateAge: 60 * 60 * 24, // 1 day (every 1 day the session expiration is updated) cookieCache: { enabled: true, maxAge: 5 * 60 // Cache duration is 5 seconds } }, user: { additionalFields: { premium: { type: "boolean", required: false, }, }, changeEmail: { enabled: true, sendChangeEmailVerification: async ({ newEmail, url }) => { await sendEmail({ to: newEmail, subject: '[Development] Verify your email change', react: await VerifyUserEmail({name: "[this service is currently disabled]", id: "1", email: newEmail, url}), }) } } }, socialProviders: { linkedin: { clientId: process.env.LINKEDIN_CLIENT_ID as string, clientSecret: process.env.LINKEDIN_CLIENT_SECRET as string, }, google: { clientId: process.env.GOOGLE_CLIENT_ID as string, clientSecret: process.env.GOOGLE_CLIENT_SECRET as string, }, }, plugins: [openAPI(), admin({ impersonationSessionDuration: 60 * 60 * 24 * 7, // 7 days })], // api/auth/reference emailAndPassword: { enabled: true, requireEmailVerification: true, sendResetPassword: async ({ user, url }) => { await sendEmail({ to: user.email, subject: "Password Reset", react: await ResetPasswordEmail({name: user.name, id: user.id, email: user.email, url}), }); }, }, emailVerification: { sendOnSignUp: true, autoSignInAfterVerification: true, sendVerificationEmail: async ({ user, token }) => { const verificationUrl = `${process.env.BETTER_AUTH_URL}/api/auth/verify-email?token=${token}&callbackURL=${process.env.EMAIL_VERIFICATION_CALLBACK_URL}`; sendEmail({ to: user.email, subject: "Email Verification", react: await VerifyUserEmail({name: user.name, id: user.id, email: user.email, url: verificationUrl}), }); }, } } satisfies BetterAuthOptions); export type Session = typeof auth.$Infer.Session; ```
GiteaMirror added the bug label 2026-03-13 07:50:20 -05:00
Author
Owner

@dhruv-m1 commented on GitHub (Jan 8, 2025):

Can confirm that this also fails with the Generic OAuth Provider, with the following error during the token request:

ERROR [Better Auth]:  {
  error: 'invalid_client',
  error_description: 'Client authentication failed',
  status: 401,
  statusText: 'Unauthorized'
}

Also, found this additional context that might be useful:
LinkedIn Login - OpenIDC Issues

Turns out LinkedIn's OIDC implementation is not a standard one.

Edit: This has nothing to do with the issue.

@dhruv-m1 commented on GitHub (Jan 8, 2025): Can confirm that this also fails with the Generic OAuth Provider, with the following error during the token request: ``` ERROR [Better Auth]: { error: 'invalid_client', error_description: 'Client authentication failed', status: 401, statusText: 'Unauthorized' } ``` Also, found this additional context that might be useful: [LinkedIn Login - OpenIDC Issues](https://github.com/OpenIDC/mod_auth_openidc/discussions/1095) Turns out LinkedIn's OIDC implementation is not a standard one. **Edit: This has nothing to do with the issue.**
Author
Owner

@dhruv-m1 commented on GitHub (Jan 18, 2025):

For anyone looking for a workaround, the Generic OAuth Adapter can now be used as an alternative post commit 6dc8422, which fixed the issue of the PKCE option being ignored during the verification request (you must be on version 1.1.14+).

The following configuration can be used:

plugins: [genericOAuth({
    config: [
      {
        providerId: "linkedin",
        clientId: process.env.LINKEDIN_CLIENT_ID as string,
        clientSecret: process.env.LINKEDIN_CLIENT_SECRET as string,
        scopes: ["profile", "email", "openid"],
        discoveryUrl: "https://www.linkedin.com/oauth/.well-known/openid-configuration",
        pkce: false

      }
    ]
  }),

Make sure you update your callback URL on the Linkedin Developer dashboard to ${baseURL}/api/auth/oauth2/callback/:providerId

As for the actual LinkedIn provider, I believe the issue seems to be with the getUserInfo() function. Based on my initial investigation, verification request seems to take place properly.

@dhruv-m1 commented on GitHub (Jan 18, 2025): For anyone looking for a workaround, the [Generic OAuth Adapter](https://better-auth.vercel.app/docs/plugins/generic-oauth) can now be used as an alternative post commit 6dc8422, which fixed the issue of the PKCE option being ignored during the verification request (you must be on version 1.1.14+). The following configuration can be used: ```javascript plugins: [genericOAuth({ config: [ { providerId: "linkedin", clientId: process.env.LINKEDIN_CLIENT_ID as string, clientSecret: process.env.LINKEDIN_CLIENT_SECRET as string, scopes: ["profile", "email", "openid"], discoveryUrl: "https://www.linkedin.com/oauth/.well-known/openid-configuration", pkce: false } ] }), ``` Make sure you update your callback URL on the Linkedin Developer dashboard to `${baseURL}/api/auth/oauth2/callback/:providerId` As for the actual LinkedIn provider, I believe the issue seems to be with the [`getUserInfo()` function](https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/social-providers/linkedin.ts#L49). Based on my initial investigation, verification request seems to take place properly.
Author
Owner

@tsteckenborn commented on GitHub (Jan 26, 2025):

Yeah, also receiving

{
  error: 'invalid_client',
  error_description: 'Client authentication failed',
  status: 401,
  statusText: 'Unauthorized'
}

with LinkedIn Social Provider.

Image

@tsteckenborn commented on GitHub (Jan 26, 2025): Yeah, also receiving ``` { error: 'invalid_client', error_description: 'Client authentication failed', status: 401, statusText: 'Unauthorized' } ``` with LinkedIn Social Provider. ![Image](https://github.com/user-attachments/assets/d5b78f72-9533-4cf1-85d2-834676733447)
Author
Owner

@dosubot[bot] commented on GitHub (Jun 13, 2025):

Hi, @dhruv-m1. I'm Dosu, and I'm helping the better-auth team manage their backlog. I'm marking this issue as stale.

Issue Summary:

  • The issue involves an HTTP 500 error during LinkedIn authentication due to a socket connection timeout.
  • This problem occurs in version 1.1.8 on Windows 11 with Next.js 15 and Node.js v20.2.0.
  • You suggested using the Generic OAuth Adapter as a workaround, available from version 1.1.14+.
  • A potential issue with the getUserInfo() function in the LinkedIn provider was identified.
  • Another user, @tsteckenborn, confirmed experiencing a similar error.

Next Steps:

  • Please let us know if this issue is still relevant to the latest version of the better-auth repository. If so, you can keep the discussion open by commenting on the issue.
  • Otherwise, this issue will be automatically closed in 7 days.

Thank you for your understanding and contribution!

@dosubot[bot] commented on GitHub (Jun 13, 2025): Hi, @dhruv-m1. I'm [Dosu](https://dosu.dev), and I'm helping the better-auth team manage their backlog. I'm marking this issue as stale. **Issue Summary:** - The issue involves an HTTP 500 error during LinkedIn authentication due to a socket connection timeout. - This problem occurs in version 1.1.8 on Windows 11 with Next.js 15 and Node.js v20.2.0. - You suggested using the Generic OAuth Adapter as a workaround, available from version 1.1.14+. - A potential issue with the `getUserInfo()` function in the LinkedIn provider was identified. - Another user, @tsteckenborn, confirmed experiencing a similar error. **Next Steps:** - Please let us know if this issue is still relevant to the latest version of the better-auth repository. If so, you can keep the discussion open by commenting on the issue. - Otherwise, this issue will be automatically closed in 7 days. Thank you for your understanding and contribution!
Author
Owner

@kziemski commented on GitHub (Jun 16, 2025):

I believe this is still broken.

@kziemski commented on GitHub (Jun 16, 2025): I believe this is still broken.
Author
Owner

@frectonz commented on GitHub (Jul 15, 2025):

@kziemski @dhruv-m1

I wasn't able to reproduce the issue. Are you still facing the problem?

@frectonz commented on GitHub (Jul 15, 2025): @kziemski @dhruv-m1 I wasn't able to reproduce the issue. Are you still facing the problem?
Author
Owner

@frectonz commented on GitHub (Jul 15, 2025):

For reference, This the repo I used to try to reproduce the issue.

https://github.com/frectonz/better-auth-linkedin

@frectonz commented on GitHub (Jul 15, 2025): For reference, This the repo I used to try to reproduce the issue. https://github.com/frectonz/better-auth-linkedin
Author
Owner

@dhruv-m1 commented on GitHub (Jul 16, 2025):

@frectonz Thank you for looking into this, we are currently using the Generic OAuth Provider as a workaround.

I'll clone your repo later today and get back to you on whether I am still able to recreate the issue on my end.

My analysis back then was that something was going wrong in the getUserInfo() function as few accounts were able to login seamlessly, but most were not.

@dhruv-m1 commented on GitHub (Jul 16, 2025): @frectonz Thank you for looking into this, we are currently using the Generic OAuth Provider as a workaround. I'll clone your repo later today and get back to you on whether I am still able to recreate the issue on my end. My analysis back then was that something was going wrong in the [getUserInfo() function](https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/social-providers/linkedin.ts#L49) as few accounts were able to login seamlessly, but most were not.
Author
Owner

@dhruv-m1 commented on GitHub (Jul 20, 2025):

@frectonz Sorry for the delay in getting back to you.

I tested:

  • Using the code you shared (nice choice of stack btw 🙂)
  • Using the app (+ few accounts) with which we were facing this issue earlier (upgraded to the latest version of better-auth & replaced our workaround with the standard Linkedin provider)

Everything worked smoothly.

I think it would be safe to close this issue, unless @kziemski or @tsteckenborn are still able to recreate this issue with the latest version.

@frectonz - I'll take the responsibility to close the issue in 2 days if there is no objection/response.

Again, thanks for looking into this!

@dhruv-m1 commented on GitHub (Jul 20, 2025): @frectonz Sorry for the delay in getting back to you. I tested: - Using the code you shared (nice choice of stack btw 🙂) - Using the app (+ few accounts) with which we were facing this issue earlier (upgraded to the latest version of better-auth & replaced our workaround with the standard Linkedin provider) Everything worked smoothly. I think it would be safe to close this issue, unless @kziemski or @tsteckenborn are still able to recreate this issue with the latest version. @frectonz - I'll take the responsibility to close the issue in 2 days if there is no objection/response. Again, thanks for looking into this!
Author
Owner

@kziemski commented on GitHub (Jul 21, 2025):

Ya I'm fine with that as I was having your reported issue as well so if it's tested and works we should close it.

@kziemski commented on GitHub (Jul 21, 2025): Ya I'm fine with that as I was having your reported issue as well so if it's tested and works we should close it.
Author
Owner

@dhruv-m1 commented on GitHub (Jul 23, 2025):

Closing this issue as discussed

@dhruv-m1 commented on GitHub (Jul 23, 2025): Closing this issue as discussed
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#509