16185 and 16630 language switching

This commit is contained in:
2025-06-13 12:58:38 +01:00
parent b4451dc7ee
commit 46876cdff6
8 changed files with 213 additions and 97 deletions
+29 -78
View File
@@ -14,80 +14,9 @@ import { PrismaAdapter } from "@next-auth/prisma-adapter";
import { PrismaClient } from "@prisma/client";
import NextAuth from "next-auth";
import EmailProvider from "next-auth/providers/email";
import { consoleLogger } from "../../../actions";
import { consoleLogger, getPreferredLanguage } from "../../../actions";
const prisma = new PrismaClient();
// For more information on each option (and a full list of options) go to
// https://next-auth.js.org/configuration/options
// export default NextAuth({
// // https://next-auth.js.org/configuration/providers
// providers: [
// EmailProvider({
// // server: {
// // host: process.env.EMAIL_SERVER_HOST,
// // port: process.env.EMAIL_SERVER_PORT,
// // auth: {
// // user: process.env.EMAIL_SERVER_USER,
// // pass: process.env.EMAIL_SERVER_PASSWORD,
// // },
// // },
// // from: process.env.EMAIL_FROM,
// maxAge: 10 * 60, // Magic links are valid for 10 min only
// async sendVerificationRequest({
// identifier: email,
// url,
// provider: { server, from },
// }) {
// const { host } = new URL(url);
// const templateId = "b1b5704b-9bb8-4deb-a75c-d887ca902661";
// const emailAddress = email;
// const personalisation = {
// "emailAddress": email,
// "signInLink": url,
// "linkExpiry": 10 * 60,
// };
// const reference = "PEDW-SIGNIN";
// var NotifyClient =
// require("notifications-node-client").NotifyClient;
// const notifyClient = new NotifyClient(
// process.env.NOTIFY_API_KEY
// );
// await notifyClient
// .sendEmail(templateId, emailAddress, {
// personalisation: personalisation,
// reference: reference,
// // emailReplyToId: emailReplyToId,
// })
// //.then((response) => console.log(response))
// .catch((error) => consoleLogger(err));
// },
// }),
// ],
// adapter: PrismaAdapter(prisma),
// secret: process.env.SECRET,
// session: {
// jwt: true,
// pages: {},
// theme: "dark",
// debug: true,
// },
// pages: {
// signIn: "/auth/signin",
// //signOut: "/auth/signout",
// error: "/auth/error", // Error code passed in query string as ?error=
// verifyRequest: "/auth/verify-request", // (used for check email message)
// newUser: "/account/register", // New users will be directed here on first sign in (leave the property out if not of interest)
// },
// callbacks: {
// session: async (session, user) => {
// //console.log(user);
// return Promise.resolve(session);
// },
// },
// });
const parseUrl = (url) => {
const defaultUrl = new URL(
@@ -169,7 +98,9 @@ const authOptions = (locale, req, res) => {
"&token=" +
searchParams.get("token") +
"&email=" +
encodeURIComponent(email)
encodeURIComponent(email) +
"&locale=" +
effectiveLocale
: url;
let formURL = appendParamsAndPathToNewUrl(url, newURL);
@@ -191,23 +122,43 @@ const authOptions = (locale, req, res) => {
const { host, port, protocol, token, searchParams } =
new URL(url);
const baseDomain = `${protocol}//${host}`;
const templateId = "b1b5704b-9bb8-4deb-a75c-d887ca902661";
const templateIdcy = "0614ce53-cd5f-421f-a1a5-8c8486a9113a";
const emailAddress = email;
const queryLocale = req?.query?.locale || req?.body?.locale;
const cookieLocale = req.cookies["pedw_locale"];
let locale = queryLocale || cookieLocale || "en";
console.log(
"============================================================\n",
"verification url----: " + url + "\n",
"============================================================\n"
);
const prefLang = await getPreferredLanguage(emailAddress);
const registeredPrefLanguage =
prefLang.value.length > 0
? prefLang.value[0].pinswg_preferredlanguage ===
846040000
? "cy"
: "en"
: "en";
const effectiveLocale = registeredPrefLanguage ?? locale;
let newURL =
locale == "cy"
? process.env.CY_API_ROOT +
effectiveLocale == "cy"
? baseDomain +
"/api/auth/callback/email?callbackUrl=" +
encodeURIComponent(process.env.CY_API_ROOT) +
encodeURIComponent(baseDomain + "/cy") +
"&token=" +
searchParams.get("token") +
"&email=" +
encodeURIComponent(email)
encodeURIComponent(email) +
"&locale=" +
effectiveLocale
: url;
let formURL = appendParamsAndPathToNewUrl(url, newURL);
@@ -240,7 +191,7 @@ const authOptions = (locale, req, res) => {
await notifyClient
.sendEmail(
locale == "cy" ? templateIdcy : templateId,
effectiveLocale == "cy" ? templateIdcy : templateId,
emailAddress,
{
personalisation: personalisation,