CLI authentication
A CLI needs browser-based sign-in without opening a local callback listener.
Better Auth · Const
deviceAuthorization enables RFC 8628 device authorization for limited-input clients. Devices obtain codes, users approve them in a browser, and devices poll for a session or OAuth token.
The plugin enables a device flow for clients with limited input. A device obtains a device code and user code, the user approves the request at the verification route, and the device polls until it receives a Better Auth session token or an OAuth token from the configured integration.
deviceAuthorization(options?)| Name | Type | Description |
|---|---|---|
options | object | Configuration object; the page demonstrates verificationUri, the route used for user verification. |
A Better Auth device-authorization plugin configuration used in betterAuth({ plugins: [...] }).
import { betterAuth } from "better-auth";
import { deviceAuthorization } from "better-auth/plugins";
export const auth = betterAuth({
plugins: [
deviceAuthorization({
verificationUri: "/device",
}),
],
});Adds the device-authorization plugin and sets the route where users verify a displayed user code.
A CLI needs browser-based sign-in without opening a local callback listener.
A smart TV or console cannot conveniently enter credentials.
An IoT device needs a user-approved Better Auth session.
A device client must receive OAuth access tokens.
It implements the OAuth 2.0 Device Authorization Grant for clients that have limited input. The client receives codes, the user authorizes in a browser, and the client polls until authorization completes.
Follow-up: How would you support OAuth access tokens instead of a Better Auth session? Compose device authorization with the OAuth Provider integration.
The device calls the device-code endpoint with a client ID to obtain a device code and user code. It then directs the user to the verification URI and polls the token endpoint.
Follow-up: What happens if the user has not approved yet? Continue polling according to the endpoint's pending response and stop when authorization succeeds or the flow expires.
Configure the plugin in the server's Better Auth configuration, optionally providing a verification URI such as /device. Add the corresponding client plugin when using the client-side API.
Follow-up: What application responsibility remains after configuration? Implement the verification and approval UI at the configured route.
deviceAuthorizationClientdevice.codedevice.tokenoauthProvideroauthDeviceAuthorization