File size: 1,272 Bytes
ebe891e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Cookie options for iframe compatibility using CHIPS (Cookies Having Independent Partitioned State)
export interface IframeCookieOptions {
  expires?: Date;
  maxAge?: number;
  sameSite?: "strict" | "lax" | "none";
  secure?: boolean;
  partitioned?: boolean;
  domain?: string;
  path?: string;
}

/**
 * Get cookie options optimized for iframe usage
 * Uses CHIPS (Cookies Having Independent Partitioned State) for cross-site cookie support
 */
export function getIframeCookieOptions(
  customOptions: Partial<IframeCookieOptions> = {}
): IframeCookieOptions {
  return {
    sameSite: "none",
    secure: true,
    partitioned: true,
    path: "/",
    ...customOptions,
  };
}

/**
 * Get cookie options for the auth token specifically
 */
export function getAuthCookieOptions(expiresIn?: number): IframeCookieOptions {
  return getIframeCookieOptions({
    expires: expiresIn 
      ? new Date(Date.now() + expiresIn * 1000)
      : undefined,
  });
}

/**
 * Get cookie options for removing iframe-compatible cookies
 * Sets the cookie to expire immediately while maintaining the same attributes
 */
export function getRemoveCookieOptions(): IframeCookieOptions {
  return getIframeCookieOptions({
    expires: new Date(0), // Set to epoch time (expired)
  });
}