set logged in from GovGateway Code
This commit is contained in:
+52
-2
@@ -1,8 +1,12 @@
|
|||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
|
import { hashAPIPath } from "./";
|
||||||
|
|
||||||
const configEndpoint = process.env.GG_PROVIDER_CONFIG_ENDPOINT;
|
const configEndpoint = process.env.GG_PROVIDER_CONFIG_ENDPOINT;
|
||||||
const ggClientID = process.env.GG_ClIENT_ID;
|
const ggClientID = process.env.GG_ClIENT_ID;
|
||||||
const ggClientSecret = process.env.GG_CLIENT_SECRET;
|
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 = () => {
|
export const getProviderConfig = () => {
|
||||||
return axios
|
return axios
|
||||||
@@ -36,11 +40,57 @@ export const getGGToken = (tokenURL, authCode, redirectUri) => {
|
|||||||
.post(tokenURL, tokenBody, tokenConfig)
|
.post(tokenURL, tokenBody, tokenConfig)
|
||||||
.then((res) => res.data)
|
.then((res) => res.data)
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
console.log("error :", error.response);
|
// if (error.response) {
|
||||||
// return error;
|
// 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 = {
|
var Base64 = {
|
||||||
_keyStr:
|
_keyStr:
|
||||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
|
||||||
|
|||||||
+1
-1
@@ -94,7 +94,7 @@ const azureHeadersPaged = (access_token) => {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
const hashAPIPath = (queryPath) => {
|
export const hashAPIPath = (queryPath) => {
|
||||||
var hashPath =
|
var hashPath =
|
||||||
queryPath.indexOf("/api/proxy/") == -1
|
queryPath.indexOf("/api/proxy/") == -1
|
||||||
? queryPath
|
? queryPath
|
||||||
|
|||||||
@@ -82,10 +82,11 @@ let PersonalDetails = (props) => {
|
|||||||
let errorsobj = {};
|
let errorsobj = {};
|
||||||
|
|
||||||
const required = (value) => (value ? undefined : "Is required");
|
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) =>
|
const email = (value) =>
|
||||||
value && !/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(value)
|
value && !emailRegex.test(value) ? "Invalid email address" : undefined;
|
||||||
? "Invalid email address"
|
|
||||||
: undefined;
|
|
||||||
const phoneNumber = (value) =>
|
const phoneNumber = (value) =>
|
||||||
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(
|
!/^(?:(?:\(?(?: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
@@ -14,18 +14,42 @@ import { wrapper } from "../store/store";
|
|||||||
import { useRouter } from "next/router";
|
import { useRouter } from "next/router";
|
||||||
import useTranslation from "next-translate/useTranslation";
|
import useTranslation from "next-translate/useTranslation";
|
||||||
import { getIncidents } from "../actions";
|
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 { getGovGatewayConfig } from "../store/govgateway/action";
|
||||||
|
import {
|
||||||
|
setLoggedInUserId,
|
||||||
|
setAccountDetails,
|
||||||
|
} from "../store/accountDetails/action";
|
||||||
|
import { parseCookies, setCookie, destroyCookie } from "nookies";
|
||||||
|
|
||||||
const Home = (props) => {
|
const Home = (props) => {
|
||||||
const { footerLinks, pages, showLogin } = props;
|
const { footerLinks, pages, showLogin, loggedInUserId } = props;
|
||||||
let { t, lang } = useTranslation();
|
let { t, lang } = useTranslation();
|
||||||
|
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const { locale } = router;
|
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 (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<Head>
|
<Head>
|
||||||
@@ -81,7 +105,11 @@ const Home = (props) => {
|
|||||||
<Header courseName="Appeals Casework Portal" />
|
<Header courseName="Appeals Casework Portal" />
|
||||||
<div className="govuk-width-container">
|
<div className="govuk-width-container">
|
||||||
<ServiceBanner />
|
<ServiceBanner />
|
||||||
<Main pages={pages} showLogin={showLogin} />
|
<Main
|
||||||
|
pages={pages}
|
||||||
|
showLogin={showLogin}
|
||||||
|
loggedInUserId={loggedInUserId}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
<Footer footerLinks={footerLinks} />
|
<Footer footerLinks={footerLinks} />
|
||||||
</div>
|
</div>
|
||||||
@@ -93,6 +121,9 @@ export const getServerSideProps = wrapper.getServerSideProps(
|
|||||||
(store) => async (ctx) => {
|
(store) => async (ctx) => {
|
||||||
const { query, req, res } = ctx;
|
const { query, req, res } = ctx;
|
||||||
|
|
||||||
|
var relayToken = await getToken();
|
||||||
|
relayToken = relayToken.access_token;
|
||||||
|
|
||||||
console.log(query.code);
|
console.log(query.code);
|
||||||
|
|
||||||
const showLoginCheck = process.env.SHOWLOGIN || false;
|
const showLoginCheck = process.env.SHOWLOGIN || false;
|
||||||
@@ -106,17 +137,33 @@ export const getServerSideProps = wrapper.getServerSideProps(
|
|||||||
|
|
||||||
store.dispatch(getGovGatewayConfig(providerConfig));
|
store.dispatch(getGovGatewayConfig(providerConfig));
|
||||||
|
|
||||||
|
let ggUserInfo = {};
|
||||||
|
let portalUserObj = {};
|
||||||
|
|
||||||
if (hasLoginCode == true) {
|
if (hasLoginCode == true) {
|
||||||
let ggToken = await getGGToken(
|
const ggToken = await getGGToken(
|
||||||
providerConfig.token_endpoint,
|
providerConfig.token_endpoint,
|
||||||
query.code,
|
query.code,
|
||||||
process.env.GG_REDIRECT_URI
|
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 {
|
return {
|
||||||
props: { showLogin: showLoginCheck },
|
props: { showLogin: showLoginCheck, loggedInUserId: portalUserObj },
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user