/*
This file is part of GNU Taler
(C) 2022 Taler Systems S.A.
GNU Taler is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation; either version 3, or (at your option) any later version.
GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
GNU Taler; see the file COPYING. If not, see
*/
import { Fragment, h, VNode } from "preact";
import { route } from "preact-router";
import { StateUpdater, useState } from "preact/hooks";
import { PageStateType, usePageContext } from "../../context/pageState.js";
import { useTranslationContext } from "../../context/translation.js";
import { BackendStateType, useBackendState } from "../../hooks/backend.js";
import { bankUiSettings } from "../../settings.js";
import { getBankBackendBaseUrl, undefinedIfEmpty } from "../../utils.js";
import { BankFrame } from "./BankFrame.js";
import { ShowInputErrorLabel } from "./ShowInputErrorLabel.js";
export function RegistrationPage(): VNode {
const { i18n } = useTranslationContext();
if (!bankUiSettings.allowRegistrations) {
return (
{i18n.str`Currently, the bank is not accepting new registrations!`}
{i18n.str`Welcome to ${bankUiSettings.bankName}!`}
);
}
/**
* This function requests /register.
*
* This function is responsible to change two states:
* the backend's (to store the login credentials) and
* the page's (to indicate a successful login or a problem).
*/
async function registrationCall(
req: { username: string; password: string },
/**
* FIXME: figure out if the two following
* functions can be retrieved somewhat from
* the state.
*/
backendStateSetter: StateUpdater,
pageStateSetter: StateUpdater,
): Promise {
let baseUrl = getBankBackendBaseUrl();
/**
* If the base URL doesn't end with slash and the path
* is not empty, then the concatenation made by URL()
* drops the last path element.
*/
if (!baseUrl.endsWith("/")) baseUrl += "/";
const headers = new Headers();
headers.append("Content-Type", "application/json");
const url = new URL("access-api/testing/register", baseUrl);
let res: Response;
try {
res = await fetch(url.href, {
method: "POST",
body: JSON.stringify({
username: req.username,
password: req.password,
}),
headers,
});
} catch (error) {
console.log(
`Could not POST new registration to the bank (${url.href})`,
error,
);
pageStateSetter((prevState) => ({
...prevState,
error: {
title: `Registration failed, please report`,
debug: JSON.stringify(error),
},
}));
return;
}
if (!res.ok) {
const response = await res.json();
if (res.status === 409) {
pageStateSetter((prevState) => ({
...prevState,
error: {
title: `That username is already taken`,
debug: JSON.stringify(response),
},
}));
} else {
pageStateSetter((prevState) => ({
...prevState,
error: {
title: `New registration gave response error`,
debug: JSON.stringify(response),
},
}));
}
} else {
// registration was ok
pageStateSetter((prevState) => ({
...prevState,
isLoggedIn: true,
}));
backendStateSetter((prevState) => ({
...prevState,
url: baseUrl,
username: req.username,
password: req.password,
}));
route("/account");
}
}