Merged PR 2318: auth stabilisation: extract shared myportal auth guard helper

auth stabilisation: extract shared myportal auth guard helper

ntroduces a small shared SSR helper (resolveMyPortalAuthContext) to standardize common myportal auth/session guards (session presence, session user identity, and pinsUser cookie) with preserved reason-coded diagnostics and signin redirect behavior. Migrates exactly two loaders (pages/myportal/searchresults.js, pages/myportal/addresssearchresults.js) to use the helper while keeping loader-specific UPSTREAM_FAILURE and CONTACT_LOOKUP_FAILED logic unchanged.

Related work items: #23020
This commit is contained in:
Robert Bond
2026-05-14 10:38:45 +00:00
parent 4fb62a6773
commit c2ae6964f9
5 changed files with 224 additions and 203 deletions
+60 -96
View File
@@ -31,8 +31,8 @@ import {
setWatchedCasesDetails
} from "../../store/watchedCases/action";
import { getSession } from "next-auth/react";
import TimeOut from "../../components/timeout";
import { resolveMyPortalAuthContext } from "../../lib/auth/resolveMyPortalAuthContext";
import {
setAccountDetails,
setContainerID,
@@ -81,20 +81,37 @@ export const getServerSideProps = wrapper.getServerSideProps(
getIP(req);
console.log("query-", query);
const { cookies } = req;
let thisSession = await getSession(ctx);
const showReps = process.env.SHOWREPRESENTATIONS || false;
const showLoginCheck = process.env.SHOWLOGIN || false;
store.dispatch(setShowReps(showReps, showLoginCheck));
if (!thisSession) {
const authContext = await resolveMyPortalAuthContext(ctx, {
loggerName: "MyPortalSearchResultsAuthGuard",
noSessionMessage:
"myportal searchresults loader missing session; redirecting to signin",
noSessionUserMessage:
"myportal searchresults loader missing session user identity; redirecting to signin",
noPinsUserMessage:
"myportal searchresults loader missing pinsUser cookie; redirecting to signin"
});
if (!authContext.ok) {
return { redirect: authContext.redirect };
}
const thisSession = authContext.session;
let loggedInUser;
try {
const portalLogin = await getPortalLogin(thisSession.user.email);
loggedInUser = portalLogin?.value?.[0]?.contactid;
} catch (error) {
consoleLogger({
name: "MyPortalSearchResultsAuthGuard",
reasonCode: "NO_SESSION",
reasonCode: "UPSTREAM_FAILURE",
message:
"myportal searchresults loader missing session; redirecting to signin"
"myportal searchresults loader portal login lookup failed; redirecting to signin",
error: error?.message
});
return {
redirect: {
@@ -102,95 +119,42 @@ export const getServerSideProps = wrapper.getServerSideProps(
permanent: false
}
};
} else {
if (!thisSession?.user?.id || !thisSession?.user?.email) {
consoleLogger({
name: "MyPortalSearchResultsAuthGuard",
reasonCode: "NO_SESSION_USER",
message:
"myportal searchresults loader missing session user identity; redirecting to signin",
hasSessionUserId: !!thisSession?.user?.id,
hasSessionUserEmail: !!thisSession?.user?.email
});
return {
redirect: {
destination: "/auth/signin",
permanent: false
}
};
}
if (!cookies?.pinsUser) {
consoleLogger({
name: "MyPortalSearchResultsAuthGuard",
reasonCode: "NO_PINSUSER_COOKIE",
message:
"myportal searchresults loader missing pinsUser cookie; redirecting to signin"
});
return {
redirect: {
destination: "/auth/signin",
permanent: false
}
};
}
let loggedInUser;
try {
const portalLogin = await getPortalLogin(
thisSession.user.email
);
loggedInUser = portalLogin?.value?.[0]?.contactid;
} catch (error) {
consoleLogger({
name: "MyPortalSearchResultsAuthGuard",
reasonCode: "UPSTREAM_FAILURE",
message:
"myportal searchresults loader portal login lookup failed; redirecting to signin",
error: error?.message
});
return {
redirect: {
destination: "/auth/signin",
permanent: false
}
};
}
if (!loggedInUser) {
consoleLogger({
name: "MyPortalSearchResultsAuthGuard",
reasonCode: "CONTACT_LOOKUP_FAILED",
message:
"myportal searchresults loader missing CRM contact id; redirecting to signin"
});
return {
redirect: {
destination: "/auth/signin",
permanent: false
}
};
}
//console.log("sssss", searchResultsObj);
const watchedCases = await getWatchedCases(loggedInUser);
//console.log("sssss", loggedInUser);
const [accountDetails, watchedCasesDetails] = await Promise.all([
getPersonalAccount(loggedInUser),
getDetails(watchedCases, "myWatchedCases")
]);
store.dispatch(setAccountDetails(accountDetails));
store.dispatch(setSearch(query.q));
store.dispatch(setWatchedCases(watchedCases));
store.dispatch(setWatchedCasesDetails(watchedCasesDetails));
store.dispatch(setLoggedInUserId(loggedInUser));
thisSession != false &&
store.dispatch(setContainerID(thisSession.user.id));
}
if (!loggedInUser) {
consoleLogger({
name: "MyPortalSearchResultsAuthGuard",
reasonCode: "CONTACT_LOOKUP_FAILED",
message:
"myportal searchresults loader missing CRM contact id; redirecting to signin"
});
return {
redirect: {
destination: "/auth/signin",
permanent: false
}
};
}
//console.log("sssss", searchResultsObj);
const watchedCases = await getWatchedCases(loggedInUser);
//console.log("sssss", loggedInUser);
const [accountDetails, watchedCasesDetails] = await Promise.all([
getPersonalAccount(loggedInUser),
getDetails(watchedCases, "myWatchedCases")
]);
store.dispatch(setAccountDetails(accountDetails));
store.dispatch(setSearch(query.q));
store.dispatch(setWatchedCases(watchedCases));
store.dispatch(setWatchedCasesDetails(watchedCasesDetails));
store.dispatch(setLoggedInUserId(loggedInUser));
thisSession != false &&
store.dispatch(setContainerID(thisSession.user.id));
return {
props: {
showLoginCheck: showLoginCheck