From bcfef0ec911a10dd6e65b59a2e862044d7dab036 Mon Sep 17 00:00:00 2001 From: rdbsolutions Date: Tue, 7 Dec 2021 08:14:15 +0000 Subject: [PATCH] set logged in from GovGateway Code --- actions/govgateway.js | 54 +++++++++++++++++++++++- actions/index.js | 2 +- components/account/personaldetails.js | 7 +-- pages/index.js | 61 ++++++++++++++++++++++++--- 4 files changed, 111 insertions(+), 13 deletions(-) diff --git a/actions/govgateway.js b/actions/govgateway.js index daace495..f774a739 100644 --- a/actions/govgateway.js +++ b/actions/govgateway.js @@ -1,8 +1,12 @@ import axios from "axios"; +import { hashAPIPath } from "./"; const configEndpoint = process.env.GG_PROVIDER_CONFIG_ENDPOINT; const ggClientID = process.env.GG_ClIENT_ID; const ggClientSecret = process.env.GG_CLIENT_SECRET; +const WEBAPI_URL = + process.env.RELAY_ROOT || + "https://dev-pedw-ns.servicebus.windows.net/dev-pedw-hc/"; export const getProviderConfig = () => { return axios @@ -36,11 +40,57 @@ export const getGGToken = (tokenURL, authCode, redirectUri) => { .post(tokenURL, tokenBody, tokenConfig) .then((res) => res.data) .catch((error) => { - console.log("error :", error.response); - // return error; + // if (error.response) { + // console.log(error.response.data); + // console.log(error.response.status); + // console.log(error.response.headers); + // } + return error.response; }); }; +export const getUserInfo = (userInfoURL, authToken) => { + const tokenConfig = { + headers: { + "Authorization": "Bearer " + authToken, + }, + }; + + return axios + .get(userInfoURL, tokenConfig) + .then((res) => res) + .catch((error) => { + //console.log(`Error: ${err?.response?.data}`); + //return error; + }); +}; + +export const getPortalLogin = (emailAddress, token) => { + var queryUrl = + "contacts?$filter=emailaddress1 eq '" + + emailAddress + + "'&$count=true&$select=emailaddress1,contactid,pinswg_custom_password,yomifullname,firstname,lastname"; + return axios + .get(WEBAPI_URL + queryUrl + hashAPIPath(queryUrl), azureHeaders(token)) + .then((res) => res.data) + .catch((error) => { + console.log("thiserror", error); + }); +}; + +const azureHeaders = (access_token) => { + return { + headers: { + "OData-MaxVersion": "4.0", + "OData-Version": "4.0", + "Accept": "application/json;odata.metadata=none", + "Prefer": 'odata.include-annotations="*",return=representation', + "Content-Type": "application/json", + "Authorization": "Bearer " + access_token, + }, + }; +}; + var Base64 = { _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", diff --git a/actions/index.js b/actions/index.js index b5d8a3a1..b986db75 100644 --- a/actions/index.js +++ b/actions/index.js @@ -94,7 +94,7 @@ const azureHeadersPaged = (access_token) => { }; }; -const hashAPIPath = (queryPath) => { +export const hashAPIPath = (queryPath) => { var hashPath = queryPath.indexOf("/api/proxy/") == -1 ? queryPath diff --git a/components/account/personaldetails.js b/components/account/personaldetails.js index 70f449db..85fd9da6 100644 --- a/components/account/personaldetails.js +++ b/components/account/personaldetails.js @@ -82,10 +82,11 @@ let PersonalDetails = (props) => { let errorsobj = {}; const required = (value) => (value ? undefined : "Is required"); + const emailRegex = + /(?:[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/gi; + const email = (value) => - value && !/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(value) - ? "Invalid email address" - : undefined; + value && !emailRegex.test(value) ? "Invalid email address" : undefined; const phoneNumber = (value) => value && !/^(?:(?:\(?(?:0(?:0|11)\)?[\s-]?\(?|\+)44\)?[\s-]?(?:\(?0\)?[\s-]?)?)|(?:\(?0))(?:(?:\d{5}\)?[\s-]?\d{4,5})|(?:\d{4}\)?[\s-]?(?:\d{5}|\d{3}[\s-]?\d{3}))|(?:\d{3}\)?[\s-]?\d{3}[\s-]?\d{3,4})|(?:\d{2}\)?[\s-]?\d{4}[\s-]?\d{4}))(?:[\s-]?(?:x|ext\.?|\#)\d{3,4})?$/i.test( diff --git a/pages/index.js b/pages/index.js index 17d4a250..aa1dd7fb 100644 --- a/pages/index.js +++ b/pages/index.js @@ -14,18 +14,42 @@ import { wrapper } from "../store/store"; import { useRouter } from "next/router"; import useTranslation from "next-translate/useTranslation"; import { getIncidents } from "../actions"; -import { parseCookies } from "nookies"; -import { getProviderConfig, getGGToken } from "../actions/govgateway"; +import { + getProviderConfig, + getGGToken, + getUserInfo, + getPortalLogin, +} from "../actions/govgateway"; +import { getToken } from "../actions"; import { getGovGatewayConfig } from "../store/govgateway/action"; +import { + setLoggedInUserId, + setAccountDetails, +} from "../store/accountDetails/action"; +import { parseCookies, setCookie, destroyCookie } from "nookies"; const Home = (props) => { - const { footerLinks, pages, showLogin } = props; + const { footerLinks, pages, showLogin, loggedInUserId } = props; let { t, lang } = useTranslation(); const router = useRouter(); const { locale } = router; + let now = new Date(); + let expDate = new Date(now); + expDate.setDate(now.getDate() + 1); + + _.isEmpty(loggedInUserId) == false && + (setCookie(null, "pinsUser", loggedInUserId.value[0].contactid, { + path: "/", + expires: expDate, + }), + router.replace({ + pathname: router.locale == "cy" ? "/fymhorth" : "/myportal", + shallow: true, + })); + return (
@@ -81,7 +105,11 @@ const Home = (props) => {
-
+
@@ -93,6 +121,9 @@ export const getServerSideProps = wrapper.getServerSideProps( (store) => async (ctx) => { const { query, req, res } = ctx; + var relayToken = await getToken(); + relayToken = relayToken.access_token; + console.log(query.code); const showLoginCheck = process.env.SHOWLOGIN || false; @@ -106,17 +137,33 @@ export const getServerSideProps = wrapper.getServerSideProps( store.dispatch(getGovGatewayConfig(providerConfig)); + let ggUserInfo = {}; + let portalUserObj = {}; + if (hasLoginCode == true) { - let ggToken = await getGGToken( + const ggToken = await getGGToken( providerConfig.token_endpoint, query.code, process.env.GG_REDIRECT_URI ); - console.log(ggToken); + + _.has(ggToken, "access_token") + ? ((ggUserInfo = await getUserInfo( + providerConfig.userinfo_endpoint, + ggToken.access_token + )), + (portalUserObj = await getPortalLogin( + ggUserInfo.data.email, + relayToken + )), + store.dispatch(setAccountDetails(portalUserObj))) + : console.log( + ggToken.status + " " + ggToken.data.error_description + ); } return { - props: { showLogin: showLoginCheck }, + props: { showLogin: showLoginCheck, loggedInUserId: portalUserObj }, }; } );