File size: 2,666 Bytes
c2bb873
 
 
 
 
 
 
 
 
 
 
 
 
cbc1bda
 
 
 
 
 
 
 
 
 
 
 
 
 
c2bb873
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
 * Utility for handling storage in iframe contexts
 * Falls back to localStorage when cookies are blocked
 */

export const isInIframe = (): boolean => {
  try {
    return window.self !== window.top;
  } catch {
    return true; // If we can't access window.top, we're likely in an iframe
  }
};

export const isMobileDevice = (): boolean => {
  if (typeof window === 'undefined') return false;
  
  // Check user agent for mobile patterns
  const userAgent = window.navigator.userAgent;
  const mobilePattern = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini|Mobile|mobile|CriOS/i;
  
  // Also check for touch capability and screen size
  const hasTouchScreen = 'ontouchstart' in window || navigator.maxTouchPoints > 0;
  const hasSmallScreen = window.innerWidth <= 768; // Common mobile breakpoint
  
  return mobilePattern.test(userAgent) || (hasTouchScreen && hasSmallScreen);
};

export const STORAGE_KEYS = {
  ACCESS_TOKEN: "deepsite-auth-token-fallback",
  USER_DATA: "deepsite-user-data-fallback",
} as const;

export const iframeStorage = {
  setItem: (key: string, value: string): void => {
    try {
      localStorage.setItem(key, value);
    } catch (error) {
      console.warn("Failed to set localStorage item:", error);
    }
  },

  getItem: (key: string): string | null => {
    try {
      return localStorage.getItem(key);
    } catch (error) {
      console.warn("Failed to get localStorage item:", error);
      return null;
    }
  },

  removeItem: (key: string): void => {
    try {
      localStorage.removeItem(key);
    } catch (error) {
      console.warn("Failed to remove localStorage item:", error);
    }
  },

  clear: (): void => {
    try {
      localStorage.removeItem(STORAGE_KEYS.ACCESS_TOKEN);
      localStorage.removeItem(STORAGE_KEYS.USER_DATA);
    } catch (error) {
      console.warn("Failed to clear localStorage items:", error);
    }
  },
};

export const storeAuthDataFallback = (accessToken: string, userData: any): void => {
  if (isInIframe()) {
    iframeStorage.setItem(STORAGE_KEYS.ACCESS_TOKEN, accessToken);
    iframeStorage.setItem(STORAGE_KEYS.USER_DATA, JSON.stringify(userData));
  }
};

export const getAuthDataFallback = (): { token: string | null; user: any | null } => {
  if (isInIframe()) {
    const token = iframeStorage.getItem(STORAGE_KEYS.ACCESS_TOKEN);
    const userDataStr = iframeStorage.getItem(STORAGE_KEYS.USER_DATA);
    const user = userDataStr ? JSON.parse(userDataStr) : null;
    return { token, user };
  }
  return { token: null, user: null };
};

export const clearAuthDataFallback = (): void => {
  if (isInIframe()) {
    iframeStorage.clear();
  }
};