Files
t5de-hub/apps/web/app/consent/consent-form.tsx
T
dhkatz 096ee32fa5
Pull request / title (pull_request) Successful in 1s
CI / test (pull_request) Successful in 1m30s
CI / image (apps/api/Dockerfile, t5de-hub-api) (pull_request) Skipped
CI / image (apps/web/Dockerfile, t5de-hub-web) (pull_request) Skipped
The hub as an OpenID Connect provider for T5DE Music
2026-11-19 01:22:33 -08:00

46 lines
1.7 KiB
TypeScript

"use client";
// Allow or deny a site's request. The provider's client plugin sends the page's signed request (oauth_query) along,
// and answers with where to go next: back to the site, with a code or with access_denied.
import { oauthProviderClient } from "@better-auth/oauth-provider/client";
import { createAuthClient } from "better-auth/react";
import { useState } from "react";
import { ErrorText } from "@/components/forms";
import { Button } from "@/components/ui/button";
const consentClient = createAuthClient({
baseURL: typeof window === "undefined" ? undefined : window.location.origin,
plugins: [oauthProviderClient()],
});
export function ConsentForm() {
const [pending, setPending] = useState<"allow" | "deny" | null>(null);
const [error, setError] = useState("");
async function answer(accept: boolean) {
setPending(accept ? "allow" : "deny");
setError("");
const { data, error } = await consentClient.oauth2.consent({ accept });
const url = data && typeof data === "object" && "url" in data ? (data as { url?: string }).url : undefined;
if (error || !url) {
setError(error?.message ?? "That didn't work. Start again from the site.");
setPending(null);
return;
}
window.location.assign(url);
}
return (
<div className="space-y-3">
<div className="flex gap-2">
<Button type="button" disabled={pending !== null} onClick={() => void answer(true)}>
{pending === "allow" ? "Allowing…" : "Allow"}
</Button>
<Button type="button" variant="outline" disabled={pending !== null} onClick={() => void answer(false)}>
{pending === "deny" ? "Denying…" : "Deny"}
</Button>
</div>
{error && <ErrorText>{error}</ErrorText>}
</div>
);
}