Skip to content

Commit b1fc0ff

Browse files
Merge pull request #90 from datum-cloud/fix/device-auth-already-handled
fix: treat already-handled device authorization as success
2 parents c95cf26 + 15d853b commit b1fc0ff

1 file changed

Lines changed: 25 additions & 6 deletions

File tree

apps/login/src/lib/server/device.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,29 @@ export async function completeDeviceAuthorization(
1111
const _headers = await headers();
1212
const { serviceUrl } = getServiceUrlFromHeaders(_headers);
1313

14-
// without the session, device auth request is denied
15-
return authorizeOrDenyDeviceAuthorization({
16-
serviceUrl,
17-
deviceAuthorizationId,
18-
session,
19-
});
14+
try {
15+
// without the session, device auth request is denied
16+
return await authorizeOrDenyDeviceAuthorization({
17+
serviceUrl,
18+
deviceAuthorizationId,
19+
session,
20+
});
21+
} catch (error) {
22+
// The /signedin page completes the device flow during a Server Component
23+
// render, which the App Router may evaluate more than once per navigation
24+
// (RSC refetch, prefetch, refresh, retries). The first call authorizes the
25+
// grant successfully; subsequent calls fail with FAILED_PRECONDITION
26+
// ("Device Authorization Request has already been handled"). Treat that as
27+
// success so the user isn't shown a spurious error after a working login.
28+
if (
29+
error &&
30+
typeof error === "object" &&
31+
"code" in error &&
32+
error.code === 9 /* FAILED_PRECONDITION */
33+
) {
34+
return {};
35+
}
36+
37+
throw error;
38+
}
2039
}

0 commit comments

Comments
 (0)