Get your LinkedIn credentials
To use LinkedIn sign in, you need a client ID and client secret. You can get them from the LinkedIn Developer Portal.
Make sure to set the redirect URL to http://localhost:3000/api/auth/callback/linkedin for local development. For production, you should set it to the URL of your application. If you change the base path of the auth routes, you should update the redirect URL accordingly.
In the LinkedIn portal under products you need the Sign In with LinkedIn using OpenID Connect product.
There are some different Guides here: Authorization Code Flow (3-legged OAuth) (Outdated) Sign In with LinkedIn using OpenID Connect
Configure the provider
To configure the provider, you need to import the provider and pass it to the social_providers option of the auth instance.
require "better_auth"
auth = BetterAuth.auth(
secret: ENV.fetch("BETTER_AUTH_SECRET"),
base_url: ENV.fetch("BETTER_AUTH_URL", "http://localhost:3000"),
social_providers: {
linkedin: BetterAuth::SocialProviders.linkedin(
client_id: ENV.fetch("LINKEDIN_CLIENT_ID"),
client_secret: ENV.fetch("LINKEDIN_CLIENT_SECRET")
)
}
)LinkedIn's email and email_verified claims are documented as optional and may be absent when no confirmed email is associated with the member. See Handling Providers Without Email for the recommended map_profile_to_user fallback.
Sign In with LinkedIn
To sign in with LinkedIn, call auth.api.sign_in_social on your Ruby auth instance. The endpoint body takes the following properties:
provider: The provider to use. It should be set tolinkedin.
response = auth.api.sign_in_social(
body: {
provider: "linkedin",
callback_url: "/dashboard",
error_callback_url: "/login",
disable_redirect: true
}
)
redirect_url = response.fetch(:url)