Files
pedwfrontend/lib/auth/resolveMyPortalAuthContext.js
T
Robert Bond c2ae6964f9 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
2026-05-14 10:38:45 +00:00

59 lines
1.8 KiB
JavaScript

import { getSession } from "next-auth/react";
import { consoleLogger } from "../../actions/core/logger";
const signinRedirect = {
destination: "/auth/signin",
permanent: false
};
const buildSigninRedirectResult = () => ({ redirect: signinRedirect });
export async function resolveMyPortalAuthContext(ctx, options = {}) {
const {
loggerName = "MyPortalAuthGuard",
noSessionMessage = "myportal loader missing session; redirecting to signin",
noSessionUserMessage = "myportal loader missing session user identity; redirecting to signin",
noPinsUserMessage = "myportal loader missing pinsUser cookie; redirecting to signin"
} = options;
const session = await getSession(ctx);
if (!session) {
consoleLogger({
name: loggerName,
reasonCode: "NO_SESSION",
message: noSessionMessage
});
return { ok: false, redirect: buildSigninRedirectResult().redirect };
}
if (!session?.user?.id || !session?.user?.email) {
consoleLogger({
name: loggerName,
reasonCode: "NO_SESSION_USER",
message: noSessionUserMessage,
hasSessionUserId: !!session?.user?.id,
hasSessionUserEmail: !!session?.user?.email
});
return { ok: false, redirect: buildSigninRedirectResult().redirect };
}
const pinsUser = ctx?.req?.cookies?.pinsUser;
if (!pinsUser) {
consoleLogger({
name: loggerName,
reasonCode: "NO_PINSUSER_COOKIE",
message: noPinsUserMessage
});
return { ok: false, redirect: buildSigninRedirectResult().redirect };
}
return {
ok: true,
session,
sessionUserId: session.user.id,
sessionUserEmail: session.user.email,
pinsUser
};
}