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
+66 -101
View File
@@ -1,4 +1,3 @@
import { getSession } from "next-auth/react";
import useTranslation from "next-translate/useTranslation";
import Head from "next/head";
import { useRouter } from "next/router";
@@ -39,6 +38,7 @@ import {
import { wrapper } from "../../store/store";
import ServiceBanner from "../../components/myportal/servicebanner";
import TimeOut from "../../components/timeout";
import { resolveMyPortalAuthContext } from "../../lib/auth/resolveMyPortalAuthContext";
const Home = (props) => {
const { footerLinks, pages } = props;
@@ -147,17 +147,51 @@ export const getServerSideProps = wrapper.getServerSideProps(
store.dispatch(setShowReps(showReps, showLoginCheck));
store.dispatch(setSearch(Object.entries(query)));
const { cookies } = req;
const authContext = await resolveMyPortalAuthContext(ctx, {
loggerName: "MyPortalAddressSearchResultsAuthGuard",
noSessionMessage:
"myportal addresssearchresults loader missing session; redirecting to signin",
noSessionUserMessage:
"myportal addresssearchresults loader missing session user identity; redirecting to signin",
noPinsUserMessage:
"myportal addresssearchresults loader missing pinsUser cookie; redirecting to signin"
});
let loggedInUser = cookies.pinsUser;
let thisSession = await getSession(ctx);
if (!authContext.ok) {
return { redirect: authContext.redirect };
}
if (!thisSession) {
const thisSession = authContext.session;
const loggedInUser = authContext.pinsUser;
thisSession != false &&
store.dispatch(setContainerID(thisSession.user.id));
let accountDetails;
let searchResultsObj;
let watchedCases;
let searchDetailsObj;
let watchedCasesDetails;
try {
[accountDetails, searchResultsObj, watchedCases] =
await Promise.all([
getPersonalAccount(loggedInUser),
getAdvancedSearch(query),
getWatchedCases(loggedInUser)
]);
[searchDetailsObj, watchedCasesDetails] = await Promise.all([
getSearchDetails(searchResultsObj),
getDetails(watchedCases, "myWatchedCases")
]);
} catch (error) {
consoleLogger({
name: "MyPortalAddressSearchResultsAuthGuard",
reasonCode: "NO_SESSION",
reasonCode: "UPSTREAM_FAILURE",
message:
"myportal addresssearchresults loader missing session; redirecting to signin"
"myportal addresssearchresults loader dependency call failed; redirecting to signin",
error: error?.message
});
return {
redirect: {
@@ -165,102 +199,33 @@ export const getServerSideProps = wrapper.getServerSideProps(
permanent: false
}
};
} else {
if (!thisSession?.user?.id || !thisSession?.user?.email) {
consoleLogger({
name: "MyPortalAddressSearchResultsAuthGuard",
reasonCode: "NO_SESSION_USER",
message:
"myportal addresssearchresults loader missing session user identity; redirecting to signin",
hasSessionUserId: !!thisSession?.user?.id,
hasSessionUserEmail: !!thisSession?.user?.email
});
return {
redirect: {
destination: "/auth/signin",
permanent: false
}
};
}
if (!loggedInUser) {
consoleLogger({
name: "MyPortalAddressSearchResultsAuthGuard",
reasonCode: "NO_PINSUSER_COOKIE",
message:
"myportal addresssearchresults loader missing pinsUser cookie; redirecting to signin"
});
return {
redirect: {
destination: "/auth/signin",
permanent: false
}
};
}
thisSession != false &&
store.dispatch(setContainerID(thisSession.user.id));
let accountDetails;
let searchResultsObj;
let watchedCases;
let searchDetailsObj;
let watchedCasesDetails;
try {
[accountDetails, searchResultsObj, watchedCases] =
await Promise.all([
getPersonalAccount(loggedInUser),
getAdvancedSearch(query),
getWatchedCases(loggedInUser)
]);
[searchDetailsObj, watchedCasesDetails] = await Promise.all([
getSearchDetails(searchResultsObj),
getDetails(watchedCases, "myWatchedCases")
]);
} catch (error) {
consoleLogger({
name: "MyPortalAddressSearchResultsAuthGuard",
reasonCode: "UPSTREAM_FAILURE",
message:
"myportal addresssearchresults loader dependency call failed; redirecting to signin",
error: error?.message
});
return {
redirect: {
destination: "/auth/signin",
permanent: false
}
};
}
if (!accountDetails || accountDetails?.errorCode) {
consoleLogger({
name: "MyPortalAddressSearchResultsAuthGuard",
reasonCode: "CONTACT_LOOKUP_FAILED",
message:
"myportal addresssearchresults loader account lookup failed; redirecting to signin",
hasAccountDetails: !!accountDetails,
hasErrorCode: !!accountDetails?.errorCode
});
return {
redirect: {
destination: "/auth/signin",
permanent: false
}
};
}
store.dispatch(setAccountDetails(accountDetails));
store.dispatch(setSearchResults(searchResultsObj));
store.dispatch(setSearchDetails(searchDetailsObj));
store.dispatch(setWatchedCases(watchedCases));
store.dispatch(setWatchedCasesDetails(watchedCasesDetails));
store.dispatch(setSearch(Object.entries(query)));
store.dispatch(setLoggedInUserId(loggedInUser));
}
if (!accountDetails || accountDetails?.errorCode) {
consoleLogger({
name: "MyPortalAddressSearchResultsAuthGuard",
reasonCode: "CONTACT_LOOKUP_FAILED",
message:
"myportal addresssearchresults loader account lookup failed; redirecting to signin",
hasAccountDetails: !!accountDetails,
hasErrorCode: !!accountDetails?.errorCode
});
return {
redirect: {
destination: "/auth/signin",
permanent: false
}
};
}
store.dispatch(setAccountDetails(accountDetails));
store.dispatch(setSearchResults(searchResultsObj));
store.dispatch(setSearchDetails(searchDetailsObj));
store.dispatch(setWatchedCases(watchedCases));
store.dispatch(setWatchedCasesDetails(watchedCasesDetails));
store.dispatch(setSearch(Object.entries(query)));
store.dispatch(setLoggedInUserId(loggedInUser));
return {
props: {
searchResultsObj: {