16185 and 16630 language switching
This commit is contained in:
@@ -7,6 +7,8 @@ import CookieBanner from "../../components/cookieBanner";
|
||||
import Footer from "../../components/footer";
|
||||
import Header from "../../components/header";
|
||||
import ServiceBanner from "../../components/servicebanner";
|
||||
import { destroyCookie, setCookie } from "nookies";
|
||||
import { useEffect } from "react";
|
||||
|
||||
import { getCsrfToken } from "next-auth/react";
|
||||
|
||||
@@ -16,6 +18,14 @@ const Error = (props) => {
|
||||
|
||||
const { error } = useRouter().query;
|
||||
|
||||
useEffect(() => {
|
||||
window.localStorage.clear(),
|
||||
destroyCookie(null, "next-auth.csrf-token", { path: "/" }),
|
||||
destroyCookie(null, "next-auth.callback-url", { path: "/" }),
|
||||
destroyCookie(null, "pedw_locale", { path: "/" }),
|
||||
destroyCookie(null, "pinsUser", { path: "/" });
|
||||
}, []);
|
||||
|
||||
const errors = {
|
||||
Signin: t("auth:auth-error-Signin-label"),
|
||||
OAuthSignin: t("auth:auth-error-OAuthSignin-label"),
|
||||
|
||||
@@ -7,6 +7,8 @@ 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();
|
||||
@@ -25,6 +27,35 @@ const SignIn = (props) => {
|
||||
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();
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user