16185 and 16630 language switching
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
/**
|
||||
* @swagger
|
||||
* /api/endpoint/getemailaccountcheck_api:
|
||||
* get:
|
||||
* tags: [Login]
|
||||
* description: Returns portal login
|
||||
* summary: Phase 2
|
||||
* parameters:
|
||||
* - name: emailAddress
|
||||
* in: query
|
||||
* required: true
|
||||
* schema:
|
||||
* type: string
|
||||
* responses:
|
||||
* 200:
|
||||
* description: hello world
|
||||
*/
|
||||
import axios from "axios";
|
||||
import CryptoJS from "crypto-js";
|
||||
import _ from "lodash";
|
||||
import {
|
||||
azureHeaders,
|
||||
consoleLogger,
|
||||
getToken,
|
||||
getPreferredLanguage,
|
||||
} from "../../../actions";
|
||||
|
||||
const WORDKEY = process.env.HASHKEY;
|
||||
|
||||
const WEBAPI_URL =
|
||||
process.env.RELAY_ROOT ||
|
||||
"https://dev-pedw-ns.servicebus.windows.net/dev-pedw-hc/";
|
||||
|
||||
const hashAPIPath = (queryPath) => {
|
||||
var hashlink = CryptoJS.HmacSHA256(
|
||||
queryPath,
|
||||
CryptoJS.enc.Hex.parse(WORDKEY)
|
||||
);
|
||||
hashlink = hashlink.toString(CryptoJS.enc.Hex);
|
||||
return (queryPath.indexOf("?") > -1 ? "&hash=" : "?hash=") + hashlink;
|
||||
};
|
||||
|
||||
export default async function ApiProxy(req, res) {
|
||||
var emailAddress = req.query.emailAddress;
|
||||
var token = await getToken();
|
||||
|
||||
var queryUrl =
|
||||
"contacts?$filter=emailaddress1 eq '" +
|
||||
emailAddress +
|
||||
"'&$count=true&$select=pinswg_preferredlanguage,contactid";
|
||||
|
||||
console.log(queryUrl);
|
||||
// var queryUrl = "contacts/?$count=true&$select=emailaddress1, contactid";
|
||||
|
||||
var apiResponse = _.isEmpty(req.query)
|
||||
? res.status(400).json()
|
||||
: typeof emailAddress != "undefined" && emailAddress.length > 0
|
||||
? axios
|
||||
.get(
|
||||
WEBAPI_URL + queryUrl + hashAPIPath(queryUrl),
|
||||
azureHeaders(token.access_token)
|
||||
)
|
||||
.then(({ data }) => {
|
||||
res.status(200).json(data);
|
||||
})
|
||||
.catch((error) => {
|
||||
consoleLogger(error);
|
||||
res.status(400).json(error);
|
||||
})
|
||||
: res.status(400).json();
|
||||
|
||||
return apiResponse;
|
||||
}
|
||||
Reference in New Issue
Block a user