1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
/* 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<string | undefined>()
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<HTMLInputElement>(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 (
<AnastasisClientFrame hideNav
title={!providerLabel ? `Backup: Adding a provider` : `Backup: Adding a ${providerLabel} provider`}
hideNext={errors}>
<div>
<p>
Add a provider url {errors}
</p>
<div class="container">
<TextInput
label="Provider URL"
placeholder="https://provider.com"
grabFocus
bind={[providerURL, setProviderURL]} />
</div>
{!!error && <p class="block has-text-danger">{error}</p>}
{error === "" && <p class="block has-text-success">This provider worked!</p>}
<div style={{ marginTop: '2em', display: 'flex', justifyContent: 'space-between' }}>
<button class="button" onClick={testProvider}>TEST</button>
</div>
<div style={{ marginTop: '2em', display: 'flex', justifyContent: 'space-between' }}>
<button class="button" onClick={cancel}>Cancel</button>
<span data-tooltip={errors}>
<button class="button is-info" disabled={errors !== undefined} onClick={addProvider}>Add</button>
</span>
</div>
</div>
</AnastasisClientFrame>
);
}
|