Rack Integration
Better Auth Ruby is Rack-first. BetterAuth.auth(...) returns an object that can
be called directly by Rack.
Gemfile
gem "better_auth"Auth Instance
require "better_auth"
AUTH = BetterAuth.auth(
secret: ENV.fetch("BETTER_AUTH_SECRET"),
base_url: ENV.fetch("BETTER_AUTH_URL", "http://localhost:3000"),
email_and_password: {
enabled: true
},
database: BetterAuth::Adapters::Postgres.new(
url: ENV.fetch("DATABASE_URL")
)
)Mount In Rack
require_relative "config/auth"
map "/api/auth" do
run AUTH
endVerify the mount:
curl http://localhost:3000/api/auth/ok{ "ok": true }Sinatra
Sinatra apps should use the better_auth-sinatra adapter for helpers and
Better Auth-specific Rake tasks. See the Sinatra integration guide for the full
setup.
Server-Side API Calls
You can also call endpoints from Ruby code:
result = AUTH.api.sign_in_email(
body: {
email: "ada@example.com",
password: "password123"
}
)When you need to forward cookies, request a Rack response:
status, headers, body = AUTH.api.sign_in_email(
{
body: {
email: "ada@example.com",
password: "password123"
},
as_response: true
}
)