48 lines
1.2 KiB
JavaScript
48 lines
1.2 KiB
JavaScript
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)
|
|
});
|
|
};
|