Spotify

Get your Spotify Credentials

To use Spotify sign in, you need a client ID and client secret. You can get them from the Spotify Developer Portal.

Important: Spotify no longer supports localhost as a redirect URI. You must use 127.0.0.1 for local development.

Make sure to set the redirect URL to http://127.0.0.1:3000/api/auth/callback/spotify in your Spotify Dashboard.

Consequently, ensure you access your local app via http://127.0.0.1:3000 (not localhost:3000) so the browser URL matches the redirect URI exactly.

For production, you should set it to the URL of your application (must be HTTPS). If you change the base path of the auth routes, you should update the redirect URL accordingly.

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.

You must also ensure your environment variables use the correct loopback IP to match the redirect URI. Update your .env file:

.env
BETTER_AUTH_URL=http://127.0.0.1:3000
config/auth.rb
require "better_auth"

auth = BetterAuth.auth(
secret: ENV.fetch("BETTER_AUTH_SECRET"),
base_url: ENV.fetch("BETTER_AUTH_URL", "http://localhost:3000"),
social_providers: {
spotify: BetterAuth::SocialProviders.spotify(
  client_id: ENV.fetch("SPOTIFY_CLIENT_ID"),
  client_secret: ENV.fetch("SPOTIFY_CLIENT_SECRET")
)
}
)

Sign In with Spotify

To sign in with Spotify, 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 to spotify.
server.rb
response = auth.api.sign_in_social(
body: {
provider: "spotify",
callback_url: "/dashboard",
error_callback_url: "/login",
disable_redirect: true
}
)

redirect_url = response.fetch(:url)

On this page