Dub
Track Dub leads on signup and link Dub OAuth accounts.
This page documents the current Ruby port behavior. Ruby uses snake_case option names and auth.api method names; HTTP paths and JSON keys keep the upstream wire shape where implemented.
Installation
The Dub plugin ships with the core better_auth gem. The Dub SDK remains optional, so applications that do not use Dub do not install it.
gem "better_auth"
gem "dub"Configure
require "better_auth"
require "dub"
Models = OpenApiSDK::Models
dub = OpenApiSDK::Dub.new(
security: Models::Shared::Security.new(
token: ENV.fetch("DUB_API_KEY")
)
)
auth = BetterAuth.auth(
secret: ENV.fetch("BETTER_AUTH_SECRET"),
base_url: ENV.fetch("BETTER_AUTH_URL", "http://localhost:3000"),
email_and_password: { enabled: true },
plugins: [
BetterAuth::Plugins.dub(dub_client: dub)
]
)dub_client can be any object that responds to track.lead. The official dub gem is used only when your application requires it.
Lead Tracking
When a signup request includes a dub_id cookie, the plugin tracks a Dub lead after the user is created. The default event name is "Sign Up".
BetterAuth::Plugins.dub(
dub_client: dub,
lead_event_name: "Created Account"
)Disable automatic lead tracking:
BetterAuth::Plugins.dub(
dub_client: dub,
disable_lead_tracking: true
)Use custom_lead_track to replace the default Dub call:
BetterAuth::Plugins.dub(
dub_client: dub,
custom_lead_track: ->(user, ctx) {
Analytics.track_signup(user.fetch("id"), ctx.get_cookie("dub_id"))
}
)OAuth Linking
Configure Dub OAuth credentials to enable account linking:
BetterAuth::Plugins.dub(
dub_client: dub,
oauth: {
client_id: ENV.fetch("DUB_OAUTH_CLIENT_ID"),
client_secret: ENV.fetch("DUB_OAUTH_CLIENT_SECRET")
}
)From Ruby, call the generated API method with session cookies:
link = auth.api.dub_link(
headers: { "cookie" => request.env["HTTP_COOKIE"] },
body: { callbackURL: "/dashboard" }
)The response contains the Dub authorization URL:
link[:url]
link[:redirect] # => trueRoutes
| Method | Path | Ruby API method |
|---|---|---|
POST | /dub/link | auth.api.dub_link |
GET/POST | /oauth2/callback/:providerId | auth.api.dub_o_auth2_callback |
Options
Current Ruby options accepted by BetterAuth::Plugins.dub:
dub_clientdisable_lead_trackinglead_event_namecustom_lead_trackoauthoauth.client_idoauth.client_secretoauth.pkce
Support Notes
- Dub OAuth uses
https://app.dub.co/oauth/authorizeandhttps://api.dub.co/oauth/token. - OAuth PKCE is enabled by default. Set
oauth: { pkce: false }to disable it. - The browser-only
dubAnalyticsClienthelper from upstream is not ported. Frontends should call the HTTP endpoint with the user's session cookies. - The plugin is based on the upstream
@dub/better-authpackage and adapted to the Ruby plugin and database hook system.