mirror of
https://github.com/better-auth/better-auth.git
synced 2026-05-29 18:36:34 -05:00
fix: device authorization interval (#4243)
This commit is contained in:
@@ -2,6 +2,7 @@ import { describe, expect, it, vi } from "vitest";
|
||||
import { getTestInstance } from "../../test-utils/test-instance";
|
||||
import { $deviceAuthorizationOptionsSchema, deviceAuthorization } from ".";
|
||||
import { deviceAuthorizationClient } from "./client";
|
||||
import type { DeviceCode } from "./schema";
|
||||
|
||||
describe("device authorization plugin input validation", () => {
|
||||
it("basic validation", async () => {
|
||||
@@ -470,6 +471,40 @@ describe("device authorization flow", async () => {
|
||||
});
|
||||
|
||||
describe("device authorization with custom options", async () => {
|
||||
it("should correctly store interval as milliseconds in database", async () => {
|
||||
const { auth, client, db } = await getTestInstance({
|
||||
plugins: [
|
||||
deviceAuthorization({
|
||||
interval: "5s",
|
||||
}),
|
||||
],
|
||||
});
|
||||
|
||||
const response = await auth.api.deviceCode({
|
||||
body: {
|
||||
client_id: "test-client",
|
||||
},
|
||||
});
|
||||
|
||||
// Response should return interval in seconds
|
||||
expect(response.interval).toBe(5);
|
||||
|
||||
// Check that the interval is stored as milliseconds in the database
|
||||
const deviceCodeRecord: DeviceCode | null = await db.findOne({
|
||||
model: "deviceCode",
|
||||
where: [
|
||||
{
|
||||
field: "deviceCode",
|
||||
value: response.device_code,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
// Should be stored as 5000 milliseconds, not "5s" string
|
||||
expect(deviceCodeRecord?.pollingInterval).toBe(5000);
|
||||
expect(typeof deviceCodeRecord?.pollingInterval).toBe("number");
|
||||
});
|
||||
|
||||
it("should use custom code generators", async () => {
|
||||
const customDeviceCode = "custom-device-code-12345";
|
||||
const customUserCode = "CUSTOM12";
|
||||
|
||||
@@ -293,7 +293,7 @@ Follow [rfc8628#section-3.2](https://datatracker.ietf.org/doc/html/rfc8628#secti
|
||||
userCode,
|
||||
expiresAt,
|
||||
status: "pending",
|
||||
pollingInterval: opts.interval,
|
||||
pollingInterval: ms(opts.interval),
|
||||
clientId: ctx.body.client_id,
|
||||
scope: ctx.body.scope,
|
||||
},
|
||||
@@ -473,10 +473,7 @@ Follow [rfc8628#section-3.4](https://datatracker.ietf.org/doc/html/rfc8628#secti
|
||||
) {
|
||||
const timeSinceLastPoll =
|
||||
Date.now() - new Date(deviceCodeRecord.lastPolledAt).getTime();
|
||||
const minInterval =
|
||||
typeof deviceCodeRecord.pollingInterval === "string"
|
||||
? ms(deviceCodeRecord.pollingInterval as MSStringValue)
|
||||
: deviceCodeRecord.pollingInterval;
|
||||
const minInterval = deviceCodeRecord.pollingInterval;
|
||||
|
||||
if (timeSinceLastPoll < minInterval) {
|
||||
throw new APIError("BAD_REQUEST", {
|
||||
|
||||
Reference in New Issue
Block a user