22081 welsh signin flow
This commit is contained in:
+20
-72
@@ -7,55 +7,28 @@ import CookieBanner from "../../components/cookieBanner";
|
||||
import Footer from "../../components/footer";
|
||||
import Header from "../../components/header";
|
||||
import ServiceBanner from "../../components/servicebanner";
|
||||
import { getPreferredLanguage } from "../../actions/services/accountService";
|
||||
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;
|
||||
const currentLocale = lang || "en";
|
||||
const url = new URL(event.target.callbackUrl.value);
|
||||
url.searchParams.set("locale", currentLocale);
|
||||
|
||||
try {
|
||||
const res = await getPreferredLanguage(encodeURIComponent(email));
|
||||
const data = await res;
|
||||
setCookie(null, "pedw_locale", currentLocale, {
|
||||
path: "/",
|
||||
maxAge: 60 * 60 * 24 * 365
|
||||
});
|
||||
|
||||
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.callbackUrl.value = url.toString();
|
||||
event.target.submit();
|
||||
};
|
||||
|
||||
@@ -97,11 +70,7 @@ const SignIn = (props) => {
|
||||
action="/api/auth/signin/email"
|
||||
onSubmit={handleSubmit}
|
||||
>
|
||||
<div
|
||||
className={
|
||||
"govuk-form-group "
|
||||
}
|
||||
>
|
||||
<div className="govuk-form-group ">
|
||||
<input
|
||||
className="govuk-input"
|
||||
name="csrfToken"
|
||||
@@ -129,7 +98,7 @@ const SignIn = (props) => {
|
||||
</label>
|
||||
|
||||
<input
|
||||
className="govuk-input govuk-!-width-three-quarters"
|
||||
className="govuk-input govuk-!-width-three-quarters"
|
||||
type="email"
|
||||
id="email"
|
||||
name="email"
|
||||
@@ -175,44 +144,31 @@ export async function getServerSideProps(context) {
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
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"];
|
||||
}
|
||||
// Otherwise, check if the connection is encrypted (for https)
|
||||
else if (context.req.connection.encrypted) {
|
||||
} 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 fromUrlObj = new URL(fromUrl);
|
||||
const params = fromUrlObj.searchParams;
|
||||
|
||||
const toUrlObj = new URL(toUrl); // Parse the new domain URL
|
||||
toUrlObj.pathname = fromUrlObj.pathname; // Copy the path from the source URL
|
||||
const toUrlObj = new URL(toUrl);
|
||||
toUrlObj.pathname = fromUrlObj.pathname;
|
||||
|
||||
// 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
|
||||
return toUrlObj.toString();
|
||||
};
|
||||
|
||||
typeof context.query.callbackUrl != "undefined" &&
|
||||
@@ -221,19 +177,11 @@ export async function getServerSideProps(context) {
|
||||
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());
|
||||
const url = new URL(formURL);
|
||||
url.searchParams.set("locale", context.locale || "en");
|
||||
|
||||
return {
|
||||
props: { csrfToken: csrfToken, callbackUrl: url.toString() }
|
||||
props: { csrfToken, callbackUrl: url.toString() }
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user