Fix connection testing for realm e-mail (#1303)

This commit is contained in:
Jon Koops 2021-10-06 22:54:33 +02:00 committed by GitHub
parent 10d7c35b81
commit f273ae70db
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -60,7 +60,7 @@ export const RealmSettingsEmailTab = ({
const authenticationEnabled = useWatch({ const authenticationEnabled = useWatch({
control, control,
name: "smtpServer.authentication", name: "smtpServer.authentication",
defaultValue: {}, defaultValue: "",
}); });
const handleModalToggle = () => { const handleModalToggle = () => {
@ -104,10 +104,31 @@ export const RealmSettingsEmailTab = ({
}; };
const testConnection = async () => { const testConnection = async () => {
const serverSettings = { ...getValues()["smtpServer"] };
// Code below uses defensive coding as the server configuration uses an ambiguous record type.
if (typeof serverSettings.port === "string") {
serverSettings.port = Number(serverSettings.port);
}
if (typeof serverSettings.ssl === "string") {
serverSettings.ssl = serverSettings.ssl === true.toString();
}
if (typeof serverSettings.starttls === "string") {
serverSettings.starttls = serverSettings.starttls === true.toString();
}
// For some reason the API wants a duplicate field for the authentication status.
// Somebody thought this was a good idea, so here we are.
if (serverSettings.authentication === true.toString()) {
serverSettings.auth = true;
}
try { try {
await adminClient.realms.testSMTPConnection( await adminClient.realms.testSMTPConnection(
{ realm: realm.realm! }, { realm: realm.realm! },
getValues()["smtpServer"] || {} serverSettings
); );
addAlert(t("testConnectionSuccess"), AlertVariant.success); addAlert(t("testConnectionSuccess"), AlertVariant.success);
} catch (error) { } catch (error) {
@ -311,7 +332,7 @@ export const RealmSettingsEmailTab = ({
<Controller <Controller
name="smtpServer.authentication" name="smtpServer.authentication"
control={control} control={control}
defaultValue={{}} defaultValue=""
render={({ onChange, value }) => ( render={({ onChange, value }) => (
<Switch <Switch
id="kc-authentication-switch" id="kc-authentication-switch"