46 lines
1.7 KiB
TypeScript
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>
|
|
);
|
|
}
|