/* eslint-disable @typescript-eslint/camelcase */ import { encodeCrock, stringToBytes } from "@gnu-taler/taler-util"; import { h, VNode } from "preact"; import { useLayoutEffect, useRef, useState } from "preact/hooks"; import { TextInput } from "../../components/fields/TextInput"; import { authMethods, KnownAuthMethods } from "./authMethod"; import { AnastasisClientFrame } from "./index"; interface Props { providerType?: KnownAuthMethods; cancel: () => void; } export function AddingProviderScreen({ providerType, cancel }: Props): VNode { const [providerURL, setProviderURL] = useState(""); const [error, setError] = useState() const providerLabel = providerType ? authMethods[providerType].label : undefined function testProvider(): void { setError(undefined) fetch(`${providerURL}/config`) .then(r => r.json().catch(d => ({}))) .then(r => { if (!("methods" in r) || !Array.isArray(r.methods)) { setError("This provider doesn't have authentication method. Check the provider URL") return; } if (!providerLabel) { setError("") return } let found = false for (let i = 0; i < r.methods.length && !found; i++) { found = r.methods[i].type !== providerType } if (!found) { setError(`This provider does not support authentication method ${providerLabel}`) } }) .catch(e => { setError(`There was an error testing this provider, try another one. ${e.message}`) }) } function addProvider(): void { // addAuthMethod({ // authentication_method: { // type: "sms", // instructions: `SMS to ${providerURL}`, // challenge: encodeCrock(stringToBytes(providerURL)), // }, // }); } const inputRef = useRef(null); useLayoutEffect(() => { inputRef.current?.focus(); }, []); let errors = !providerURL ? 'Add provider URL' : undefined try { new URL(providerURL) } catch { errors = 'Check the URL' } if (!!error && !errors) { errors = error } return (

Add a provider url {errors}

{!!error &&

{error}

} {error === "" &&

This provider worked!

}
); }