18 lines
498 B
TypeScript
18 lines
498 B
TypeScript
|
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;
|
||
|
};
|