set logged in from GovGateway Code

This commit is contained in:
2021-12-07 08:14:15 +00:00
parent 8bf7fd973e
commit bcfef0ec91
4 changed files with 111 additions and 13 deletions
+52 -2
View File
@@ -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+/=",
+1 -1
View File
@@ -94,7 +94,7 @@ const azureHeadersPaged = (access_token) => {
};
};
const hashAPIPath = (queryPath) => {
export const hashAPIPath = (queryPath) => {
var hashPath =
queryPath.indexOf("/api/proxy/") == -1
? queryPath
+4 -3
View File
@@ -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(
+54 -7
View File
@@ -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 (
<div>
<Head>
@@ -81,7 +105,11 @@ const Home = (props) => {
<Header courseName="Appeals Casework Portal" />
<div className="govuk-width-container">
<ServiceBanner />
<Main pages={pages} showLogin={showLogin} />
<Main
pages={pages}
showLogin={showLogin}
loggedInUserId={loggedInUserId}
/>
</div>
<Footer footerLinks={footerLinks} />
</div>
@@ -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 },
};
}
);