updated authentication for welsh url

This commit is contained in:
2025-04-09 10:24:44 +01:00
parent 0c11bd846b
commit c874bb4f59
9 changed files with 167 additions and 110 deletions
+78 -20
View File
@@ -118,6 +118,24 @@ const parseUrl = (url) => {
};
};
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 only the parameters that don't already exist in the destination URL
params.forEach((value, key) => {
if (!toUrlObj.searchParams.has(key)) {
// Check if the parameter already exists
toUrlObj.searchParams.append(key, value);
}
});
return toUrlObj.toString(); // Return the full new URL as a string
};
const authOptions = (locale, req, res) => {
//console.log(req.query.callbackUrl);
// let newURL = parseUrl(
@@ -125,7 +143,9 @@ const authOptions = (locale, req, res) => {
// ? "https://" + process.env.I18N_DOMAIN + ":3000"
// : process.env.NEXTAUTH_URL
// );
// console.log("which locale", locale);
// console.log("which referer", req.body);
// console.log("cookie locale", req.cookies["pedw_locale"]);
return {
providers: [
{
@@ -140,9 +160,29 @@ const authOptions = (locale, req, res) => {
"verification url API: " + url + "\n",
"============================================================\n"
);
let newURL =
locale == "cy"
? process.env.CY_API_ROOT +
"/api/auth/callback/email?callbackUrl=" +
encodeURIComponent(process.env.CY_API_ROOT) +
"&token=" +
searchParams.get("token") +
"&email=" +
encodeURIComponent(email)
: url;
let formURL = appendParamsAndPathToNewUrl(url, newURL);
console.log(
"============================================================\n",
"new verification url----: " + formURL + "\n",
"============================================================\n"
);
return res
.writeHead(200, { "Content-Type": "application/json" })
.json({ url: url });
.json({ url: formURL });
},
},
EmailProvider({
@@ -156,7 +196,7 @@ const authOptions = (locale, req, res) => {
const emailAddress = email;
console.log(
"============================================================\n",
"verification url: " + url + "\n",
"verification url----: " + url + "\n",
"============================================================\n"
);
let newURL =
@@ -168,11 +208,19 @@ const authOptions = (locale, req, res) => {
searchParams.get("token") +
"&email=" +
encodeURIComponent(email)
: process.env.NEXTAUTH_URL;
: url;
let formURL = appendParamsAndPathToNewUrl(url, newURL);
console.log(
"============================================================\n",
"new verification url----: " + formURL + "\n",
"============================================================\n"
);
const personalisation = {
"emailAddress": email,
"signInLink": url,
"signInLink": formURL,
// locale == "cy"
// ? url.split("&token")[0] +
// "/cy&token" +
@@ -228,7 +276,6 @@ const authOptions = (locale, req, res) => {
},
pages: {
signIn: (locale == "cy" ? "/cy" : "") + "/auth/signin",
//signOut: "/logout/signout",
error: (locale == "cy" ? "/cy" : "") + "/auth/error", // Error code passed in query string as ?error=
verifyRequest:
(locale == "cy" ? "/cy" : "") + "/auth/verify-request", // (used for check email message)
@@ -239,21 +286,32 @@ const authOptions = (locale, req, res) => {
console.log("callback in auth", session, user);
return Promise.resolve(session);
},
// redirect({ url, baseUrl }) {
// debugger;
// console.log("baseurl:", url, baseUrl);
// // Allows relative callback URLs
// if (url.startsWith("/")) return `${baseUrl}${url}`;
// // Allows callback URLs on the same origin
// else if (new URL(url).origin === baseUrl) return url;
// // let newURL =
// // locale == "cy"
// // ? process.env.CY_API_ROOT
// // : process.env.NEXTAUTH_URL;
// //return baseUrl;
// // return newURL;
// },
redirect({ url, baseUrl }) {
//debugger;
console.log("baseurl:", url, baseUrl);
// Allows relative callback URLs
if (url.startsWith("/")) return `${baseUrl}${url}`;
// Allows callback URLs on the same origin
else if (new URL(url).origin === baseUrl) return url;
const originalUrl = url;
let newUrl =
locale == "cy"
? process.env.CY_API_ROOT
: process.env.NEXTAUTH_URL;
const updatedUrl = appendParamsAndPathToNewUrl(
originalUrl,
newUrl
);
console.log(updatedUrl);
//return baseUrl;
console.log("the url:", updatedUrl);
return updatedUrl;
},
},
};
};