import _ from "lodash"; import { useState, useEffect, useRef } from "react"; import Head from "next/head"; import Header from "../components/header"; import ServiceBanner from "../components/servicebanner"; import CookieBanner from "../components/cookieBanner"; import Breadcrumbs from "../components/breadcrumbs"; import Main from "../components/main"; import Footer from "../components/footer"; import Errorpage from "../components/errorpage"; import styles from "../styles/Home.module.css"; import { useSelector, shallowEqual, connect } from "react-redux"; import { wrapper } from "../store/store"; import { useRouter } from "next/router"; import useTranslation from "next-translate/useTranslation"; import { getIncidents, hashString } from "../actions"; import CryptoJS from "crypto-js"; import HmacSHA256 from "crypto-js/hmac-sha256"; 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 WORDKEY = process.env.NEXT_PUBLIC_HASHKEY; const Home = (props) => { const { footerLinks, pages, showLogin, loggedInUserId, hasGGCode, loginEmailStr, loggedInUserEmail, } = 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); console.log("hashed email str", loginEmailStr); _.isEmpty(loggedInUserId) != false && !hasGGCode ? console.log("no log in no ggcode") : _.has(loggedInUserId, "value") ? _.isEmpty(loggedInUserId.value) ? router.replace({ pathname: router.locale == "cy" ? "/account/register" : "/account/register", query: { id: encodeURI(loggedInUserEmail), }, }) : (setCookie(null, "pinsUser", loggedInUserId.value[0].contactid, { path: "/", expires: expDate, }), router.replace({ pathname: router.locale == "cy" ? "/fymhorth" : "/myportal", })) : console.log("no value in user id"); return (
{t("common:service-name")} {/* Language by default is EN, if multilingual site this will need updated */} {/* If they have a Twitter Handle, include the meta details below */}
{hasGGCode ? ( <>

Logging in

Please wait...

) : ( <>
)}
); }; export const getServerSideProps = wrapper.getServerSideProps( (store) => async (ctx) => { const { query, req, res } = ctx; var relayToken = await getToken(); relayToken = relayToken.access_token; console.log("gg code:", query.code); const showLoginCheck = process.env.SHOWLOGIN || false; const hasLoginCode = typeof query.code != "undefined"; let providerConfig = await getProviderConfig(); console.log( "endpoint: ", providerConfig.token_endpoint, "\n", "hasloginCode:", hasLoginCode, "\n", "redirect:", process.env.GG_REDIRECT_URI ); store.dispatch(getGovGatewayConfig(providerConfig)); let ggUserInfo = {}; let portalUserObj = {}; let loginEmailStr = ""; if (hasLoginCode == true) { const ggToken = await getGGToken( providerConfig.token_endpoint, query.code, process.env.GG_REDIRECT_URI ); _.has(ggToken, "access_token") ? ((ggUserInfo = await getUserInfo( providerConfig.userinfo_endpoint, ggToken.access_token )), (loginEmailStr = hashString(ggUserInfo.data.email)), (portalUserObj = await getPortalLogin( ggUserInfo.data.email, relayToken )), console.log("loggin user stuff: ", portalUserObj), store.dispatch(setAccountDetails(portalUserObj))) : console.log( ggToken.status + " " + ggToken.data.error_description ); } return { props: { showLogin: showLoginCheck, hasGGCode: hasLoginCode, loggedInUserId: portalUserObj, loggedInUserEmail: loginEmailStr, GG_REDIRECT_URI: process.env.GG_REDIRECT_URI, GG_CLIENT_ID: process.env.GG_CLIENT_ID, }, }; } ); const mapStateToProps = (state) => { return { currentView: state.currentView, search: state.search, searchResultsObj: state.searchResultsObj, formData: state.formData, appealType: state.appealType, form: state.form, myCases: state.myCases, watchedCases: state.watchedCases, myRepresentations: state.myRepresentations, awaitingSubmission: state.awaitingSubmission, govGateway: state.govGateway, }; }; export default connect(mapStateToProps)(Home);