Salesforce
Get your Salesforce Credentials
- Log into your Salesforce org (Production or Developer Edition)
- Navigate to Setup > App Manager
- Click New Connected App
- Fill in the basic information:
- Connected App Name: Your app name
- API Name: Auto-generated from app name
- Contact Email: Your email address
- Enable OAuth Settings:
- Check Enable OAuth Settings
- Set Callback URL to your redirect URI (e.g.,
http://localhost:3000/api/auth/callback/salesforcefor development) - Select Required OAuth Scopes:
- Access your basic information (id)
- Access your identity URL service (openid)
- Access your email address (email)
- Perform requests on your behalf at any time (refresh_token, offline_access)
- Enable Require Proof Key for Code Exchange (PKCE) (required)
- Save and note your Consumer Key (Client ID) and Consumer Secret (Client Secret)
- For development, you can use
http://localhost:3000URLs, but production requires HTTPS - The callback URL must exactly match what's configured in Better Auth
- PKCE (Proof Key for Code Exchange) is required by Salesforce and is automatically handled by the provider
For sandbox testing, you can create the Connected App in your sandbox org, or use the same Connected App but specify environment: "sandbox" in the provider configuration.
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: {
salesforce: BetterAuth::SocialProviders.salesforce(
client_id: ENV.fetch("SALESFORCE_CLIENT_ID"),
client_secret: ENV.fetch("SALESFORCE_CLIENT_SECRET"),
environment: "production"
)
}
)Configuration Options
client_id: Your Connected App's Consumer Keyclient_secret: Your Connected App's Consumer Secretenvironment:"production"(default) or"sandbox"login_url: Custom My Domain URL (withouthttps://) - overrides environment settingredirect_uri: Override the auto-generated redirect URI if needed
Advanced Configuration
require "better_auth"
auth = BetterAuth.auth(
secret: ENV.fetch("BETTER_AUTH_SECRET"),
base_url: ENV.fetch("BETTER_AUTH_URL", "http://localhost:3000"),
social_providers: {
salesforce: BetterAuth::SocialProviders.salesforce(
client_id: ENV.fetch("SALESFORCE_CLIENT_ID"),
client_secret: ENV.fetch("SALESFORCE_CLIENT_SECRET"),
environment: "sandbox",
login_url: "my-company.my.salesforce.com",
redirect_uri: "http://localhost:3000/api/auth/callback/salesforce"
)
}
)- Use
environment: "sandbox"for testing with Salesforce sandbox orgs - The
login_urloption is useful for organizations with My Domain enabled - The
redirect_urioption helps resolve redirect URI mismatch errors
Environment Variables
Add the following environment variables to your .env.local file:
SALESFORCE_CLIENT_ID=your_consumer_key_here
SALESFORCE_CLIENT_SECRET=your_consumer_secret_here
BETTER_AUTH_URL=http://localhost:3000 # Important for redirect URI generationFor production:
SALESFORCE_CLIENT_ID=your_consumer_key_here
SALESFORCE_CLIENT_SECRET=your_consumer_secret_here
BETTER_AUTH_URL=https://yourdomain.comSign In with Salesforce
To sign in with Salesforce, 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 tosalesforce.
response = auth.api.sign_in_social(
body: {
provider: "salesforce",
callback_url: "/dashboard",
error_callback_url: "/login",
disable_redirect: true
}
)
redirect_url = response.fetch(:url)Troubleshooting
Redirect URI Mismatch Error
If you encounter a redirect_uri_mismatch error:
- Check Callback URL: Ensure the Callback URL in your Salesforce Connected App exactly matches your Better Auth callback URL
- Protocol: Make sure you're using the same protocol (
http://vshttps://) - Port: Verify the port number matches (e.g.,
:3000) - Override if needed: Use the
redirect_urioption to explicitly set the redirect URI
require "better_auth"
auth = BetterAuth.auth(
secret: ENV.fetch("BETTER_AUTH_SECRET"),
base_url: ENV.fetch("BETTER_AUTH_URL", "http://localhost:3000"),
social_providers: {
salesforce: BetterAuth::SocialProviders.salesforce(
client_id: ENV.fetch("SALESFORCE_CLIENT_ID"),
client_secret: ENV.fetch("SALESFORCE_CLIENT_SECRET"),
redirect_uri: "http://localhost:3000/api/auth/callback/salesforce"
)
}
)Environment Issues
- Production: Use
environment: "production"(default) withlogin.salesforce.com - Sandbox: Use
environment: "sandbox"withtest.salesforce.com - My Domain: Use
login_url: "yourcompany.my.salesforce.com"for custom domains
PKCE Requirements
Salesforce requires PKCE (Proof Key for Code Exchange) which is automatically handled by this provider. Make sure PKCE is enabled in your Connected App settings.
The default scopes requested are openid, email, and profile. The provider will automatically include the id scope for accessing basic user information.