import { getCsrfToken } from "next-auth/react";
import useTranslation from "next-translate/useTranslation";
import Head from "next/head";
import { useState } from "react";
import Breadcrumbs from "../../components/breadcrumbs";
import CookieBanner from "../../components/cookieBanner";
import Footer from "../../components/footer";
import Header from "../../components/header";
import ServiceBanner from "../../components/servicebanner";
import { getPreferredLanguage } from "../../actions";
import { setCookie } from "nookies";
const SignIn = (props) => {
let { t, lang } = useTranslation();
const { footerLinks, pages, csrfToken, callbackUrl } = props;
let formURL =
new URL(window.location.href).protocol +
"//" +
new URL(window.location.href).hostname +
":" +
new URL(window.location.href).port +
"/api/auth/signin/email";
const [buttonDisabled, setButtonDisabled] = useState(false);
const handleSubmit = async (event) => {
event.preventDefault();
setButtonDisabled(true);
const email = event.target.email.value;
try {
const res = await getPreferredLanguage(encodeURIComponent(email));
const data = await res;
const userLang =
data.value.length > 0
? data.value[0].pinswg_preferredlanguage === 846040000
? "cy"
: "en"
: "en";
// Modify callbackUrl hidden input to add/update lang param
const url = new URL(event.target.callbackUrl.value);
url.searchParams.set("locale", userLang);
setCookie(null, "pedw_locale", userLang, {
path: "/",
maxAge: 60 * 60 * 24 * 365,
});
event.target.callbackUrl.value = url.toString();
} catch (error) {
console.error("Failed to get preferred language", error);
}
// Submit form after updating callbackUrl
event.target.submit();
};
return (
{t("auth:auth-page-title")} - {t("common:service-name")}
);
};
export async function getServerSideProps(context) {
if (process.env.SHOWLOGIN === "false" || process.env.SHOWLOGIN === false) {
return {
redirect: {
destination: "/status",
permanent: false,
},
};
}
const csrfToken = await getCsrfToken(context);
console.log(
"\n//////////////////////\n",
"Sign in callbackurl: " + context.query.callbackUrl,
context.locale,
"\n//////////////////////\n",
);
let formURL = context.req.headers.host;
//console.log("formUrl:", formURL);
formURL = typeof formURL == "undefined" ? null : formURL;
let protocol = "http";
if (context.req.headers["x-forwarded-proto"]) {
protocol = context.req.headers["x-forwarded-proto"];
}
// Otherwise, check if the connection is encrypted (for https)
else if (context.req.connection.encrypted) {
protocol = "https";
}
formURL = protocol + "://" + formURL;
console.log("Protocol:", protocol);
console.log("locale:", context.locale);
const appendParamsAndPathToNewUrl = (fromUrl, toUrl) => {
const fromUrlObj = new URL(fromUrl); // Parse the source URL
const params = fromUrlObj.searchParams; // Extract the query parameters
const toUrlObj = new URL(toUrl); // Parse the new domain URL
toUrlObj.pathname = fromUrlObj.pathname; // Copy the path from the source URL
// Append the query parameters to the new URL
params.forEach((value, key) =>
toUrlObj.searchParams.append(key, value),
);
return toUrlObj.toString(); // Return the full new URL as a string
};
typeof context.query.callbackUrl != "undefined" &&
(formURL = appendParamsAndPathToNewUrl(
context.query.callbackUrl,
formURL,
));
//formURL = appendParamsAndPathToNewUrl(context.query.callbackUrl, formURL);
const url = new URL(formURL); // base URL
const params = new URLSearchParams(url.search);
params.append("locale", context.locale);
url.search = params.toString();
console.log("========== callback: ", url.toString());
return {
props: { csrfToken: csrfToken, callbackUrl: url.toString() },
};
}
export default SignIn;