Merged PR 2315: Auth stabilistatiion and hardening
Related work items: #23020
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
import { destroyCookie } from "nookies";
|
||||
import { signOut } from "next-auth/react";
|
||||
|
||||
const AUTH_COOKIE_CANDIDATES = [
|
||||
"next-auth.csrf-token",
|
||||
"next-auth.callback-url",
|
||||
"__Secure-next-auth.callback-url",
|
||||
"pedw_locale",
|
||||
"pinsUser"
|
||||
];
|
||||
|
||||
export const buildSignedOutCallbackUrl = (locale) =>
|
||||
locale === "cy" ? "/cy/allgofnodi" : "/logout";
|
||||
|
||||
const resolveSignOutArgs = (args) => {
|
||||
if (typeof args === "string") {
|
||||
return {
|
||||
locale: args,
|
||||
callbackUrl: undefined,
|
||||
signOutFn: signOut
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
locale: args?.locale,
|
||||
callbackUrl: args?.callbackUrl,
|
||||
signOutFn: args?.signOutFn || signOut
|
||||
};
|
||||
};
|
||||
|
||||
export const clearSessionArtifacts = () => {
|
||||
if (typeof window !== "undefined") {
|
||||
window.localStorage.clear();
|
||||
}
|
||||
|
||||
AUTH_COOKIE_CANDIDATES.forEach((cookieName) => {
|
||||
destroyCookie(null, cookieName, { path: "/" });
|
||||
});
|
||||
};
|
||||
|
||||
export const performPortalSignOut = async (args) => {
|
||||
const { locale, callbackUrl, signOutFn } = resolveSignOutArgs(args);
|
||||
clearSessionArtifacts();
|
||||
return signOutFn({
|
||||
callbackUrl: callbackUrl || buildSignedOutCallbackUrl(locale)
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user