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 { setCookie } from "nookies";
const SignIn = (props) => {
let { t, lang } = useTranslation();
const { footerLinks, pages, csrfToken, callbackUrl } = props;
const [buttonDisabled, setButtonDisabled] = useState(false);
const handleSubmit = async (event) => {
event.preventDefault();
setButtonDisabled(true);
const currentLocale = lang || "en";
const url = new URL(event.target.callbackUrl.value);
url.searchParams.set("locale", currentLocale);
setCookie(null, "pedw_locale", currentLocale, {
path: "/",
maxAge: 60 * 60 * 24 * 365
});
event.target.callbackUrl.value = url.toString();
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);
let formURL = context.req.headers.host;
formURL = typeof formURL == "undefined" ? null : formURL;
let protocol = "http";
if (context.req.headers["x-forwarded-proto"]) {
protocol = context.req.headers["x-forwarded-proto"];
} else if (context.req.connection.encrypted) {
protocol = "https";
}
formURL = protocol + "://" + formURL;
const appendParamsAndPathToNewUrl = (fromUrl, toUrl) => {
const fromUrlObj = new URL(fromUrl);
const params = fromUrlObj.searchParams;
const toUrlObj = new URL(toUrl);
toUrlObj.pathname = fromUrlObj.pathname;
params.forEach((value, key) =>
toUrlObj.searchParams.append(key, value)
);
return toUrlObj.toString();
};
typeof context.query.callbackUrl != "undefined" &&
(formURL = appendParamsAndPathToNewUrl(
context.query.callbackUrl,
formURL
));
const url = new URL(formURL);
url.searchParams.set("locale", context.locale || "en");
return {
props: { csrfToken, callbackUrl: url.toString() }
};
}
export default SignIn;