This repository has been archived on 2024-06-14. You can view files and clone it, but cannot push or open issues or pull requests.
icynet-admin/lib/utils/get-active-cookie.ts

18 lines
498 B
TypeScript
Raw Normal View History

2022-08-29 18:09:28 +00:00
import { GetServerSidePropsContext } from 'next';
import { getCookie } from './get-cookie';
export const getActiveCookie = (
ctx: GetServerSidePropsContext,
cookieName: string
): string | undefined => {
const setCookies = ctx.res.getHeader('set-cookie') as string[];
const fallback = ctx.req.cookies[cookieName];
const entry = setCookies?.find((item) => item.startsWith(cookieName));
if (entry === 'undefined') {
return;
}
return getCookie(cookieName, entry) || fallback;
};