UNPKG

@fine-dev/fine-js

Version:

Javascript client for Fine BaaS

77 lines 3.94 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { useCallback, useEffect, useState } from "react"; export function createOAuthApprovalHook(baseURL) { /** * Custom hook that encapsulates the OAuth approval logic */ return function useOAuthApproval() { const [data, setData] = useState(null); const [status, setStatus] = useState(null); const [error, setError] = useState(null); useEffect(() => { const fetchAuthorizationData = () => __awaiter(this, void 0, void 0, function* () { try { setStatus("loading"); const url = `${baseURL}/authorize${window.location.search}`; const response = yield fetch(url, { credentials: "include" }); if (!response.ok) throw new Error(`Failed to fetch authorization data: ${response.statusText}`); const authData = (yield response.json()); if ("redirectUrl" in authData) return (window.location.href = authData.redirectUrl); // Add default values to the client object const client = (authData.client || (authData.client = {})); client.clientUri || (client.clientUri = ""); client.policyUri || (client.policyUri = ""); client.tosUri || (client.tosUri = ""); client.redirectUris || (client.redirectUris = []); client.contacts || (client.contacts = []); client.clientName || (client.clientName = "Unknown MCP Client"); setData(authData); } catch (err) { setError(err instanceof Error ? err.message : "Unknown error occurred"); console.error("Error fetching authorization data:", err); } finally { setStatus(null); } }); fetchAuthorizationData(); }, []); const handleSubmit = useCallback((e) => __awaiter(this, void 0, void 0, function* () { e.preventDefault(); if (!data) return; try { setStatus("submitting"); const formData = new FormData(); formData.append("state", btoa(JSON.stringify(data.state))); const response = yield fetch(`${baseURL}/authorize`, { method: "POST", body: formData, credentials: "include" }); if (!response.ok) throw new Error(`Authorization failed: ${response.statusText}`); const { redirectUrl } = yield response.json(); window.location.href = redirectUrl; } catch (err) { setError(err instanceof Error ? err.message : "Unknown error occurred"); console.error("Error during authorization:", err); setStatus(null); } }), [data]); return Object.assign({ status, error, handleSubmit }, data === null || data === void 0 ? void 0 : data.client); }; } //# sourceMappingURL=oauth.js.map